Search code examples
pythontkintertkinter-entrytkinter-buttontkinter-label

My Python Tkinter label doesn't update when I want it to


In my Computer Science class we were asked to make a UI that solves some problems. All of the buttons work, the math operations return the right answer, but when I try to make it pop up on the screen, It doesn't work.

Here's My Code (sorry some parts are in french):

from tkinter import *
from math import sqrt
root = Tk()
root.title("CALCULS")
root.geometry("400x300")

vala = "unset"
valb = "unset"
svala = 0
svalb = 0
valtot = "Pas de valeurs"

def updateresult():
    printedvaltot.place(x = 60, y = 250, width = 120, height = 20)

def calculer1():
    vala = float(entryvala.get())
    valb = float(entryvalb.get())
    svala = vala**2
    svalb = valb**2
    valtot = svala - svalb
    updateresult()

def calculer2():
    vala = float(entryvala.get())
    valb = float(entryvalb.get())
    valtot = vala + valb
    valtot = valtot**2
    updateresult()

def calculer3():
    vala = float(entryvala.get())
    valb = float(entryvalb.get())
    valtot = vala - valb
    valtot = valtot**2
    updateresult()

def calculer4():
    vala = float(entryvala.get())
    valb = float(entryvalb.get())
    valtot = vala*valb
    valtot = sqrt(valtot)
    updateresult()
    
#TEXTE
printed1 = Label(root, text = "Choisissez une valeur pour a et b, et une opération", foreground = "#297294")
printed1.place(x = 60, y = 20)

printedvala = Label(root, text = "Valeur de a", background = "#297294")
printedvala.place(x = 60, y = 50, width = 120, height = 20) 

printedvalb = Label(root, text = "Valeur de b", background = "#297294")
printedvalb.place(x = 60, y = 80, width = 120, height = 20)

printedvaltot = Label(root, text = str(valtot))
printedvaltot.place(x = 200, y = 250, width = 120, height = 20)

printedresultat = Label(root, text = "RESULTAT", background = "#297294")
printedresultat.place(x = 60, y = 250, width = 120, height = 20)

#INPUT

entryvala = Entry(root)
entryvala.place(x = 200, y = 50, width = 120, height = 20)

entryvalb = Entry(root)
entryvalb.place(x = 200, y = 80, width = 120, height = 20)

#BOUTONS

b1 = Button(root, command = calculer1, text = "a²-b²")
b1.place(x = 60, y = 110, width = 120, height = 20)

b2 = Button(root, command = calculer2, text = "(a+b)²")
b2.place(x = 200, y = 110, width = 120, height = 20)

b3 = Button(root, command = calculer3, text = "(a-b)²")
b3.place(x = 60, y = 140, width = 120, height = 20)

b4 = Button(root, command = calculer4, text = "√(a*b)")
b4.place(x = 200, y = 140, width = 120, height = 20)


All of it works, except the updating label part. HELP!!

I tried remaking it from scratch, didn't work. I tried using the strvar method, didn't work. I tried using textvariable for this specific label, still didn't work.


Solution

  • Note that valtot inside those calculateX() is a local variable, not the global one. So updating it does not update the global one.

    You can pass the result of those calculations to updateresult() and update the required label using that result.

    Below is the required changes:

    def updateresult(result):  # added argument
        printedvaltot.config(text=result)  # update label with passed value
    
    def calculer1():
        ...
        updateresult(valtot)  # pass result to update function
    
    def calculer2():
        ...
        updateresult(valtot)
    
    def calculer3():
        ...
        updateresult(valtot)
    
    def calculer4():
        ...
        updateresult(valtot)