Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

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 = "a2-b2")
b1.place(x = 60, y = 110, width = 120, height = 20)
b2 = Button(root, command = calculer2, text = "(a+b)2")
b2.place(x = 200, y = 110, width = 120, height = 20)
b3 = Button(root, command = calculer3, text = "(a-b)2")
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.

Answer*

Draft saved
Draft discarded
Cancel
2
  • Oh ok ! Thank you for taking the time to show me ! So if I understand correctly, all I had to do was change the text and not the label in it's entirety, correct ? Commented Oct 24, 2024 at 10:30
  • 1
    Yes you just need to update the text of the label. Commented Oct 24, 2024 at 10:43

lang-py

AltStyle によって変換されたページ (->オリジナル) /