如何从条目小部件tkinter和python获取输入
我搜索了如何获得输入,这应该是可行的,但它不。。。 我不明白为什么它不起作用。。。它开始运行并卡在主环路中。。。什么也看不出来如何从条目小部件tkinter和python获取输入,python,user-interface,tkinter,Python,User Interface,Tkinter,我搜索了如何获得输入,这应该是可行的,但它不。。。 我不明白为什么它不起作用。。。它开始运行并卡在主环路中。。。什么也看不出来 from Tkinter import * class GUI: def __init__(self): self.root = Tk() self.label1 = Label(self.root, text="name") self.label2 = Label(self.root, text="password") sel
from Tkinter import *
class GUI:
def __init__(self):
self.root = Tk()
self.label1 = Label(self.root, text="name")
self.label2 = Label(self.root, text="password")
self.entry1 = Entry(self.root)
self.entry2 = Entry(self.root, show="*")
self.button = Button(self.root, text="hello", command=self.printName)
self.button.pack()
self.label1.grid(row=0, sticky=W) # N, S, E, W
self.label2.grid(row=1, sticky=E)
self.entry1.grid(row=0, column=1)
self.entry2.grid(row=1, column=1)
self.c = Checkbutton(self.root, text="forgot my password")
self.c.grid(columnspan=2)
self.root.mainloop()
def printName(self):
print self.entry1.get()
hi = GUI()
问题在于,对于共享同一父部件的小部件,您同时使用了
grid
和pack
。你不能这么做,你得选一个
此外,要学究气,您应该在\uuuu init\uuuu
之外调用self.root.mainloop()
。原因是,当它在内部时,对象永远不会完全创建,因为mainloop
在小部件被销毁之前不会返回。通常在创建根窗口的同一范围内调用mainloop
例如:
hi = GUI()
hi.root.mainloop()
如果您不喜欢引用内部小部件,请为GUI
提供类似start
或mainloop
的方法:
class GUI():
...
def start(self):
self.root.mainloop()
...
hi = GUI()
hi.start()