Change text on tkinter button during call back execution - python

I'm trying to change the text on a button when the call back of that particular button executing.
Let's say I have "Run" as the text on my button, and I want to change it to "Running" just after it has been clicked, and during the call back execution.
After the completion of call back execution, I want to change it back to "Run".
I am not getting which part of my code is buggy.
import Tkinter as tk
import time
root = tk.Tk()
def change():
button.config(text='Running')
button.config(state='disabled')
print "start"
time.sleep(5)
print "end"
button.config(state='normal')
button.config(text="Run")
button = tk.Button(root,text="Run",command=change)
button.pack()
root.mainloop()

Sleep makes the WHOLE program stop for a moment, including the interface, usually stopping it from changing the interface itself, due to how the mainloop works.
Here, try the .after function, this should work fine.
import Tkinter as tk
import time
root = tk.Tk()
def change():
button.config(text='Running')
button.config(state='disabled')
print "start"
root.after(5000,changeback)
def changeback():
print "end"
button.config(state='normal')
button.config(text="Run")
button = tk.Button(root,text="Run",command=change)
button.pack()
root.mainloop()
root.after(5000,changeback) will call a command after 5000 milliseconds, or 5 seconds without stopping the whole program, that being changeback()

Related

How to control when does the console shows in python

I am making a python script that shows content in the console for the first 10-20 seconds after executed before the tkinter UI is shown. So is there any lines I can add to control the visibility of the console? .pyw file extension completely disables the console but I need the console to occasionally show up. Any suggestions?
First of all, you need to write your entire Tkinter code under a function.
def main_program():
# root = Tk() ...
Below this you need to write an empty print statement for the console. Below this, you can write anything. But the problem is sometimes, console quickly opens, prints the statement and closes it, so you barely have time to see
So, use time.sleep() -
import time
from tkinter import *
def main():
root = Tk()
root.mainloop()
print('Wait for a second')
time.sleep(1)
main()
This will show the print statement, delay the program for 1 second, and then open Tkinter window.
There are 2 ways to do it.
You can withdraw the window, print whatever you want and at the last, make it visible. .withdraw basically hides the window without any changes to the widgets.
from tkinter import *
root=Tk()
root.withdraw()
print("Hey there! Please wait for 10 seconds for the app to start...")
root.after(10000,lambda: root.deiconify())
root.mainloop()
Or, the second one is
from tkinter import *
import time
def create_window():
root = Tk()
root.mainloop()
print('Hey there! Please wait for 10 seconds for the app to start...')
time.sleep(10)
create_window()

What kind of code does tkinter's mainloop collect?

Suppose I have Python code like this
# <Pure Python statement A>
root = tk.Tk()
mainframe = tk.Frame(root)
# <Pure Python statement B>
# <other tkinter code>
root.mainloop()
Which statements are then ending up on tkinter's mainloop? Is it just the 3 tkinter statements?
EDIT
There must be more things going on, because some code between the tkinter code is affected: When I run the following code (taken from another question)
import tkinter as tk
import tkinter.filedialog
filename = ""
def op():
global filename
filename =tk.filedialog.askopenfilename()
root = tk.Tk()
mainframe = tk.Frame(root)
mainframe.grid(column=0, row=0)
tk.Button(mainframe, text="Open file", command=op).grid(column=0, row=1)
root.mainloop()
print(filename)
after closing the program the selected filename is displayed. But when running
import tkinter as tk
import tkinter.filedialog
filename = "this_is_a_test"
def op():
global filename
filename =tk.filedialog.askopenfilename()
root = tk.Tk()
mainframe = tk.Frame(root)
mainframe.grid(column=0, row=0)
tk.Button(mainframe, text="Open file", command=op).grid(column=0, row=1)
print(filename)
root.mainloop()
after closing the program, nothing is displayed. So somehow the pure Python statements before mainloop seem to get absorbed.
What kind of code does tkinter's mainloop collect?
It doesn't collect anything. It simply processes events, and calls functions bound to those events. It also calls functions added to the queue via after.
Which statements are then ending up on tkinter's mainloop? Is it just the 3 tkinter statements?
Nothing "ends up on tkinter's mainloop". That's a nonsensical statement, nothing can end up on it. It is just a function that processes events, and doesn't return until the window is destroyed. All code before the call to mainloop executes according to the normal rules of python.
Calling mainloop is effectively the same as if you put this in its place (but it is much more efficient):
while True:
self.update()
Much like with the above, any code after mainloop() will not execute until the loop exits, which happens when the window has been destroyed.
The reason your print seems to work after the call to mainloop but not before is simply that before mainloop, filename is the empty string. The print run normally, it's just that there's nothing to print. That print statement happens a few milliseconds after the program starts, way before the user has a chance to do anything. When called after, it seems to work because that code doesn't run until the window has been destroyed. At that point it presumably has a value, so you see something printed.
The simple answer is: There's no kind of code that mainloop collects.
It 'collect' s all configuration that is related to the Tcl interpreter it is a method of. As in if your GUI is a configuration of root = tk.Tk(), and the mainloop is a method of root then all configurations under it will be accounted for such as children widgets and their configurations.
Your print statement doesn't get absorbed. It simply prints what would've been printed if the button was never used. Try the 2nd code with simply closing the GUI without using the button. mainloop doesn't absorb anything. It simply waits for events for the GUI configured.

