简体   繁体   English

如何在 tkinter 中输入 int

[英]How to make an entry int in tkinter

i want to create a area of circle calculator because i have some free time so i tried to run it first in to the terminal and it works but when i decide to add a litte GUI well i got an error My code is this我想创建一个圆形计算器区域,因为我有一些空闲时间所以我尝试先在终端中运行它并且它可以工作但是当我决定添加一个小 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()

and this is the error that i got这是我得到的错误

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>

i try to add int() in to the entry but it dosent work我尝试将 int() 添加到条目中,但它不起作用

You need to call radius.get() to get the Entry widget's current value.您需要调用radius.get()来获取Entry小部件的当前值。 Here's some documentation on it.这是有关它的一些文档

There were some other errors and a typo in your code, so I fixed them and made it more PEP 8 - Style Guide for Python Code compliant.您的代码中还有一些其他错误和拼写错误,因此我修复了它们并使其更符合PEP 8 - Python 代码的样式指南

Here's the result:结果如下:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM