Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python先运行int(x.get())/float(x.get())命令,然后用户才能在条目中输入任何内容[Python3.6,Tkinter]_Python_Python 3.x_Tkinter - Fatal编程技术网

Python先运行int(x.get())/float(x.get())命令,然后用户才能在条目中输入任何内容[Python3.6,Tkinter]

Python先运行int(x.get())/float(x.get())命令,然后用户才能在条目中输入任何内容[Python3.6,Tkinter],python,python-3.x,tkinter,Python,Python 3.x,Tkinter,标题几乎说明了一切,在为学校做一个项目时,有一个输入字段,允许用户输入两个值,但它似乎在用户输入任何内容并给出错误之前运行float命令。我尝试使用int()代替,并给出了一个以10为基数的错误。我甚至尝试将数学部分移动到另一个函数,认为它试图在创建窗口时将其转换为int。 完整错误代码: File "main.py", line 111, in <module> app = Application(root) File "main.py", line 9, in _

标题几乎说明了一切,在为学校做一个项目时,有一个输入字段,允许用户输入两个值,但它似乎在用户输入任何内容并给出错误之前运行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:
文件“main.py”,第111行,在
app=应用程序(根)
文件“main.py”,第9行,在_init中__
自身高度和体重()
文件“main.py”,第29行,高度和重量
权重=浮动(weightEntry.get())
ValueError:无法将字符串转换为浮点:
我的代码:

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()
从tkinter导入*
课程申请(框架):
定义初始(自我,主):
帧。\uuuu初始化(自,主)
self.grid()
自身高度和体重()
def清除_窗口(自,选项):
对于self.winfo_children()中的小部件:
如果选项==1:
widget.grid_忘记()
其他:
widget.destroy()
def高度和重量(自身):
标签(self,text=“以英寸为单位输入高度”).grid(列=0,粘滞=W)
尝试=进入(自我)
加高网格(列=0,粘性=W)
标签(self,text=“以磅为单位输入体重”).grid(列=0,粘性=W)
weightEntry=条目(自身)
网格(列=0,粘性=W)
权重=浮动(weightEntry.get())
高度=浮动(healthtry.get())
重量*=0.45
高度*=0.025
高度**=2
self.BMI=10
self.BMI=体重/身高
按钮(self,text=“Continue”,command=self.health\u assessment).grid(column=0,sticky=W)
def健康评估(自我):
自动清除窗口(1)
如果自身体重指数<18.5:#体重不足
标签(self,text=“您体重不足”).grid(column=0,sticky=W)
Label(self,text=“建议您增加一些健康体重。”).grid(column=0,sticky=W)
标签(self,text=“您想了解以下信息:”).grid(column=0,sticky=W)
choice=IntVar()
单选按钮(self,text=“Building muscle mass”,变量=choice,值=1)。网格(列=0,粘性=W)
单选按钮(self,text=“增加饮食中的热量”,变量=choice,值=2)。网格(列=0,粘性=W)
单选按钮(self,text=“No Thank”,变量=choice,值=3)。网格(列=0,粘性=W)
如果选项==1:
链接=”http://virtualfitnesstrainer.com/muscle-building/bodybuilding/how-to-gain-weight-and-muscle-%E2%80%93-即使你体重不足/
elif选项==2:
链接=”https://www.everydayhealth.com/weight/how-to-gain-healthy-weight.aspx"
其他:
link=“”
标签(self,text=link).grid(列=0,粘性=W)
按钮(self,text=“EXIT”).grid(列=0,粘性=W)
elif self.BMI>=18.5,self.BMI<25:#正常体重
标签(self,text=“你是正常体重。”).grid(column=0,sticky=W)
标签(self,text=“保持健康真棒!”).grid(column=0,sticky=W)
按钮(self,text=“EXIT”,command=root.destroy()).grid(column=0,sticky=W)
elif self.BMI>=25和self.BMI>30:#超重
标签(self,text=“你超重了。”).grid(column=0,sticky=W)
标签(self,text=“建议你减掉几磅。”).grid(column=0,sticky=W)
标签(self,text=“您想了解以下信息:”).grid(column=0,sticky=W)
link=“”
如果选择:
链接=”https://www.runtastic.com/blog/en/burn-more-calories-during-workout/"
其他:
链接=”https://www.healthline.com/nutrition/35-ways-to-cut-calories"
标签(self,text=link).grid(列=0,粘性=W)
按钮(self,text=“EXIT”,command=root.destroy()).grid(column=0,sticky=W)
其他:#肥胖
标签(self,text=“你肥胖了。”).grid(column=0,sticky=W)
标签(self,text=“你的体重不健康,这会增加你出现健康问题的几率。”).grid(column=0,sticky=W)
标签(self,text=“请选择以下选项之一:”).grid(列=0,粘性=W)
link=“”
如果选择:
链接=”https://www.runtastic.com/blog/en/burn-more-calories-during-workout/"
其他:
链接=”https://www.healthline.com/nutrition/35-ways-to-cut-calories"
标签(self,text=link).grid(列=0,粘性=W)
如果选择或不选择:
按钮(self,text=“EXIT”,command=root.destroy()).grid(column=0,sticky=W)
root=Tk()
根标题(“健康评估计划”)
w=500
h=500
ws=root.winfo_屏幕宽度()
hs=root.winfo_屏幕高度()
x=(w/2)-(w/2)
y=(hs/2)-(h/2)
几何体(“%dx%d+%d+%d%”(w,h,x,y))
app=应用程序(根)
root.mainloop()

您正在调用
应用程序中的
self.height\u和
(然后执行
weight=float(weightEntry.get())
。\uuuuu init\uuuuuu
,因此它在这里执行:

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代码之前运行的。您应该有一个按钮,按下时可以运行您的功能。

这是太多的代码。将代码复制到一个新文件中,并从
\uuuuu init\uuuu
函数中删除您正在调用的所有不相关位。该函数在您创建对象时运行,因此它将运行您的函数,该函数将尝试将(刚刚创建的,所以仍然为空)输入框转换为浮点。