使用 2 个 int 变量制作 if 语句。 Python 3.7 Tkinter
Making an if statement with 2 int variables. Python 3.7 Tkinter
我正在尝试使用 Tkinter 的输入框中的 2 个数字创建一个 if 语句。当我比较这 2 个数字时,我收到一条错误消息,指出我正在比较的 2 个变量是函数。是否可以将这些函数变成变量?我试过 var = int(var)、IntVar 和 var = float(var),但都不起作用。
代码
from tkinter import *
root = Tk()
root.title("Who Is More Fat?")
root.geometry("800x600")
# LABELS
font1, font2, font3 = "arial 30 bold", "arial 18 bold", "arial 18"
Label(root, text="Welcome to the Who's More Fat Program", font=font1,
fg="steelblue").place(x=0, y=0)
Label(root, text="Enter first persons name: ", font=font2,
fg="black").place(x=10, y=200)
Label(root, text="Enter first persons weight: ", font=font2,
fg="black").place(x=10, y=240)
Label(root, text="Enter second persons name: ", font=font2,
fg="black").place(x=10, y=300)
Label(root, text="Enter second persons weight: ", font=font2,
fg="black").place(x=10, y=340)
# TEXT BOXES
entry_box1 = Entry(root, width=25, font=font3, bg="white")
entry_box1.place(x=400, y=200)
entry_box2 = Entry(root, width=25, font=font3, bg="white")
entry_box2.place(x=400, y=240)
entry_box3 = Entry(root, width=25, font=font3, bg="white")
entry_box3.place(x=400, y=300)
entry_box4 = Entry(root, width=25, font=font3, bg="white")
entry_box4.place(x=400, y=340)
entry_box2 = float(entry_box2)
entry_box4 = float(entry_box4)
def calc():
if float(entry_box2.get()) > float(entry_box4.get()):
answer.delete(0, END)
answer.insert(0, entry_box1.get() + " is the Fattest")
else:
answer.delete(0, END)
answer.insert(0, entry_box3.get() + " is the Fattest")
# CALCULATE BUTTON
Button(root, text="Calculate the Fattest!", font=font2, bg="white", command=calc).place(x=5, y=390)
Label(root, text="Answer:", font=font2).place(x=290, y=400)
answer = Entry(root, font=font3, width=25, bg="white")
answer.place(x=400, y=400)
回溯
Traceback (most recent call last):
File "C:\Users\Liam\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in
__call__
return self.func(*args)
File "C:/Users/Liam/PycharmProjects/untitled/window.py", line 39, in calc
if entry_box2.get > entry_box4.get:
TypeError: '>' not supported between instances of 'function' and 'function'
我添加了主循环,我删除了
entry_box2 = float(entry_box2)
entry_box4 = float(entry_box4)
有效:
from tkinter import *
root = Tk()
root.title("Who Is More Fat?")
root.geometry("800x600")
# LABELS
font1, font2, font3 = "arial 30 bold", "arial 18 bold", "arial 18"
Label(root, text="Welcome to the Who's More Fat Program", font=font1,
fg="steelblue").place(x=0, y=0)
Label(root, text="Enter first persons name: ", font=font2,
fg="black").place(x=10, y=200)
Label(root, text="Enter first persons weight: ", font=font2,
fg="black").place(x=10, y=240)
Label(root, text="Enter second persons name: ", font=font2,
fg="black").place(x=10, y=300)
Label(root, text="Enter second persons weight: ", font=font2,
fg="black").place(x=10, y=340)
# TEXT BOXES
entry_box1 = Entry(root, width=25, font=font3, bg="white")
entry_box1.place(x=400, y=200)
entry_box2 = Entry(root, width=25, font=font3, bg="white")
entry_box2.place(x=400, y=240)
entry_box3 = Entry(root, width=25, font=font3, bg="white")
entry_box3.place(x=400, y=300)
entry_box4 = Entry(root, width=25, font=font3, bg="white")
entry_box4.place(x=400, y=340)
def calc():
if float(entry_box2.get()) > float(entry_box4.get()):
answer.delete(0, END)
answer.insert(0, entry_box1.get() + " is the Fattest")
else:
answer.delete(0, END)
answer.insert(0, entry_box3.get() + " is the Fattest")
# CALCULATE BUTTON
Button(root, text="Calculate the Fattest!", font=font2, bg="white",
command=calc).place(x=5, y=390)
Label(root, text="Answer:", font=font2).place(x=290, y=400)
answer = Entry(root, font=font3, width=25, bg="white")
answer.place(x=400, y=400)
root.mainloop()
我正在尝试使用 Tkinter 的输入框中的 2 个数字创建一个 if 语句。当我比较这 2 个数字时,我收到一条错误消息,指出我正在比较的 2 个变量是函数。是否可以将这些函数变成变量?我试过 var = int(var)、IntVar 和 var = float(var),但都不起作用。
代码
from tkinter import *
root = Tk()
root.title("Who Is More Fat?")
root.geometry("800x600")
# LABELS
font1, font2, font3 = "arial 30 bold", "arial 18 bold", "arial 18"
Label(root, text="Welcome to the Who's More Fat Program", font=font1,
fg="steelblue").place(x=0, y=0)
Label(root, text="Enter first persons name: ", font=font2,
fg="black").place(x=10, y=200)
Label(root, text="Enter first persons weight: ", font=font2,
fg="black").place(x=10, y=240)
Label(root, text="Enter second persons name: ", font=font2,
fg="black").place(x=10, y=300)
Label(root, text="Enter second persons weight: ", font=font2,
fg="black").place(x=10, y=340)
# TEXT BOXES
entry_box1 = Entry(root, width=25, font=font3, bg="white")
entry_box1.place(x=400, y=200)
entry_box2 = Entry(root, width=25, font=font3, bg="white")
entry_box2.place(x=400, y=240)
entry_box3 = Entry(root, width=25, font=font3, bg="white")
entry_box3.place(x=400, y=300)
entry_box4 = Entry(root, width=25, font=font3, bg="white")
entry_box4.place(x=400, y=340)
entry_box2 = float(entry_box2)
entry_box4 = float(entry_box4)
def calc():
if float(entry_box2.get()) > float(entry_box4.get()):
answer.delete(0, END)
answer.insert(0, entry_box1.get() + " is the Fattest")
else:
answer.delete(0, END)
answer.insert(0, entry_box3.get() + " is the Fattest")
# CALCULATE BUTTON
Button(root, text="Calculate the Fattest!", font=font2, bg="white", command=calc).place(x=5, y=390)
Label(root, text="Answer:", font=font2).place(x=290, y=400)
answer = Entry(root, font=font3, width=25, bg="white")
answer.place(x=400, y=400)
回溯
Traceback (most recent call last):
File "C:\Users\Liam\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in
__call__
return self.func(*args)
File "C:/Users/Liam/PycharmProjects/untitled/window.py", line 39, in calc
if entry_box2.get > entry_box4.get:
TypeError: '>' not supported between instances of 'function' and 'function'
我添加了主循环,我删除了
entry_box2 = float(entry_box2)
entry_box4 = float(entry_box4)
有效:
from tkinter import *
root = Tk()
root.title("Who Is More Fat?")
root.geometry("800x600")
# LABELS
font1, font2, font3 = "arial 30 bold", "arial 18 bold", "arial 18"
Label(root, text="Welcome to the Who's More Fat Program", font=font1,
fg="steelblue").place(x=0, y=0)
Label(root, text="Enter first persons name: ", font=font2,
fg="black").place(x=10, y=200)
Label(root, text="Enter first persons weight: ", font=font2,
fg="black").place(x=10, y=240)
Label(root, text="Enter second persons name: ", font=font2,
fg="black").place(x=10, y=300)
Label(root, text="Enter second persons weight: ", font=font2,
fg="black").place(x=10, y=340)
# TEXT BOXES
entry_box1 = Entry(root, width=25, font=font3, bg="white")
entry_box1.place(x=400, y=200)
entry_box2 = Entry(root, width=25, font=font3, bg="white")
entry_box2.place(x=400, y=240)
entry_box3 = Entry(root, width=25, font=font3, bg="white")
entry_box3.place(x=400, y=300)
entry_box4 = Entry(root, width=25, font=font3, bg="white")
entry_box4.place(x=400, y=340)
def calc():
if float(entry_box2.get()) > float(entry_box4.get()):
answer.delete(0, END)
answer.insert(0, entry_box1.get() + " is the Fattest")
else:
answer.delete(0, END)
answer.insert(0, entry_box3.get() + " is the Fattest")
# CALCULATE BUTTON
Button(root, text="Calculate the Fattest!", font=font2, bg="white",
command=calc).place(x=5, y=390)
Label(root, text="Answer:", font=font2).place(x=290, y=400)
answer = Entry(root, font=font3, width=25, bg="white")
answer.place(x=400, y=400)
root.mainloop()