![](/img/trans.png)
[英]How to get a user input in entry widget without button in tkinter python?
[英]Python runs an int(x.get())/float(x.get()) command before the user can input anything into an entry [Python 3.6, Tkinter]
标题几乎说明了一切,为学校做一个项目,有一个允许用户输入两个值的输入字段,但它似乎在输入任何内容之前运行了 float 命令并给出了错误。 我尝试使用 int() 代替,并给出了一个基数为 10 的错误。 我什至尝试将数学部分移动到另一个函数,认为它在创建窗口时试图将其转换为 int。 完整的错误代码:
File "main.py", line 111, in <module>
app = Application(root)
File "main.py", line 9, in __init__
self.height_and_weight()
File "main.py", line 29, in height_and_weight
weight = float(weightEntry.get())
ValueError: could not convert string to float:
我的代码:
from tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.height_and_weight()
def clear_window(self, option):
for widget in self.winfo_children():
if option == 1:
widget.grid_forget()
else:
widget.destroy()
def height_and_weight(self):
Label(self, text = "Enter your height in inches").grid(column = 0, sticky = W)
heightEntry = Entry(self)
heightEntry.grid(column = 0, sticky = W)
Label(self, text = "Enter your weight in pounds").grid(column = 0, sticky = W)
weightEntry = Entry(self)
weightEntry.grid(column = 0, sticky = W)
weight = float(weightEntry.get())
height = float(heightEntry.get())
weight *= .45
height *= .025
height **= 2
self.BMI = 10
self.BMI = weight / height
Button(self, text = "Continue", command = self.health_assessment).grid(column = 0, sticky = W)
def health_assessment(self):
self.clear_window(1)
if self.BMI < 18.5: # Underweight
Label(self, text = "You are underweight").grid(column = 0, sticky = W)
Label(self, text = "It is recommended that you gain some healthy weight.").grid(column = 0, sticky = W)
Label(self, text = "Would you like information on:").grid(column = 0, sticky = W)
choice = IntVar()
Radiobutton(self, text = "Building muscle mass", variable = choice, value = 1).grid(column = 0, sticky = W)
Radiobutton(self, text = "Increasing good calories in your diet", variable = choice, value = 2).grid(column = 0, sticky = W)
Radiobutton(self, text = "No thanks", variable = choice, value = 3).grid(column = 0, sticky = W)
if choice == 1:
link = "http://virtualfitnesstrainer.com/muscle-building/bodybuilding/how-to-gain-weight-and-muscle-%E2%80%93-even-if-you-are-under-weight/"
elif choice == 2:
link = "https://www.everydayhealth.com/weight/how-to-gain-healthy-weight.aspx"
else:
link = ""
Label(self, text = link).grid(column = 0, sticky = W)
Button(self, text = "EXIT").grid(column = 0, sticky = W)
elif self.BMI >= 18.5 and self.BMI < 25: # Normal weight
Label(self, text = "You are a normal weight.").grid(column = 0, sticky = W)
Label(self, text = "Great job staying healthy!").grid(column = 0, sticky = W)
Button(self, text = "EXIT", command = root.destroy()).grid(column = 0, sticky = W)
elif self.BMI >= 25 and self.BMI > 30: # Overweight
Label(self, text = "You are overweight.").grid(column = 0, sticky = W)
Label(self, text = "It is recommended that you shed a few pounds.").grid(column = 0, sticky = W)
Label(self, text = "Would you like information on:").grid(column = 0, sticky = W)
link = ""
if option:
link = "https://www.runtastic.com/blog/en/burn-more-calories-during-workout/"
else:
link = "https://www.healthline.com/nutrition/35-ways-to-cut-calories"
Label(self, text = link).grid(column = 0, sticky = W)
Button(self, text = "EXIT",command = root.destroy()).grid(column = 0, sticky = W)
else: # Obese
Label(self, text = "You are obese.").grid(column = 0, sticky = W)
Label(self, text = "You are at an unhealthy weight that increases your chances of health problems.").grid(column = 0, sticky = W)
Label(self, text = "Please select one of the following:").grid(column = 0, sticky = W)
link = ""
if option:
link = "https://www.runtastic.com/blog/en/burn-more-calories-during-workout/"
else:
link = "https://www.healthline.com/nutrition/35-ways-to-cut-calories"
Label(self, text = link).grid(column = 0, sticky = W)
if option or not option:
Button(self, text = "EXIT",command = root.destroy()).grid(column = 0, sticky = W)
root = Tk()
root.title("Health Assessment Program")
w = 500
h = 500
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
app = Application(root)
root.mainloop()
您正在Application.__init__
调用self.height_and_weight()
(然后执行weight = float(weightEntry.get())
),因此在此处执行:
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
app = Application(root) # RIGHT HERE (and the error message tells you that)
root.mainloop()
所以这是在任何 Tkinter 代码之前运行的。 您应该有一个在按下时运行您的功能的按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.