Home > Mobile >  Trying to observe two IntVar with tkinter and pick the lowest number
Trying to observe two IntVar with tkinter and pick the lowest number

Time:10-21

Hi everyone I'm currently working on an rpg dynamic character sheet in python and I'm mainly using the tkinter library but I have come to an obstacle preventing me from going on.

What I am trying to achieve is getting two numbers from spinboxes (called con and vol) that can vary from 1 to 10 and compare the two to display the lowest number on a label (terre) next to the spinboxes in real time. For instance, if con is equal to 2 and vol equal to 3, then terre should display 2, if con gets added 1, then terre would be equal to 3 since con = 3 and vol = 3 and so on and so on.

The problem I'm having is that terre doesn't seem to be connected to con and vol and is not updating properly, when I change the numbers in the spinbox of the two variable, terre stays the same (that is to say 0) and won't budge.

My script so far is the following (sorry if it looks bad I'm still very new to programing) :

def Terre_calcul(*args) :
    tr1 = int(con.get())
    tr2 = int(vol.get())
    if tr1 > tr2 :
        terreV.set(vol.get())
    else : 
        terreV.set(con.get())

#################################

#creation of the parent widget
win = Tk()
win.title("Fiche personnage")

#widgets dimensions
win.geometry("800x1029 0 0")
win.resizable(width= False, height= False)

##################################

#Constitution 
con = IntVar()
conE = Spinbox(win, from_= 1, to= 10, width= 1, textvariable= con)
conE.place(x= 180, y= 165)
con.set(conE.get())

#Volonté
vol = IntVar()
volE = Spinbox(win, from_= 1, to= 10, width= 1, textvariable= vol)
volE.place(x= 168, y= 196)
vol.set(volE.get())

#Score
terreV = IntVar()
terre = Label(win, textvariable= terreV)
terre.tkraise(aboveThis= None)
terre.place(x= 266, y= 212)

win.mainloop()

Ignore some of the french gibberish it's really unimportant. I'd be more than happy to answer any further question and I hope you can help me with this problem, thank you in advance :).

CodePudding user response:

You can assign Terre_calcul to command option of the two spinboxes:

...
conE = Spinbox(win, from_= 1, to= 10, width= 5, textvariable= con, command=Terre_calcul)
...
volE = Spinbox(win, from_= 1, to= 10, width= 5, textvariable= vol, command=Terre_calcul)
...
Terre_calcul() # update terreV initially
win.mainloop()

Then whenever the value of one of the spinboxes is changed, Terre_calcul() will be executed.

  • Related