I wanna do some simple program on Python. The program calculate your normal weight. You write your age and height. The problem is after calculate I can't set up new calculated value for "Label" How can I do that?
here is program code
from tkinter import *
def clicked(): #button click
p = int(age.get()) #try ti get value of your age from Entry
b = int(height.get()) #try to get value of your height from Entry
mas = int(50 0.75*(p - 150) (b - 20) / 4) #it is calculation
lbl3.setvar(mas) # here is a problem (i think)
window = Tk()
window.title('Ваш здоровый вес')
window.geometry('200x200')
lblage = Label(window, text='Ваш возраст') # Label for Age
lblage.grid(column=0, row=1)
age = Entry(window, width=5) # Entry for Age
age.grid(column=1, row=1)
lblheight = Label(window, text='Ваш рост') #Label for height
lblheight.grid(column=0, row=2)
height = Entry(window, width=5) # Entry for height
height.grid(column=1, row=2)
button1 = Button(window, text='Получить значение', fg='green', command=clicked) #Button
button1.grid(column=0, row=5)
lbl2 = Label(window, text='Ваш здоровый вес')
lbl2.grid(column=0, row=6)
lbl3 = Label(window) #here have to be our calculation
lbl3.grid(column=1, row=6)
window.mainloop()
the program looks
error is
" Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Maxim03\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\Maxim03\PycharmProjects\practice1\weight.py", line 7, in clicked
lbl3.setvar(mas)
File "C:\Users\Maxim03\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 709, in setvar
self.tk.setvar(name, value)
TypeError: must be str, bytes or Tcl_Obj, not int"
I think the problem is here:
def clicked():
p = int(age.get())
b = int(height.get())
mas = int(50 0.75*(p - 150) (b - 20) / 4)
lbl3.setvar(mas)
CodePudding user response:
from tkinter docs
Use the config() method to update multiple attrs subsequent to object creation
fred.config(fg="red", bg="blue")
change it on
lbl3.config(text=str(mas))
Also it looks like you should replace Возраст and Рост - it will calculate healthy weight correctly, but now does not
CodePudding user response:
ok so this is a very simple problem to solve. Where you have converted entry values to int for calculation, the return value was also an int value. Label text can't be int. Just follow this code in your clicked function:
def clicked():
p = int(age.get())
b = int(height.get())
mas = int(50 0.75*(p - 150) (b - 20) / 4)
lbl3.setvar(str(mas)
The str() made the mas a string from input.
Thank You!