python - 如何在 tkinter 中创建一个条目 int
问题描述
我想创建一个圆形计算器区域,因为我有一些空闲时间所以我尝试先在终端中运行它并且它可以工作但是当我决定添加一个小 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() 添加到条目中,但它不起作用
解决方案
您需要调用radius.get()
以获取Entry
小部件的当前值。这是有关它的一些文档。
您的代码中还有一些其他错误和拼写错误,因此我修复了它们并使其更符合PEP 8 - Python 代码样式指南。
结果如下:
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()
推荐阅读
- python - 查找时间范围内的最高和最低值
- javascript - 我如何使用 javascript 库从打印机扫描仪获取图像
- ms-access - 数据库中有空表有什么影响?
- python - Python循环通过api并将多个对象作为元组附加到列表中
- c++ - 两个代码;相同的逻辑和代码行,一个有效,另一个无效
- c# - 尝试从 cmd 编译时找不到元数据文件
- python - Python 在 json 文件中转储“\n”而不是换行符
- c++ - 调整大小完成后的 Qt 窗口调整大小事件
- python - 错误的解释器:使用 pip 安装 boto3 时权限被拒绝
- c - 根据条件执行或跳过 ld.so.preload 共享库代码