Python Forum
Tkinter error for a scheduled event
Thread Rating:
  • 4 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter error for a scheduled event
#1
Scheduled a variable to be updated with the current time for display in a label.

...

def update_clock():
    tab1_labeltxt = StringVar(time.strftime('%H %M %S'))
    brass_io.after(1000, update_clock)

brass_io.after(1000, update_clock)
brass_io.mainloop()
Program works just fine without the above code, which produces this error:

Exception in Tkinter callback
Error:
Traceback (most recent call last): File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__ return self.func(*args) File "/usr/lib/python3.6/tkinter/__init__.py", line 749, in callit func(*args) File ".../BIBO_Project.py", line 100, in update_clock tab1_labeltxt = StringVar(time.strftime('%H %M %S')) File "/usr/lib/python3.6/tkinter/__init__.py", line 480, in __init__ Variable.__init__(self, master, value, name) File "/usr/lib/python3.6/tkinter/__init__.py", line 317, in __init__ self._root = master._root() AttributeError: 'str' object has no attribute '_root'
Not sure what to do. Above my pay-grade.

Did some testing though, and I can't find this exact problem elsewhere, but I did see something about it being a possible problem with class inheritance? I really don't know, very new to this.

Thanks in advance <3

[edit] currently reading this: http://effbot.org/tkinterbook/variable.htm which seems to be getting somewhere.
[edit 2] nope lol. still looking.
Reply
#2
A StringVar has to be displayed on something, a Label for example.
Reply
#3
The variable is being used by the label as the source for its' text.
Reply
#4
This one is not. A new one is being created in the function without being attached to a label or other widget.

def update_clock():
        tab1_labeltxt = StringVar(time.strftime('%H %M %S')) 
Reply
#5
Hi Ceegen

Here is a working solution:
import time
import tkinter as tk

APP_TITLE = "The Time"

APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 200
APP_HEIGHT = 100


class Application(object):

    def __init__(self, main_win):
        self.main_win = main_win
        
        self.time_var = tk.StringVar()
        self.build()
        
    def build(self):
        tk.Label(self.main_win, textvariable=self.time_var, width=10, fg='blue',
            font=('Helvetica', 30, 'bold')).pack(expand=True)
            
        self.update_time()
        
    def update_time(self):
        self.time_var.set(time.strftime('%H:%M:%S'))
        self.main_win.after(1000, self.update_time)
               
def main():
    main_win = tk.Tk()
    main_win.title(APP_TITLE)
    #main_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    main_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    
    app = Application(main_win)
    
    main_win.mainloop()
wuf Wink
Reply
#6
(Jan-14-2019, 06:42 AM)woooee Wrote: This one is not. A new one is being created in the function without being attached to a label or other widget.

def update_clock():
        tab1_labeltxt = StringVar(time.strftime('%H %M %S')) 

The error being creating a new object, rather than update the one already made, correct?

(Jan-14-2019, 07:59 AM)wuf Wrote: Hi Ceegen

Here is a working solution:

*snip*

wuf Wink

Thank you, this explains a lot. Appreciated, will try it out.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 3,710 Dec-16-2019, 04:47 AM
Last Post: woooee
  What is the purpose of "None" in event=None with tkinter ? alan9979 2 6,363 Jul-10-2019, 08:50 PM
Last Post: alan9979

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020