Opening new window in Tkinter freezes the program (python 3.6)

I have something like this:
first.py
from tkinter import *
def new_window(event):
root.destroy()
import second
root = Tk()
b = Button(root, text='New window')
b.pack()
b.bind('<Button-1>', new_window)
root.mainloop()
second.py
from tkinter import *
root = Tk()
root.mainloop()
But when I open the second window, the first one is destroyed (I hope so), but the second one is frozen (it is shown, but there is no close-button on the top and I only see the launch-icon). Why is it so? Don't I kill the first loop?
The problem is likely because import second never returns since the last thing it does is call root.mainloop(). Since it never returns, the callback in the first window never finishes. And since it never finishes, it's not able to process any other events.

Close tkinter GUI without killing the app

I'm using Tkinter to create a GUI program on python 2.7.
At some point, I want to open an informative window and close it automatically later on, the problem is, when I call quit on it the whole application closes.
root = tk.Tk()
root.title("WINDOW")
def create_wnd(xxx, yyy):
yyy.destroy()
def run_wnd():
dialog = tk.Toplevel()
dialog.title("wnd2")
wnd_run_button = tk.Button(root, text="RUN", command=lambda:run_wnd())
wnd_run_button.pack()
root.mainloop()
Put "the rest of the application" after the call to mainloop. Killing the window causes mainloop to exit, but any code after that will continue to run.
Or, put all of the window code inside a function that finishes by calling `mainloop, so the function won't exit until the window is destroyed. The code that calls the function will continue as soon as the function returns.

label.configure works sometimes why?

Part of my code is as follows:
def get_songs():
label6.configure(text='Wait')
os.system('/home/norman/my-startups/grabsongs')
label6.configure(text='Done')
The label is not updated at the first .configure() but is at the second one.
Except if I cause a deliberate error immediately after the first one at which point it is updated and then the program terminates.
The system call takes about 2 minutes to complete so it isn't as if there isn't time to display the first one.
I am using Python 2.7.6
Does anyone know why please?
I'm going to guess you're using Tkinter. If so, as #albert just suggested, you'll want to call label.update_idletasks() or label.update() to tell Tkinter to refresh the display.
As a very crude example to reproduce your problem, let's make a program that will:
Wait 1 second
Do something (sleep for 2 seconds) and update the text to "wait"
Display "done" afterwards
For example:
import Tkinter as tk
import time
root = tk.Tk()
label = tk.Label(root, text='Not waiting yet')
label.pack()
def do_stuff():
label.configure(text='Wait')
time.sleep(2)
label.configure(text='Done')
label.after(1000, do_stuff)
tk.mainloop()
Notice that "Wait" will never be displayed.
To fix that, let's call update_idletasks() after initially setting the text:
import Tkinter as tk
import time
root = tk.Tk()
label = tk.Label(root, text='Not waiting yet')
label.pack()
def do_stuff():
label.configure(text='Wait')
label.update_idletasks()
time.sleep(2)
label.configure(text='Done')
label.after(1000, do_stuff)
tk.mainloop()
As far as why this happens, it actually is because Tkinter doesn't have time to update the label.
Calling configure doesn't automatically force a refresh of the display, it just queues one the next time things are idle. Because you immediately call something that will halt execution of the mainloop (calling an executable and forcing python to halt until it finishes), Tkinter never gets a chance to process the changes to the label.
Notice that while the gui displays "Wait" (while your process/sleep is running) it won't respond to resizing, etc. Python has halted execution until the other process finishes running.
To get around this, consider using subprocess.Popen (or something similar) instead of os.system. You'll then need to perodically poll the returned pipe to see if the subprocess has finished.
As an example (I'm also moving this into a class to keep the scoping from getting excessively confusing):
import Tkinter as tk
import subprocess
class Application(object):
def __init__(self, parent):
self.parent = parent
self.label = tk.Label(parent, text='Not waiting yet')
self.label.pack()
self.parent.after(1000, self.do_stuff)
def do_stuff(self):
self.label.configure(text='Wait')
self._pipe = subprocess.Popen(['/bin/sleep', '2'])
self.poll()
def poll(self):
if self._pipe.poll() is None:
self.label.after(100, self.poll)
else:
self.label.configure(text='Done')
root = tk.Tk()
app = Application(root)
tk.mainloop()
The key difference here is that we can resize/move/interact with the window while we're waiting for the external process to finish. Also note that we never needed to call update_idletasks/update, as Tkinter now does have idle time to update the display.

Categories

Resources