Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter Label doesn't work as I thought it does [duplicate]

Tags:

python

tkinter

Beginner programmer here currently trying to learn Tkinter for a school assignment. I have a GUI class that stores the Tkinter labels etc, the labels are innitiated like this:

# GUI for Player 1
self.player_1_name_field = Label(
    self.root,
    text="Player 1",
    font=GUI_Settings.player_information_font,
    anchor=W,
    background=GUI_Settings.playerfield_active_color
)

I then create a Game() object that looks like this:

class Game():
def __init__(self):
    self.GUI = GUI()
    self.GUI.initializeBoard()
    self.GUI.root.mainloop()

When I run the code, the labels do get created and are where they are supposed to be, but are completely black. Once I move or resize the window it instantly becomes how I want it to be, it just behaves weird when at the start of the code

The interesting thing is that I also have a Canvas and a List that work perfectly fine, only the Labels are not cooperative

If you need further info, just ask for it! Thank you!

Edit 1: I have a function called drawWindow() that redraws the chessboard when I re-configure the window. In the init of the GUI class I set self.root.bind("<Configure>", self.drawWindow). If I remove that line of code, the Labels work but the Canvas doesn't anymore. I'm so confused. For anyone wanting to take a look at my tiny code: https://codeshare.io/DZYzyZ

like image 746
Kerialstraz Avatar asked Jun 11 '26 19:06

Kerialstraz


1 Answers

See comment of Thingamabobs

The issue is self.root.update(). Remove this line and you'll be fine. When should I use root.update() in tkInter for python.

This works but you shouldn't do it

This is a tricky issue. Your problem come from the bind of the configure event. Bind to the root window, it is applied to all sub-widgets of the window, which cause the bug (I don't know why yet).

This will solve your issue (line 202):

self.chessboard.bind("<Configure>", self.drawWindow)

instead of:

self.root.bind("<Configure>", self.drawWindow)

Result without moving or resizing the window: enter image description here

I found the information here (french forum).

like image 162
Dorian Turba Avatar answered Jun 14 '26 10:06

Dorian Turba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!