Bottom Page

Thread Rating:
  • 4 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Tkinter error for a scheduled event
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)
Program works just fine without the above code, which produces this error:

Exception in Tkinter callback
Traceback (most recent call last): File "/usr/lib/python3.6/tkinter/", line 1705, in __call__ return self.func(*args) File "/usr/lib/python3.6/tkinter/", line 749, in callit func(*args) File ".../", line 100, in update_clock tab1_labeltxt = StringVar(time.strftime('%H %M %S')) File "/usr/lib/python3.6/tkinter/", line 480, in __init__ Variable.__init__(self, master, value, name) File "/usr/lib/python3.6/tkinter/", 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: which seems to be getting somewhere.
[edit 2] nope lol. still looking.
A StringVar has to be displayed on something, a Label for example.
Gribouillis likes this post
The variable is being used by the label as the source for its' text.
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')) 
Hi Ceegen

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

APP_TITLE = "The Time"

APP_XPOS = 100
APP_YPOS = 100

class Application(object):

    def __init__(self, main_win):
        self.main_win = main_win
        self.time_var = tk.StringVar()
    def build(self):
        tk.Label(self.main_win, textvariable=self.time_var, width=10, fg='blue',
            font=('Helvetica', 30, 'bold')).pack(expand=True)
    def update_time(self):
        self.main_win.after(1000, self.update_time)
def main():
    main_win = tk.Tk()
    #main_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    main_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    app = Application(main_win)
wuf Wink
(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:


wuf Wink

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [WxPython] How to return a variable from an event handler Shaba1 2 135 Jun-13-2019, 02:47 PM
Last Post: Shaba1
  [Tkinter] clear event before keybinding skyerosebud 3 109 May-29-2019, 10:34 AM
Last Post: Gribouillis
  [Tkinter] Error in Tkinter moste 6 217 Apr-11-2019, 09:37 AM
Last Post: moste
  tkinter label error nonzzo 1 193 Mar-31-2019, 01:29 PM
Last Post: Larz60+
  Tkinter Gui Menu Addition Error whisperquiet 2 315 Jan-04-2019, 09:29 PM
Last Post: whisperquiet
  Python Qt Asking for a table event Faisal 4 821 Jul-25-2018, 02:55 PM
Last Post: Faisal
  SetValue to get event fired ian 1 522 May-25-2018, 06:07 PM
Last Post: Larz60+
  Event for wx.grid ian 1 618 May-16-2018, 03:01 AM
Last Post: Larz60+
  Event fired multiple times ian 1 502 May-12-2018, 06:09 PM
Last Post: Larz60+
  Launch pdf and close on delete window event ashtona 6 1,105 Mar-22-2018, 03:04 PM
Last Post: ashtona

Forum Jump:

Users browsing this thread: 1 Guest(s)