3

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

  • 2
    Try: `self.GUI.root.update_idletasks()` before `self.GUI.root.mainloop()` – Thingamabobs Dec 27 '21 at 09:37
  • 1
    I just tried, didn't work. I also tried to put root.update() and root.update() everywhere where it made sense to me but my Label keep being black until I resize the window while my Canvas and Listbox works just fine – Kerialstraz Dec 27 '21 at 10:17
  • @Thingamabobs this doesn't work for me as well – Dorian Turba Dec 27 '21 at 11:26
  • 1
    @Kerialstraz just delete `update()` in the `drawWindow` method. – Thingamabobs Dec 27 '21 at 12:57
  • [When should I use root.update() ?](https://stackoverflow.com/questions/66781579/when-should-i-use-root-update-in-tkinter-for-python/66781785#66781785) – Thingamabobs Dec 27 '21 at 13:03
  • 2
    Does this answer your question? [When should I use root.update() in tkInter for python](https://stackoverflow.com/questions/66781579/when-should-i-use-root-update-in-tkinter-for-python) – Dorian Turba Dec 27 '21 at 17:22

1 Answers1

2

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).

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
  • While this may solve the question, it is partly wrong. The statement *configure on label seems to be a bad thing :D* has nothing to do with this error and is simply false. – Thingamabobs Dec 27 '21 at 12:57
  • Fine, I'll remove that part, even if this is how I understand the issue. If you have a better explanation, feel free to comment or edit the answer. – Dorian Turba Dec 27 '21 at 13:47
  • 1
    the problem here seems to be the use of `update()` if I remove that line in the code it works fine. [When should I use root.update() ?](https://stackoverflow.com/questions/66781579/when-should-i-use-root-update-in-tkinter-for-python/66781785#66781785) Your workaround is just a way to reduce the use of `update()` and the timing will be changed. – Thingamabobs Dec 27 '21 at 15:02
  • Thanks, I flagged the question as duplicate and edit my answer to redirect to the good answer. – Dorian Turba Dec 27 '21 at 17:25