簡體   English   中英

Python 運行 int(x.get())/float(x.get()) 命令,然后用戶可以在條目中輸入任何內容 [Python 3.6, Tkinter]

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM