I'm working on getting a python/tkinter label widget to update its contents. Per an earlier thread today, I followed instructions on how to put together the widgets. At runtime, however, the label widget does NOT change contents, but simply retains its original content. As far as I can tell, decrement_widget() is never called at all. Any ideas?
def snooze (secs):
"""
Snoozes for the given number of seconds. During the snooze, a progress
dialog is launched notifying the
"""
root = Tkinter.Tk()
prompt = 'hello'
label1 = Tkinter.Label(root, text=prompt, width=len(prompt))
label1.pack()
remaining = secs
def decrement_label ():
text = "Snoozing %d sec(s)" % remaining
remaining -= 1
label1.config(text=text, width=100)
label1.update_idletasks()
for i in range(1, secs + 1):
root.after(i * 1000, decrement_label )
root.after((i+1) * 1000, lambda : root.destroy())
root.mainloop()
You'll want to set the label's textvariable with a StringVar; when the StringVar changes (by you calling myStringVar.set("text here")), then the label's text also gets updated. And yes, I agree, this is a strange way to do things.
See the Tkinter Book for a little more information on this:
You can associate a Tkinter variable with a label. When the contents of the variable changes, the label is automatically updated:
v = StringVar()
Label(master, textvariable=v).pack()
v.set("New Text!")
I think you're getting a "referenced before assignment" error because Python thinks remaining is in the local scope.
In Python 3, you can say nonlocal remaining. But in Python 2, I don't believe there's a way to refer to a non-local, non-global scope. This worked for me:
remaining = 0
def snooze (secs):
"""
Snoozes for the given number of seconds. During the snooze, a progress
dialog is launched notifying the
"""
global remaining
root = Tkinter.Tk()
prompt = 'hello'
label1 = Tkinter.Label(root, text=prompt, width=len(prompt))
label1.pack()
remaining = secs
def decrement_label ():
global remaining
text = "Snoozing %d sec(s)" % remaining
remaining -= 1
label1.config(text=text, width=100)
label1.update_idletasks()
for i in range(1, secs + 1):
root.after(i * 1000, decrement_label )
root.after((i+1) * 1000, lambda : root.destroy())
root.mainloop()
import tkinter
from tkinter import *
# just init some vars
remaining = 0
secs = 0
root = tkinter.Tk()
prompt = StringVar()
def snooze (secs):
"""
Snoozes for the given number of seconds. During the snooze, a progress
dialog is launched notifying the
"""
def decrement_label ():
global remaining, prompt
remaining -= 1
prompt.set('Snoozing %d sec(s)' % remaining)
label1.update_idletasks()
if not remaining:
print("end ... ")
root.destroy()
global remaining
prompt.set("hello")
label1 = tkinter.Label(root, textvariable=prompt, width=30)
label1.pack()
remaining = secs
for i in range(1, secs + 1):
root.after(i * 1000, decrement_label )
snooze(10)
root.mainloop()
To update text in a label you can try the following:
from tkinter import *
root = Tk()
root.title("Title")
root.geometry('300x300')
def clear_text(self):
txtE.delete(0, 'end')
def new_label(event=None):
Entree = txtE.get()
lbl1['text'] = Entree.title()
clear_text(txtE)
lbl1 = Label(root, text='Hello There')
lbl1.pack()
txtE = Entry(root)
txtE.focus()
txtE.pack()
Button(root, text='Enter', command=new_label).pack()
Button(root, text='Quit', command=root.destroy).pack(side=BOTTOM)
root.bind('<Return>', new_label)
root.mainloop()
I think you have to call snooze(secs) function
After that if your code again not works try this
Set a variable
Variable = StringVar()
In the label widget you can set "textvariable" argument to the above mentioned "Variable".
Eg:
label1 = Label(root,textvariable = Variable).pack()
And you can update by setting a new value to "Variable"
Eg:Variable.set("hi")
Hope you got it !!!
Related
I am creating an application (or program) that after the button is clicked creates a new window and displays the results of some maths in a listbox. I want it to also display the execution time of the math. I tried using time.time(), but apparently, I can't use that in a function.
I also tried using timeit, but I have no clue how to use it.
How else can I do this?
from tkinter import *
root = Tk()
def do_math():
window = Toplevel(root)
listbox = Listbox(window)
for i in range(1, 10):
math = i ** 2
listbox.insert(END, str(math))
time = Label(window, text="The math took {} seconds to execute.")
time.pack()
listbox.pack()
b1 = Button(root, text="Click me!", command=do_math).pack()
root.mainloop()
You can't use time.time() function because you had a variable named time inside your function which was causing some exception popping out (UnboundLocalError), renaming that variable to something else OR inserting global time ontop of the function would fix it in your case.
from tkinter import *
from time import time
root = Tk()
def do_math():
# global time
window = Toplevel(root)
listbox = Listbox(window)
start = time()
for i in range(1, 10000):
math = i ** 2
listbox.insert(END, str(math))
total = time() - start
label = Label(window, text="The math took {} seconds to execute.".format(str(total)))
label.pack()
listbox.pack()
b1 = Button(root, text="Click me!", command=do_math).pack()
root.mainloop()
from the List of radio button I want to know which one was clicked
Whenever a radio button (In python Tkinter) is clicked its returning 0...
I tried the following method:
declaring the 'var' variable global
passing var variable in all function
But none of the steps are working
def get_date(var):
path_read = E1.get()
date_list = readunparseddata.getdate_unparseddate(path_read)
show_date(date_list,var)
def show_date(list_date,var):
print(var)
frame = Tk()
#v.set(1)
Label(frame,text="""Choose your Date :""",justify=LEFT,padx=20).pack( anchor = W )
count = 0
for date in list_date:
print count
R1=Radiobutton(frame, text=date, padx=20, value=count, variable=var, command=lambda:ShowChoice(var))
R1.pack()
count+=1
def ShowChoice(var):
print "option : " + str(var.get())
top = Tk()
var=IntVar()
The problem was with the instance of Tk() that I was creating.
Below link ( 1 ) said to use TopLevel() which solved the problem
Increment the counter in the function that is being called when the radio button is selected.
Here is an example to help you.It prints the number of times the button is selected.
import Tkinter as tk
count=0
root = tk.Tk()
def add():
global count
count=count+1
print count
v = tk.IntVar()
tk.Label(root,
text="""Choose a
programming language:""",
justify = tk.LEFT,
padx = 20).pack()
tk.Radiobutton(root,
text="Python",
padx = 20,
variable=v,
value=1,command=add).pack(anchor=tk.W)
root.mainloop()
I have a list of strings sorted in a tuple like this:
values = ('1.Python','2.Ruby','3.PHP','4.Perl','5.JavaScript')
My simple code is:
from tkinter import *
root = Tk()
values = ('1.Python','2.Ruby','3.PHP','4.Perl','5.JavaScript')
ru = Button(root,
text="Next",
)
ru.grid(column=0,row=0)
lab = Label(root,
text=values[0])
lab.grid(column=1,row=0)
ru2 = Button(root,
text="Previous"
)
ru2.grid(column=2,row=0)
root.mainloop()
I have two tkinter buttons "next" and "previous", the text value of the Label is directly taken from the tuple (text=value[0]), however I would want to know how to show the next string from the tuple when the next button is pressed, and how to change it to the previous values when the "previous" button is pressed. I know it can be done using for-loop but I cannot figure out how to implement that. I am new to python.
Use Button(..., command=callback) to assign function which will change text in label lab["text"] = "new text"
callback means function name without ()
You will have to use global inside function to inform function to assign current += 1 to external variable, not search local one.
import tkinter as tk
# --- functions ---
def set_next():
global current
if current < len(values)-1:
current += 1
lab["text"] = values[current]
def set_prev():
global current
if current > 0:
current -= 1
lab["text"] = values[current]
# --- main ---
values = ('1.Python','2.Ruby','3.PHP','4.Perl','5.JavaScript')
current = 0
root = tk.Tk()
ru = tk.Button(root, text="Next", command=set_next)
ru.grid(column=0, row=0)
lab = tk.Label(root, text=values[current])
lab.grid(column=1, row=0)
ru2 = tk.Button(root, text="Previous", command=set_prev)
ru2.grid(column=2, row=0)
root.mainloop()
BTW: if Next has to show first element after last one
def set_next():
global current
current = (current + 1) % len(values)
lab["text"] = values[current]
def set_prev():
global current
current = (current - 1) % len(values)
lab["text"] = values[current]
On the Forum there has recently been posted a Question by #Clueless_captain; it was answered by #furas. I am new to stackoverflow so I can not comment in that Conversation. This is the URL to that Conversation: (Tkinter Entry widget stays empty in larger programs (Python 2)). The Code posted by furas is not exactly what I try to do, but the only Thing I can not do myself is finding a way to re-use the Input that has been given in the EntryWidget. I modified the Code written by furas; my Goal was that the Input would be printed before the GUI terminated. For that I bound the Return key to a new Function, this Function was supposed to get the Textstring in this new Function where it would further be processed. It does only do that when I click the Button to get a name for a second time. Is the order of this Code off? I believe the Issue is closely related to the String 'e.bind' on line ten, but I can not find the Issue.
Best Regards, G_Lehmann
---------- The modified code:
from Tkinter import *
def get_input(text, variable):
win = Toplevel()
win.title("Get value")
f = LabelFrame(win, text=text)
f.pack()
e = Entry(win, textvariable=variable)
e.bind("<Return>", do_more(text, variable, e))
e.pack(side="right")
#I tried e.bind here, but This had no Effect.
b = Button(win, text = "Cancel", command=win.destroy)
b.pack()
#do_more is the new Function I want to use to process the Variable 'data' further.
def do_more(text, variable, e):
data = e.get()
print data
print len(data)
print type(data)
def get_value(text, variable):
get_input(text, variable)
# --- main --
root = Tk()
root.title("Ask-name-SUB")
# global variables
var_name = StringVar()
var_address = StringVar()
b = Button(root, text="Enter your name", command=lambda: get_value("Your name:", var_name))
b.pack()
b = Button(root, text="Enter your address", command=lambda: get_value("Your address:", var_address))
b.pack()
b = Button(root, text="Cancel", command=root.destroy)
b.pack()
root.mainloop()
# --- after -- (My Edit: I disabled this as I want to bind the Variables before my GUI gets terminated)
"""
name = var_name.get()
print "Print name, its length, its type"
print name, len(name), type(name)
address = var_address.get()
print "Print address, its length, its type"
print address, len(address), type(address)
"""
bind expects function name (like in command=) so you have to use lambda to assign function with arguments. Besides bind execute function with argument event so you have to receive it.
e.bind("<Return>", lambda event:do_more(variable))
You can assign do_more to button too
b = Button(win, text="OK", command=lambda:do_more(variable))
and do the same with Return and with Button - and close window after do something with variable.
You can also do the same in get_value after you close window but you have to use win.wait_window() because normally tkinter create window and doesn't wait till you close it.
So now you have two possibility to do something with value - in do_more or in get_value - choose one. Both method can be modify - ie. you can use class and create own dialog window.
from Tkinter import *
# --- ---
# global variable to use in different functions
win = None
def get_input(text, variable):
global win # inform function to use global variable `win` to assign value
win = Toplevel()
win.title("Get value")
f = LabelFrame(win, text=text)
f.pack()
# use `f` instead of `win` to put inside LabelFrame
e = Entry(f, textvariable=variable)
e.pack()#side="right")
e.bind("<Return>", lambda event:do_more(variable))
b = Button(win, text="OK", command=lambda:do_more(variable))
b.pack()
def do_more(variable):
data = variable.get()
print 'do_more:', data, len(data), type(data)
win.destroy()
# --- ---
def get_value(text, variable):
# create window
get_input(text, variable)
# this code is executed directly after creating window, not after closing window
# so code has to wait till window is closed
win.wait_window()
data = variable.get()
print 'get_value:', data, len(data), type(data)
# --- main --
root = Tk()
root.title("Ask-name-SUB")
# global variables
var_name = StringVar()
var_address = StringVar()
b = Button(root, text="Enter your name", command=lambda:get_value("Your name:", var_name))
b.pack()
b = Button(root, text="Enter your address", command=lambda:get_value("Your address:", var_address))
b.pack()
b = Button(root, text="Cancel", command=root.destroy)
b.pack()
root.mainloop()
My little brother is just getting into programming, and for his Science Fair project, he's doing a simulation of a flock of birds in the sky. He's gotten most of his code written, and it works nicely, but the birds need to move every moment.
Tkinter, however, hogs the time for its own event loop, and so his code won't run. Doing root.mainloop() runs, runs, and keeps running, and the only thing it runs is the event handlers.
Is there a way to have his code run alongside the mainloop (without multithreading, it's confusing and this should be kept simple), and if so, what is it?
Right now, he came up with an ugly hack, tying his move() function to <b1-motion>, so that as long as he holds the button down and wiggles the mouse, it works. But there's got to be a better way.
Use the after method on the Tk object:
from tkinter import *
root = Tk()
def task():
print("hello")
root.after(2000, task) # reschedule event in 2 seconds
root.after(2000, task)
root.mainloop()
Here's the declaration and documentation for the after method:
def after(self, ms, func=None, *args):
"""Call function once after given time.
MS specifies the time in milliseconds. FUNC gives the
function which shall be called. Additional parameters
are given as parameters to the function call. Return
identifier to cancel scheduling with after_cancel."""
The solution posted by Bjorn results in a "RuntimeError: Calling Tcl from different appartment" message on my computer (RedHat Enterprise 5, python 2.6.1). Bjorn might not have gotten this message, since, according to one place I checked, mishandling threading with Tkinter is unpredictable and platform-dependent.
The problem seems to be that app.start() counts as a reference to Tk, since app contains Tk elements. I fixed this by replacing app.start() with a self.start() inside __init__. I also made it so that all Tk references are either inside the function that calls mainloop() or are inside functions that are called by the function that calls mainloop() (this is apparently critical to avoid the "different apartment" error).
Finally, I added a protocol handler with a callback, since without this the program exits with an error when the Tk window is closed by the user.
The revised code is as follows:
# Run tkinter code in another thread
import tkinter as tk
import threading
class App(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.start()
def callback(self):
self.root.quit()
def run(self):
self.root = tk.Tk()
self.root.protocol("WM_DELETE_WINDOW", self.callback)
label = tk.Label(self.root, text="Hello World")
label.pack()
self.root.mainloop()
app = App()
print('Now we can continue running code while mainloop runs!')
for i in range(100000):
print(i)
When writing your own loop, as in the simulation (I assume), you need to call the update function which does what the mainloop does: updates the window with your changes, but you do it in your loop.
def task():
# do something
root.update()
while 1:
task()
Another option is to let tkinter execute on a separate thread. One way of doing it is like this:
import Tkinter
import threading
class MyTkApp(threading.Thread):
def __init__(self):
self.root=Tkinter.Tk()
self.s = Tkinter.StringVar()
self.s.set('Foo')
l = Tkinter.Label(self.root,textvariable=self.s)
l.pack()
threading.Thread.__init__(self)
def run(self):
self.root.mainloop()
app = MyTkApp()
app.start()
# Now the app should be running and the value shown on the label
# can be changed by changing the member variable s.
# Like this:
# app.s.set('Bar')
Be careful though, multithreaded programming is hard and it is really easy to shoot your self in the foot. For example you have to be careful when you change member variables of the sample class above so you don't interrupt with the event loop of Tkinter.
This is the first working version of what will be a GPS reader and data presenter. tkinter is a very fragile thing with way too few error messages. It does not put stuff up and does not tell why much of the time. Very difficult coming from a good WYSIWYG form developer. Anyway, this runs a small routine 10 times a second and presents the information on a form. Took a while to make it happen. When I tried a timer value of 0, the form never came up. My head now hurts! 10 or more times per second is good enough for me. I hope it helps someone else. Mike Morrow
import tkinter as tk
import time
def GetDateTime():
# Get current date and time in ISO8601
# https://en.wikipedia.org/wiki/ISO_8601
# https://xkcd.com/1179/
return (time.strftime("%Y%m%d", time.gmtime()),
time.strftime("%H%M%S", time.gmtime()),
time.strftime("%Y%m%d", time.localtime()),
time.strftime("%H%M%S", time.localtime()))
class Application(tk.Frame):
def __init__(self, master):
fontsize = 12
textwidth = 9
tk.Frame.__init__(self, master)
self.pack()
tk.Label(self, font=('Helvetica', fontsize), bg = '#be004e', fg = 'white', width = textwidth,
text='Local Time').grid(row=0, column=0)
self.LocalDate = tk.StringVar()
self.LocalDate.set('waiting...')
tk.Label(self, font=('Helvetica', fontsize), bg = '#be004e', fg = 'white', width = textwidth,
textvariable=self.LocalDate).grid(row=0, column=1)
tk.Label(self, font=('Helvetica', fontsize), bg = '#be004e', fg = 'white', width = textwidth,
text='Local Date').grid(row=1, column=0)
self.LocalTime = tk.StringVar()
self.LocalTime.set('waiting...')
tk.Label(self, font=('Helvetica', fontsize), bg = '#be004e', fg = 'white', width = textwidth,
textvariable=self.LocalTime).grid(row=1, column=1)
tk.Label(self, font=('Helvetica', fontsize), bg = '#40CCC0', fg = 'white', width = textwidth,
text='GMT Time').grid(row=2, column=0)
self.nowGdate = tk.StringVar()
self.nowGdate.set('waiting...')
tk.Label(self, font=('Helvetica', fontsize), bg = '#40CCC0', fg = 'white', width = textwidth,
textvariable=self.nowGdate).grid(row=2, column=1)
tk.Label(self, font=('Helvetica', fontsize), bg = '#40CCC0', fg = 'white', width = textwidth,
text='GMT Date').grid(row=3, column=0)
self.nowGtime = tk.StringVar()
self.nowGtime.set('waiting...')
tk.Label(self, font=('Helvetica', fontsize), bg = '#40CCC0', fg = 'white', width = textwidth,
textvariable=self.nowGtime).grid(row=3, column=1)
tk.Button(self, text='Exit', width = 10, bg = '#FF8080', command=root.destroy).grid(row=4, columnspan=2)
self.gettime()
pass
def gettime(self):
gdt, gtm, ldt, ltm = GetDateTime()
gdt = gdt[0:4] + '/' + gdt[4:6] + '/' + gdt[6:8]
gtm = gtm[0:2] + ':' + gtm[2:4] + ':' + gtm[4:6] + ' Z'
ldt = ldt[0:4] + '/' + ldt[4:6] + '/' + ldt[6:8]
ltm = ltm[0:2] + ':' + ltm[2:4] + ':' + ltm[4:6]
self.nowGtime.set(gdt)
self.nowGdate.set(gtm)
self.LocalTime.set(ldt)
self.LocalDate.set(ltm)
self.after(100, self.gettime)
#print (ltm) # Prove it is running this and the external code, too.
pass
root = tk.Tk()
root.wm_title('Temp Converter')
app = Application(master=root)
w = 200 # width for the Tk root
h = 125 # height for the Tk root
# get display screen width and height
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen
# calculate x and y coordinates for positioning the Tk root window
#centered
#x = (ws/2) - (w/2)
#y = (hs/2) - (h/2)
#right bottom corner (misfires in Win10 putting it too low. OK in Ubuntu)
x = ws - w
y = hs - h - 35 # -35 fixes it, more or less, for Win10
#set the dimensions of the screen and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.mainloop()