我想在 Python 上做一些簡單的程式。該程式計算您的正常體重。你寫你的年齡和身高。問題是計算后我無法為“標簽”設定新的計算值我該怎么做?
這是程式代碼
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()
程式看起來

錯誤是
" 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"
我認為問題出在這里:
def clicked():
p = int(age.get())
b = int(height.get())
mas = int(50 0.75*(p - 150) (b - 20) / 4)
lbl3.setvar(mas)
uj5u.com熱心網友回復:
來自tkinter 檔案
Use the config() method to update multiple attrs subsequent to object creation
fred.config(fg="red", bg="blue")
改變它
lbl3.config(text=str(mas))
此外,您似乎應該替換 Возраст 和 Рост - 它會正確計算健康體重,但現在不會
uj5u.com熱心網友回復:
好的,所以這是一個非常簡單的問題來解決。在您將條目值轉換為 int 進行計算的情況下,回傳值也是一個 int 值。標簽文本不能為 int。只需在單擊的函式中遵循以下代碼:
def clicked():
p = int(age.get())
b = int(height.get())
mas = int(50 0.75*(p - 150) (b - 20) / 4)
lbl3.setvar(str(mas))
str() 使 mas 成為來自輸入的字串。
謝謝你!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/312906.html
上一篇:Blazor-Serversidenav/sidebarforIdentity/Account/Manage看起來像NavMenu.razor
下一篇:懸停效果的影像不會保持可見
