Python Forum
[Tkinter] can't update label to include variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] can't update label to include variable
#1
im really new to python,this was made using extensive youtube tutorials
im trying to make a GUI app for a game i play:don't starve together
the app should take the ammount of mushrooms(2 red raw,3 green cooked etc) and then say what effects on my final stats they will have(health sanity hunger)
the calculations work(you must manualy enter a 0 in all fields to not get errors but thats not a issue)

my problem is that i cant get my app to display the results,i use a label and tried many online tutorials but alas none worked
im certain the variables get calculated as adding in print(hp) prints the correct result to the console


how can i make my app actually show the new text in place of the 0 i get by default
here is the FULL code(pretty big although i tried to make it readable and well layed out)
from tkinter import*
from math import *
from time import sleep

#Variables

redraw = 0
redrawhp = (-20)
redrawhunger = (+12.5) 
redrawsanity = (0)

redcooked = 0
redcookedhp = (1)
redcookedhunger = (0)
redcookedsanity = (-10)

greenraw = 0
greenrawhp = (0)
greenrawhunger = (12.5)
greenrawsanity = (-50)

greencooked = 0
greencookedhp = (-1)
greencookedhunger = (0)
greencookedsanity = (15)

blueraw = 0
bluerawhp = (20)
bluerawhunger = (12.5)
bluerawsanity = (-15)

bluecooked = 0
bluecookedhp = (-3)
bluecookedhunger = (0)
bluecookedsanity = (10)


hp=0
sanity=0
hunger=0







#Functions

def calculate():
    redraw = entryrawred.get()
    greenraw = entryrawgreen.get()
    blueraw = entryrawblue.get()
    redcooked = entrycookedred.get()
    greencooked = entrycookedgreen.get()
    bluecooked = entrycookedblue.get()
    hp = ((redrawhp)*(int(redraw))+(redcookedhp)*(int(redcooked))+(bluerawhp)*(int(blueraw))+(bluecookedhp)*(int(bluecooked)))
    hunger = ((redrawhunger)*(int(redraw))+(redcookedhunger)*(int(redcooked))+(bluerawhunger)*(int(blueraw))+(bluecookedhunger)*(int(bluecooked)))
    sanity = ((redrawsanity)*(int(redraw))+(redcookedsanity)*(int(redcooked))+(bluerawsanity)*(int(blueraw))+(bluecookedsanity)*(int(bluecooked))) 
    hp2 = StringVar()
    sanity2 = StringVar()
    hunger2 = StringVar()
    hp2.set(str(hp))
    sanity2.set(str(sanity))
    hunger2.set(str(hunger))
    
    print(hp)



#gui begin

root = Tk()

hp2 = StringVar()
sanity2 = StringVar()
hunger2 = StringVar()
hp2.set(hp)
sanity2.set(str(sanity))
hunger2.set(str(hunger))




#First column labels,mushroom types

labelmushroomtype = Label(root, text="Mushroom Type") 
labelraw = Label(root, text="Raw Ammount")
labelcooked = Label(root, text="Cooked Ammount")




#second column labels,mushroom colors

labelred = Label(root, text="Red")
labelgreen = Label(root, text="Green")
labelblue = Label(root, text="Blue")



#finale display things

labelhp = Label(root, textvariable=hp2)
 


#Entries

entryrawred = Entry(root)
entrycookedred = Entry(root)

entryrawgreen = Entry(root)
entrycookedgreen = Entry(root)

entryrawblue = Entry(root)
entrycookedblue = Entry(root)


#Buttons

buttoncalculate = Button(root, text="Calculate", command=calculate)
 


#Grid

labelmushroomtype.grid(row=0, sticky=W)
labelred.grid(row=1, sticky=W)

labelgreen.grid(row=2, sticky=W)
labelblue.grid(row=3, sticky=W)
 
labelraw.grid(row=0, column=1)
entryrawred.grid(row=1, column=1)
entryrawgreen.grid(row=2, column=1)
entryrawblue.grid(row=3, column=1)

labelcooked.grid(row=0, column=2)
entrycookedred.grid(row=1, column=2)
entrycookedgreen.grid(row=2, column=2)
entrycookedblue.grid(row=3, column=2)

buttoncalculate.grid(row=4, columnspan=3)

labelhp.grid(row=5, column=1)
















root.mainloop()
PLEASE don't use too much terminology,im really new to python and programming itself
i can use integrers strings and floats and do maths in script,i have no knowledge what classes are or anythng that has the word "self" all i have used untill now are loops and basic math so be gentle please Shy
Reply
#2
Simply comment out the lines 60-62
#    hp2 = StringVar()
#    sanity2 = StringVar()
#    hunger2 = StringVar()
The StringVars are already defined in global namespace at line 75-77. You don't need to redefine them when calculate() is executed. Especially, the line 104 links the StringVar instance defined at line 75 to the Label instance labelhp. If you define a new hp2, it will not be linked to the label and the result won't show when you call hp2.set()
Reply
#3
Those StringVar hp2, sanity2 and hunger2 you updated in the calculate() are local variable of this function, not those defined at line 75~77. Comment out them(line 60~62) and everything will be fine.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Update label if there are no records in treeview TomasSanchexx 1 948 Aug-20-2023, 04:45 PM
Last Post: menator01
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 3,569 Aug-30-2022, 08:44 AM
Last Post: tompranks
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,235 Oct-15-2021, 08:01 AM
Last Post: drSlump
  update text variable on label with keypress knoxvilles_joker 3 4,947 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  [Tkinter] how to update label text from list Roshan 8 5,477 Apr-25-2020, 08:04 AM
Last Post: Roshan
  Printing Variable into the label ardvci 5 3,124 Mar-19-2020, 09:35 PM
Last Post: joe_momma
  [Tkinter] Update variable between class/frame edwin6938 6 4,582 Nov-22-2019, 08:13 AM
Last Post: edwin6938
  [Tkinter] Trying to display a variable in a label Jordansonatina 2 17,868 Oct-31-2019, 06:28 PM
Last Post: Jordansonatina
  Update a label text from subprocess jim53 3 4,362 Aug-19-2019, 08:21 PM
Last Post: Denni
  Unable to update or refresh label text in tkinter jenkins43 3 6,639 Jul-24-2019, 02:09 PM
Last Post: Friend

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020