Danny Boots
Danny Boots

Reputation: 21

Tkinter Python updating a variable

I build this tamagochi-like think which I want to feed and play sports with. Feeding him should add + 1 weight and sporting should make him lose weight. Since I am dutch Feeding is translated as 'voeden' and sports is translated as 'sporten'. I can't update the variable in the GUI Tkinter screen when I run the programm. Anyone out there can help me?

from tkinter import *

root = Tk()
root.geometry("610x445")
photo = PhotoImage(file="hondje.png")

pet_tukker = {

'naam': 'Tukker',
'leeftijd': 2,
'gewicht': 11,
'honger': True,
'foto': photo,
'zinnen': ['Hey leuk dat je er bent', 'Ik vind jou leuk', 'Zullen we buiten spelen', 'Dag maatje!',
           'Ik hoop dat het goed gaat met je, met mij in ieder geval wel!', 'Hey knapperd'],
'etenzinnen': ['Dat was smullen!!', 'Ik vond het heerlijk!', 'Ik kan dit blijven eten!',
               'Heb je nog meer voor me?', 'Tsjoh dat was niet zo lekker hoor'],
'sportenzinnen': ['Ik hou van sporten!', 'Ik vond het heerlijk!', 'Ik kan wel blijven rennen!',
                  'Wat gingen we snel he!', 'Wanneer gaan we nog een keertje rennen?', 'Ik heb het idee dat ik steeds beter wordt!']

}

class Window(Frame):


def __init__(self, master=None):
    Frame.__init__(self, master)
    self.master = master
    self.init_window()


def init_window(self):

    self.master.title("Danny's dierentuin")
    self.pack(fill=BOTH, expand=1)

    quitButton = Button(self, text="Afsluiten", command=self.client_exit)
    voedenButton = Button(self, text="Voeden", command=self.voeden)
    sportenButton = Button(self, text="Sporten", command=self.sporten)

    quitButton.place(x=150, y=410)
    voedenButton.place(x=330, y=410)
    sportenButton.place(x=500, y=410)

    naamLabel = Label(root, text="Naam: " + str(pet_tukker['naam']))
    leeftijdLabel = Label(root, text="Leeftijd: " + str(pet_tukker['leeftijd']))
    gewichtLabel = Label(root, text="Gewicht: " + str(pet_tukker['gewicht']))
    tukkerLabel = Label(root, image=pet_tukker['foto'])
    pratenLabel = Label(root)
    pratenLabel = Entry(root)

    naamLabel.place(x=10, y=10)
    leeftijdLabel.place(x=10, y=35)
    gewichtLabel.place(x=10, y=60)
    pratenLabel.place(x=10, y=410)
    tukkerLabel.place(x=100, y=0)


def voeden(self):
    gewicht = pet_tukker.get('gewicht')
    nieuw_gewicht = gewicht + 1
    pet_tukker.update({'gewicht': nieuw_gewicht})

def sporten(self):
    gewicht = pet_tukker.get('gewicht')
    nieuw_gewicht = gewicht - 1
    pet_tukker.update({'gewicht': nieuw_gewicht})

def client_exit(self):
    exit()


app = Window(root)
root.mainloop()

Please if anyone could help me, I am pretty new to this. Especially to Tkinter.

Upvotes: 2

Views: 103

Answers (2)

ColdBrew
ColdBrew

Reputation: 105

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)

Upvotes: 1

Stef van der Zon
Stef van der Zon

Reputation: 629

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']))

Upvotes: 1

Related Questions