andyj280
Posts: 3
Joined: Fri Aug 16, 2019 3:39 am

Tkinter label widget not updating

Fri Aug 16, 2019 3:57 am

Hi everyone,

I have a simple list of users, where each user's name is on a label. I am using python's cec lib.

I am receiving the UP and DOWN messages from the remote control just fine, but the Tkinter labels are not changing.
Any help would be greatly appreciated! I am using python 3.7.0.

The problematic part is:

Code: Select all

user_list[usercounter].config(fg="black")
user_list[usercounter].update()
window.update()
#no label update!!
Here's the whole code. I have added "..." for irrelevant pieces of code / irrelevant widgets.

Code: Select all

import sys
import tkinter
import cec
from enum import Enum

# Define enums for keypresses
class Keypress(Enum):
    ENTER_KEY = 0
    UP_KEY = 1
    DOWN_KEY = 2

...

# User list
user1 = tkinter.Label(window, text = "Bob", fg = "white", bg = "red", font=("Arial", 25)).pack(fill = "both", expand = True)
user2 = tkinter.Label(window, text = "Alice", fg = "white", bg = "green", font=("Arial", 25)).pack(fill = "both", expand = True)
user3 = tkinter.Label(window, text = "Jane", fg = "white", bg = "blue", font=("Arial", 25)).pack(fill = "both", expand = True)
user4 = tkinter.Label(window, text = "Bill", fg = "white", bg = "orange", font=("Arial", 25)).pack(fill = "both", expand = True)
user_list = [user1, user2, user3, user4]

# Initialize CEC
cec.init()
usercounter = 0
print("Loaded CEC from", cec.__file__)
def keypress_cb(event, *args):
    global usercounter
    if(args[0] == Keypress.UP_KEY.value):
        print("UP button was pressed")
        print(usercounter)
        if(usercounter <= 3):
            usercounter += 1
            user_list[usercounter].config(fg="black")
            user_list[usercounter].update()
            window.update()
...
cec.add_callback(keypress_cb, cec.EVENT_KEYPRESS)

# Fullscreen bindings
def quit(event):
    sys.exit()
window.bind('<Key-Escape>', quit)

window.mainloop()

User avatar
neilgl
Posts: 640
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Tkinter label widget not updating

Fri Aug 16, 2019 12:55 pm

Not sure you need the update. Try this example of updating a list of labels (using a timer in this example) :

Code: Select all

# python3
import tkinter as tk

def Draw():
    global labels
    string = 'Some text '
    labtext = ['Bob', 'Carol', 'Ted', 'Alice']
    labels=[] #creates an empty list for your labels
    frame=tk.Frame(root,width=400,height=400,relief='solid',bd=1)
    frame.place(x=10,y=10)
    for x in labtext: #iterates over your label texts
        jk = string + x
        label = tk.Label(frame,text=jk, bg="green", fg="white")
        label.pack()
        labels.append(label) #appends the label to the list for further use

def Refresher():
    global labels
    global i
    labfg = ['black', 'red', 'purple', 'orange']
    labels[i].configure(fg=labfg[i])
    i=i+1
    if i>3:
      i=0
      labels[0].configure(fg="white")
      labels[1].configure(fg="white")
      labels[2].configure(fg="white")
      labels[3].configure(fg="white")
    root.after(2000, Refresher) # repeat regularly

i = 0
root=tk.Tk()
root.geometry('%dx%d+%d+%d' % (400, 300, 200, 100))
Draw()
Refresher()
root.mainloop()

andyj280
Posts: 3
Joined: Fri Aug 16, 2019 3:39 am

Re: Tkinter label widget not updating

Sun Aug 18, 2019 1:20 pm

Thank you so much! The 'Refresher' concept on a list of labels worked like an absolute charm.

User avatar
neilgl
Posts: 640
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Tkinter label widget not updating

Sun Aug 18, 2019 5:18 pm

Glad it helped you. You and you can add your up/down from the remote to it.

Return to “Beginners”