【发布时间】:2016-05-13 21:12:04
【问题描述】:
我正在学习 Python 和 Tkinter 并遵循教程。我正在编写一个程序来完成我在 Powershell 等脚本语言中习惯做的事情,获取用户值,然后用它做一些事情,首先。
我找不到任何看起来像我的示例代码。所以我请求你的帮助。
基本上,这个程序创建了一个带有退出按钮、用户输入值的条目小部件和一个确定按钮的框架。然后,他按下 OK 按钮,该值应该会打印在 python 终端中。
我收到一个错误,但我不明白为什么。我正在学习一个教程,所以我按照他们展示的方式进行操作,通过定义一个类来创建框架,将按钮和文本框放在其中。
下面设置了从条目小部件获取用户输入文本的功能,我将在其中放置其他功能,假设我想为按钮添加功能以进行打印。如果之前设置了要打印的值,我设法使打印工作,但我无法从条目小部件中获取值。
from tkinter import *
class Window:
def __init__(self, master):
self.master = master
master.title("Get a value and print it")
self.info = Label(master, text="Please write something then click ENTER")
self.info.pack()
self.ebox= Entry()
self.ebox.pack()
self.viewnumber = Button(master, text="OK", activebackground="red", command=get_int)
self.viewnumber.pack()
self.quit = Button(master, text="Quit", activebackground="blue", command=quit)
self.quit.pack()
def get_int():
intnum = ebox.get
print(intnum)
def quit():
root.quit()
root = Tk()
root.geometry("400x400")
my_gui = Window(root)
root.mainloop()
我收到此错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python35-32\lib\tkint
er\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Users\USERNAME\Desktop\pyth222184.py", line 20, in get_int
intnum = ebox.get
NameError: name 'ebox' is not defined
感谢您的帮助。
【问题讨论】:
-
所以,它告诉你
ebox不存在。当我查看您的代码时,我必须同意:没有ebox。有一个self.ebox,但self.ebox与ebox不同。调试时的一个好的经验法则是假设错误是真实的。
标签: python-3.x tkinter