I have a program that runs a long process after you click an action button. As the process is running the root window will say that it is not responding even though I know the program is running in the background. This program is going to be released to a few people that I work with and I want to make sure they don't freak out and close the window when they see this. The solution I have is sitting a root.update in the loop of the process that is running but I am not sure this was the best fix.
Using the python 3.3
Here is a sample of the code so you get an idea of what I am doing, this is called from the main loop:
def combine(boxes_to, boxes_from, frame):
to_value,to_pos = gui.checkBoxes(boxes_to)
from_value,from_pos = gui.checkBoxes(boxes_from)
frame.destroy()
running = Label(root,text="Running please do not close..",font = (16))
running.pack()
root.update()
map_to = open("map_to",'r')
for line in map_to:
root.update()
process(line)
running.destroy()
map_to.close()
finish = Button(root, text="Done",command=gui.stop)
finish.pack()
While you can call root.update() in your loop, this will still produce some (potentially) undesirable side-effects.
The program may act laggy, meaning it takes a long time to respond to user input.
You will only be able to run this one action. Any other action has to wait for this to finish.
As an alternative I would suggest that you implement simple multi-threading. Python multithreading is pretty simple, and will prevent both of these drawbacks. You will be able to execute your long running code, while still providing a clean and responsive UI.
If your application is trivially parallelizable, you could use multiple threads to decrease running time. Ex. Thread 1 handles entries 1-100, while thread 2 handles entries 101-200.
The best you can do here is to use multithreading in Python. Here's how to do this:
Let's say you have a function named combine() due to which the window is freezing, which is being used as a command for a button named 'btn' as shown here:
btn = Button(root, text="Click Me", command=combine)
Now, when btn is pressed you might be getting the 'not responding' problem. To fix this, edit the code as shown below:
import threading
btn = Button(root, text="Click Me", command=threading.Thread(target=combine).start)
Here threading.Thread creates a separate thread in which the combine() method is executed, so the GUI can continue to keep responding while the command is being executed.
Related
MyButton1 =Button(master, text='Quit',bg="grey",width=20,
command=master.quit)
MyButton1.place(x=200, y=100)
MyButton2 =Button(master, text='Propagate', bg="grey",width=20,
command=mainmethod)
MyButton2.place(x=1000, y=100)
master.geometry("1500x1500")
master.mainloop( )
In the above code after pressing propagate button mainmethod is invoking..
I wrote my logic in main method where this method alone taking 2minutes to execute in the mean time GUI going unresponsive state for few min and later displaying all my required output on text box i inserted
whether any away to avoid the unresponsive issue apart from using multi threading
and i am looking such that after pressing propagate button button should disabled and window should not go unresponsive and display text.insert statements continuously which i added in main method ?????
To prevent hanging, you need to separate the calculations in the mainmethod from Tkinter's main loop by executing them in different threads. However, threading system in Python is not that well-developed as in other languages (AFAIK) because of GIL (Global Interpreter Lock), but there is an alternative - using processes just like threads. This is possible with multiprocessing library.
In order to just prevent hanging, you could create another function
from multiprocessing import Process
def mainmethodLaunch():
global mainmethodProcess
mainmethodProcess = Process(target=mainmethod)
mainmethodProcess.start()
And bind this function to MyButton2 instead of mainmethod itself.
Docs: https://docs.python.org/2/library/multiprocessing.html#the-process-class
You can see p.join in the example. join method will cause your main process to wait for the other one to complete, which you don't want.
So when you press the button, mainmethodLaunch function will be invoked, and it will create another process executing mainmethod. mainmethodLaunch function's own run duration should be insignificant. Due to usage of another process, Tkinter window will not hang. However, if you do just this, you will not be able to interact with mainmethod process in any kind while it will be working.
In order to let these processes communicate with each other, you could use pipes (https://docs.python.org/2/library/multiprocessing.html#exchanging-objects-between-processes)
I guess the example is quite clear.
In order to receive some data from the mainmethod process over time, you will have to poll the parent_conn once a little time, let's say, second. This can be achieved with Tkinter's after method
(tkinter: how to use after method)
IMPORTANT NOTE: when using multiprocessing, you MUST initialize the program in if __name__ == '__main__': block. I mean, there should be no doing-something code outside functions and this block, no doing-something code with zero indent.
This is because multiprocessing is going to fork the same Python executable file, and it will have to distinguish the main process from the forked one, and not do initializing stuff in the forked one.
Check twice if you have done that because if you make such a mistake, it can cost you hanging of not just Tkinter window, but the whole system :)
Because the process will be going to fork itself endlessly, consuming all RAM you have, regardless of how much you have.
I developed a simple Python application doing some stuff, then I decided to add a simple GUI using Tkinter.
The problem is that, while the main function is doing its stuff, the window freezes.
I know it's a common problem and I've already read that I should use multithreads (very complicated, because the function updates the GUI too) or divide my code in different function, each one working for a little time.
Anyway I don't want to change my code for such a stupid application.
My question is: is it possible there's no easy way to update my Tkinter window every second? I just want to apply the KISS rule!
I'll give you a pseudo code example below that I tried but didn't work:
class Gui:
[...]#costructor and other stuff
def refresh(self):
self.root.update()
self.root.after(1000,self.refresh)
def start(self):
self.refresh()
doingALotOfStuff()
#outside
GUI = Gui(Tk())
GUI.mainloop()
It simply will execute refresh only once, and I cannot understand why.
Thanks a lot for your help.
Tkinter is in a mainloop. Which basically means it's constantly refreshing the window, waiting for buttons to be clicked, words to be typed, running callbacks, etc. When you run some code on the same thread that mainloop is on, then nothing else is going to perform on the mainloop until that section of code is done. A very simple workaround is spawning a long running process onto a separate thread. This will still be able to communicate with Tkinter and update it's GUI (for the most part).
Here's a simple example that doesn't drastically modify your psuedo code:
import threading
class Gui:
[...]#costructor and other stuff
def refresh(self):
self.root.update()
self.root.after(1000,self.refresh)
def start(self):
self.refresh()
threading.Thread(target=doingALotOfStuff).start()
#outside
GUI = Gui(Tk())
GUI.mainloop()
This answer goes into some detail on mainloop and how it blocks your code.
Here's another approach that goes over starting the GUI on it's own thread and then running different code after.
'Not responding' problem can be avoided using Multithreading in python using the thread module.
If you've defined any function, say combine() due to which the Tkinter window is freezing, then make another function to start combine() in background as shown below:
import threading
def combine():
...
def start_combine_in_bg():
threading.Thread(target=combine).start()
Now instead of calling combine(), you have to call start_combine_in_bg()
I did this and now my window is not freezing so I shared it here. Remember you can run only one thread at a time.
Have a look for reference: stackoverflow - avoid freezing of tkinter window using one line multithreading code
Here's how I managed to work around this issue (maybe it's quite dirty I don't know):
Whenever I update my tkinter window through the doingALotOfStuff() function, I call myTk.update().
It avoids freezing for me.
I am working on a very long piece of code and one which I would like to take GUI input more than once in multiple windows.
The code is too long and wouldn't make much sense if I put it in here, but here's the layout of my code:
callback1():
do something
tkinter stuff, using callback1()
...
lots of other code (none to do with tkinter)
...
callback2():
do something else
tkinter stuff using callback2()
some more code (none to do with tkinter)
mainloop()
Sorry for the vague structure, but this is more or less the layout of my code. The problem is though, that mainloop() never seems to be reached, and my other code in between the callbacks relies on the input from the GUI and so it crashes.
I've tried:
callback1():
do something
after(500, callback1)
tkinter stuff, using callback1()
...
lots of other code (none to do with tkinter)
...
callback2():
do something else
after(500, callback1)
tkinter stuff using callback2()
some more code (none to do with tkinter)
after(0, callback1)
after(0, callback2)
mainloop()
But had no luck. Can anyone help me with the correct usage of after() (or any other function) so that I can just get the GUI to display?
GUI frameworks like tkinter are event-driven. They start running when they start their mainloop(). Everything before that is basically set-up and housekeeping.
After the mainloop starts, the only pieces of your code that they run are callbacks that you've attached to interface elements like buttons or to timers with after.
These callbacks must be relatively short and should not take a long time. The GUI toolkit calls the function you provided to handle an event in the mainloop. So while your callback is running, no further events are being processed and the GUI is effectively frozen. If that only takes a little while (say 50 milliseconds) nobody will notice.
But if you want to run a computation that takes several seconds to complete, you cannot do that in a callback without freezing the GUI.
You could consider doing the long-running job in a second thread. But in CPython only one thread at a time can be executing Python bytecode for technical reasons. However (especially in Python 3) threads are forced to relinquish the CPU regularly, so this might be a workable solution.
Another solution is to farm long-running jobs out to a separate process using multiprocessing. In this case you do have to set-up communication between the processes via e.g. a Queue.
You can't organize your code like that. Presumably, you have something similar to this:
import tkinter as tk
mainwindow = tk.Tk()
mainwindow.geometry("300x200")
mainwindow.title("MyWindow")
label = tk.Label(mainwindow, text="Enter stuff, then click the button.")
label.pack()
var = tk.StringVar()
entry = tk.Entry(mainwindow, textvariable=var)
entry.pack()
#callback1():
def onclick():
#do something with var.get() ?
pass
button = tk.Button(mainwindow, text="click me", command=onclick)
button.pack()
#lots of other code (none to do with tkinter):
print("The value I need is: {}".format(var.get()))
mainwindow.mainloop()
The output of that program is:
The value I need is:
You need to put your code in either the callback function, or another function that is called by the callback function:
def my_func():
print("The value I need: {}".format(var.get()))
def onclick():
#do something with var.get()
my_func()
button = tk.Button(mainwindow, text="click me", command=onclick)
Now, the output is something like:
The value I need is: 10
tkinter is a GUI framework, and it responds to events that happen in your window. The way you respond to an event is to wrap your code in a callback function and pass the function to tkinter, which will then call the function at the proper time. You do not write arbitrary code in the middle of a tkinter program.
The after() method does not seem appropriate, because it essentially says:
After the mainloop starts, execute this function.
You can delay the execution for however many seconds you want--but that doesn't mean that the user will have entered the data you need by then. What you want to do is execute your code after the user enters their data, and usually you do that by responding to a button click, where the button says, Submit, or some other event.
I'm writing a GUI application that has a button which invokes a long task. In order for it not to freeze the GUI I delegate the task to a different process using python 3.3's multiprocessing module. Then I return the result for display using a Pipe.
I want the application not to leave any zombie process even if quit during the computation. As I'm on a mac this can happen one of two ways: through quitting the application (Command+Q) or but closing it's window.
Here's the code in the function linked to a button in the GUI:
main_pipe,child_pipe=Pipe()
p=Process(target=worker,args=(child_pipe,data))
p.start()
try:
while not main_pipe.poll():
root.update()
value_array=main_pipe.recv()
finally:
p.join()
This doesn't work the application doesn't respond to Command+q, and closing the window leaves two zombie process running (one for the GUI and one for the worker).
How to make it work in the other case as well?
Is this good practice? Is the a nicer, more pythonic way of doing it?
Additionally at the very end of the script I have those two lines (the exit() closes the application if the window is closed while not processing anything):
root.mainloop()
exit()
And finally, what's the difference between update() and mainloop()? Is it only that the latter hogs up the program while update() doesn't?
Ok, I finally solved it. Although I not complete sure regarding this method's pythoness or side effects, if somebody needs it here it is.
I figured that quitting correctly can only happen in a mainloop() not in a update() so I wrote two functions, one for creating the process, and one for checking it's output and they call each other using root.after(). I set the processes daemon flag to true to ensure proper quitting behaviour. Here's the code:
def process_start():
global value_array
global main_pipe
main_pipe,child_pipe = Pipe()
p=Process(target=worker,args=(child_pipe,data))
p.daemon=True
p.start()
root.after(500,check_proc)
def check_proc():
if not main_pipe.poll():
root.after(500,check_proc)
else:
global value_array
value_array=main_pipe.recv()
I'm still not sure if p.join() is needed but the deamon thing seems to get around the zombie-proceses problem
I was facing the similar problem earlier (except I wasn't using multiprocess). After nearly a whole days research, I come up with the following conclusion:
mainloop and root.waitwindow will (sometimes) block the sys.exit signal, which your program should receive after you hit ⌘Q.
You can bind ⌘Q to a new function, although you may still receive the sys.exit signal
Another way (more reliable in my case) is to remap the tcl quitting signal to another function, instead of the default one. You can remap the quit event (dock quit and ⌘Q) using this: root.createcommand('::tk::mac::Quit',function)
When trying to quit the software, use sys.exit instead of exit() or quit()
You can also use root.wm_protocol("WM_DELETE_WINDOW", function) to define the behavior when users clicking on the red button.
You can actually make the border of the window and the three default buttons disappear by flagging root.overridedirect(1) and thus force the user to click on a button on your GUI instead of closing the window.
I am having an issue with the portion of code below:
my_var = tk.StringVar(value="start")
my_label = tk.Label(self.root, textvariable=my_var)
def my_method(input):
my_var.set("working")
#make a method call to an external Python API; can take a few mins to finish
my_var.set("complete")
tk.Button(self.root, text="GO!", command=lambda: my_method(input))
I have a button that, when clicked, should execute my_method which should initially change the text in my_label to working, process some actions in an external API, then update the label to complete. What is actually happening is my_method is called, the label's text DOES NOT change, the API call is made which takes a few mins, then my label is updated to complete. I am assuming that it processes both the set commands quickly, but I only see the final set value.
Is there a reason why this is happening (have I done something wrong) and is there a solution to it? If this is expected behaviour, could someone explain why it is the case?
Whilst writing this, I thought about maybe launching a thread to execute the initial set call to see if that will work. I'd prefer a simpler solution though.
Tkinter needs you to return promptly from the callback, so that it can get back to the processing it needs to be doing in Tk.mainloop. Without being able to do this processing, it can't update your label's text. (Nor can it handle normal processing events like dragging the window, etc.)
The callbacks should never hang onto the main thread for long periods of time, as a result. If there's some time-consuming processing to do for them, it should be done in a separate thread.