Tkinter Python updating a variable Tkinter Python updating a variable tkinter tkinter

Tkinter Python updating a variable


The configure function in Tkinter lets you update labels and buttons.

You could add gewichtLabel.configure(text="Gewicht: " + str(pet_tukker['gewicht'])) at the bottom of the voeden function.

It won't recognize gewichtLabel since it's local. You can make it a class variable by changing gewichtLabel to self.gewichtLabel everywhere. It should work then.

def voeden(self):    gewicht = pet_tukker.get('gewicht')    nieuw_gewicht = gewicht + 1    pet_tukker.update({'gewicht': nieuw_gewicht})    self.gewichtLabel.configure(text="Gewicht: " + str(pet_tukker['gewicht']))


If I understand your code correctly, the pet's weight is displayed in the gewichtLabel. The text this label displays is set once you initiate the window but never changed.I think the code to change the display text of a tkinter label looks something like this:

gewichtLabel.config(text="Gewicht: " + str(pet_tukker['gewicht']))

Effbot has a lot of valuable information about tkinter: http://effbot.org/tkinterbook/label.htm




On a second glance, a few thoughts:

Window looks like a custom class you inherite from the tkinter class Frame. I hope you're aware that tkinter has it's own Window class that you're overwriting. To avoid this you could import tkinter like this: import tkinter as tk and then call the class like this: class Window(tk.Frame):this way theres the tkinter class tk.Window and your inherited class Window.

I'm a bit wary about this: pet_tukker.update({'gewicht': nieuw_gewicht})
This is probably intended to update the gewichtLabel but I've never used this method while working with tkinter and from what I gathered in the doc I suspect it doesn't work the way you intended. (But quite frankly, I might be wrong here)