已经提出了类似的问题,但它们都没有解决我的脚本构造的特定方式:
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()
Run Code Online (Sandbox Code Playgroud)
当我点击提交按钮时,我得到"NameError:全局名称'run_val'未定义".我在这做错了什么.现在,print语句只是为了检查我的工作.稍后,我将在程序中使用该值.
您没有存储对Entry小部件的引用initUI.
def initUI(self):
# ...
self.run_val = Entry(self)
self.run_val["width"] = 5
self.run_val.place(x=80, y=40)
Run Code Online (Sandbox Code Playgroud)
然后,您可以检索self.run_val.get()没有任何问题的值.
| 归档时间: |
|
| 查看次数: |
1120 次 |
| 最近记录: |