4

How to run multiple processes in python without multithreading? For example consider the following problem:-

We have to make a Gui,which has a start button which starts a function(say, prints all integers) and there is a stop button, such that clicking it stops the function.

How to do this in Tkinter?

Chandan
  • 749
  • 1
  • 8
  • 11
  • 1
    when you say "another process" do you literally mean another process -- spawning a .exe or equivalent? Or, when you say "process" do you really mean "some other function"? I find it hard to believe you would have a whole separate process just to print integers. – Bryan Oakley Feb 25 '13 at 11:40
  • I meant some other function. – Chandan Feb 25 '13 at 18:14
  • 2
    @Chandan: terminology is critically important when asking technical questions. There is a _huge_ difference between a process and a function in this context. You should edit your question to reflect that you don't intend to spawn other processes. – Bryan Oakley Feb 25 '13 at 23:55

2 Answers2

4

Then you need to bind the Button widget with the function which starts the working thread. For example:

import time
import threading
import Tkinter as tk

class App():
    def __init__(self, root):
        self.button = tk.Button(root)
        self.button.pack()
        self._resetbutton()
    def _resetbutton(self):
        self.running = False
        self.button.config(text="Start", command=self.startthread)
    def startthread(self):
        self.running = True
        newthread = threading.Thread(target=self.printints)
        newthread.start()
        self.button.config(text="Stop", command=self._resetbutton)
    def printints(self):
        x = 0
        while self.running:
            print(x)
            x += 1
            time.sleep(1) # Simulate harder task

With the self.running approach, you can end the thread gracefully only by changing its value. Note that the use of multiple threads serves to avoid blocking the GUI while printints is being executed.

I have read this previous question and I suppose why you explicitly asked here for a solution without multithreading. In Tkinter this solution can be used in a scenario where the other threads have to communicate with the GUI part. For example: filling a progressbar while some images are being rendered.

However, as it has been pointed in the comments, this approach is too complex for just printing numbers.

Here you can find a lot of information and more examples about Tkinter.


Edit:

Since your new question has been closed, I'll change the code here to clarify the last point.

Community
  • 1
  • 1
A. Rodas
  • 20,171
  • 8
  • 62
  • 72
  • @BryanOakley I totally agree, but I suppose the use of the worker thread is for something more relevant than printing integers - i.e, process a big amount of data, which may block the GUI until it finishes. – A. Rodas Feb 25 '13 at 12:56
  • 1
    the question specifically asks how to do this _without_ multithreading. – Bryan Oakley Feb 25 '13 at 13:51
  • @A.Rodas How to do it without using two buttons? I mean can we change the text of the button from 'Start' to 'Stop' when the it starts and 'Stop' to 'Start' when it stops? – Chandan Feb 25 '13 at 18:30
  • @BryanOakley you said that Tkinter doesn't support multithreading, then how does it work?(sorry, if I misunderstood the thing) – Chandan Feb 25 '13 at 18:35
  • 2
    @Chandan: as long as nothing in a worker thread attempts to access any Tkinter data structures it will probably work. The problems come from trying to access GUI objects from more than one thread. – Bryan Oakley Feb 25 '13 at 19:03
  • @Chandan Yes, it can be done by calling `config` with the new values. However, if you need a more detailed answer, it's better to ask it in a new question. – A. Rodas Feb 25 '13 at 19:31
  • @Chandan I've edited the answer to include the change of the button. – A. Rodas Feb 25 '13 at 23:44
  • @A.Rodas thank you million times for such brilliant answer which solved the very annoying screen freezing caused by **while loop**, I am sharing your code as **tkinter while loop** boilerplate. –  Nov 11 '20 at 12:44
0

Did you try to used multiprocessing module? Seems to be the one you're looking for.

Dr I
  • 288
  • 5
  • 19