Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从条目小部件tkinter和python获取输入_Python_User Interface_Tkinter - Fatal编程技术网

如何从条目小部件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()