[英]Get value from Entry of Tkinter
Similar questions have been asked, but none of them address the particular way my script is constructed: 有人提出了类似的问题,但没有一个解决我的脚本构造的特殊方式:
from Tkinter import *
from ttk import *
class Gui(Frame):
def __init__(self, parent):
Frame.__init__(self, parent) #Gui inherits from built in Frame Class
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Shoes Ware")
self.pack(fill=BOTH, expand=1)
run_val = Entry(self)
run_val["width"] = 5
run_val.place(x=80, y=40)
quit_B = Button(self, text="Submit", command=self.submit)
quit_B.place(x=130, y=170)
def submit(self):
value = run_val.get()
print value
self.quit()
def main():
root = Tk()
root.geometry("300x200+50+50")
app = Gui(root)
root.mainloop()
if __name__ == '__main__':
main()
I get "NameError: global name 'run_val' is not defined" when I hit the submit button. 单击提交按钮时,出现“ NameError:全局名称'run_val'未定义”。 What am I doing wrong here.
我在这里做错了。 Right now the print statement is just to check my work.
现在,打印声明只是为了检查我的工作。 Later on, I'll be using that value in a program.
稍后,我将在程序中使用该值。
You are not storing the reference to the Entry widget in initUI
. 您没有在
initUI
存储对Entry小部件的initUI
。
def initUI(self):
# ...
self.run_val = Entry(self)
self.run_val["width"] = 5
self.run_val.place(x=80, y=40)
Then you can retrieve the value of self.run_val.get()
without any problem. 然后,您可以
self.run_val.get()
检索self.run_val.get()
的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.