我想创建一个面积计算器,因为我有一些空闲时间,所以我尝试先在终端上运行它,并且它可以正常工作,但是当我决定添加一个精巧的GUI时,出现了一个错误 我的代码是这个
from tkinter import *
screen = Tk()
screen.title("Area of Circle Calculator")
screen.geometry("500x500")
def submit():
global done
pi = 3.14159265359
an = int(pi * radius*radius)
laod = Label(screen, text = "Hello" % (an))
load.grid()
ask = Label(screen, text = "What is the radius of the circle?")
ask.pack()
radius = Entry(screen)
radius.pack()
done = Button(screen, text="submit", command = submit)
done.pack()
screen.mainloop()
这是我得到的错误
C:\Users\Timothy\Desktop>python aoc.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Timothy\AppData\Local\Programs\Python\Python37-32\lib\tkinter\_
_init__.py", line 1705, in __call__
return self.func(*args)
File "aoc.py", line 8, in submit
an = int(pi * radius*radius)
TypeError: unsupported operand type(s) for *: 'float' and 'Entry'
C:\Users\Timothy\Desktop>
我尝试将int()添加到条目中,但这确实有用
答案 0 :(得分:1)
您需要调用radius.get()
来获取Entry
小部件的当前值。
这里是一些documentation。
您的代码中还有其他错误和错别字,因此我将其修复并使其更符合PEP 8 - Style Guide for Python Code。
结果如下:
from math import pi
from tkinter import *
screen = Tk()
screen.title("Area of Circle Calculator")
screen.geometry("500x500")
def submit():
r = float(radius.get())
an = pi * r**2
load = Label(screen, text="Hello %f" % (an))
load.pack()
ask = Label(screen, text="What is the radius of the circle?")
ask.pack()
radius = Entry(screen)
radius.pack()
done = Button(screen, text="submit", command=submit)
done.pack()
screen.mainloop()