2

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.

asked Oct 24, 2024 at 9:43
4
  • You need to update the label using printedvaltot.config(text=...) inside updateresult(). But currently you just call .place(...) so the label will not be updated. Also you need to pass the result to updateresult(). Commented Oct 24, 2024 at 10:00
  • So what you're saying is that I shoud basically put this printedvaltot.config(text= str(valtot)) in the updateresult function ? Because I tried it and it doesn't seem to work. Commented Oct 24, 2024 at 10:08
  • No, because valtot inside those calculateX() is a local variable, not the global one. I have said in my last comment: you need to pass the result to updateresult() Commented Oct 24, 2024 at 10:18
  • Oh sorry for the misunderstanding. I've added global valtot to the calculateX() functions, but it still isn't working :/ Commented Oct 24, 2024 at 10:25

1 Answer 1

3

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)
answered Oct 24, 2024 at 10:23
Sign up to request clarification or add additional context in comments.

2 Comments

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 ?
Yes you just need to update the text of the label.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.