2012-04-07 87 views
0

这是一段代码,我写了这样一个问题:Entry text on a different window?Tkinter Button不会出现在TopLevel上?

这真是奇怪mySubmitButton发生了什么事,看来该按钮不希望出现当它第一次启动时,它会,但出现时你点击它。即使你点击它并从按钮上释放它,这样它就不会被发送。我怀疑,如果这只发生在Mac上,或者它只发生在我的电脑上,因为这是一个非常小的问题。或者我用我的代码做了些傻事。

self.mySubmitButton = tk.Button(top, text='Hello', command=self.send) 
self.mySubmitButton.pack() 

我是否错过了什么?我搜索了一下,发现这question and answer on daniweb。我在他们身上做了一个差异,不知道他做了什么“固定”,但我确实看到线路更改为command=root.quit。但它与我的不同...

这里是完整的源代码,并没有错误消息,但该按钮只是缺少。

import tkinter as tk 

class MyDialog: 
    def __init__(self, parent): 
     top = self.top = tk.Toplevel(parent) 
     self.myLabel = tk.Label(top, text='Enter your username below') 
     self.myLabel.pack() 

     self.myEntryBox = tk.Entry(top) 
     self.myEntryBox.pack() 

     self.mySubmitButton = tk.Button(top, text='Hello', command=self.send) 
     self.mySubmitButton.pack() 

    def send(self): 
     global username 
     username = self.myEntryBox.get() 
     self.top.destroy() 

def onClick(): 
    inputDialog = MyDialog(root) 
    root.wait_window(inputDialog.top) 
    print('Username: ', username) 

username = 'Empty' 
root = tk.Tk() 
mainLabel = tk.Label(root, text='Example for pop up input box') 
mainLabel.pack() 

mainButton = tk.Button(root, text='Click me', command=onClick) 
mainButton.pack() 

root.mainloop() 

enter image description here

enter image description here

  1. 这一项之后添加另一个按钮,第二个实际上出现。我认为这可能是因为我没有调用相同的功能,但我调用了相同的功能,它看起来和它完全相同......
  2. 在它们之间添加一个空标签不起作用。该按钮仍然没有被绘制。

enter image description here

PS:我使用的是Mac OS 10.5.8和TK 8.4.7。

+0

无论是IDE(Stani的SPE)还是双击开始,此工作原理在win7 64位activepython 2.6.7(在更改tkinter到Tkinter后)都是完美的。它可能是操作系统特定的 – joaquin 2012-04-07 06:56:34

+0

但是我写的其他程序的常规按钮很好,甚至当我创建第二个(具有相同的功能)时,它显示,(只是中间的一个(点击我))丢失)...这不是什么大不了的事情,但我只是好奇而好奇。或者,也许我的安装有一些问题。 – George 2012-04-07 07:02:06

+0

你的代码可以在python 2.7下正常工作。也许你有一个错误版本的python/tkinter? – 2012-04-07 11:30:29

回答

3

我看到你好按钮,但我在Windows 7

我做你的榜样的快速重新写。如果它对你有所帮助,我会很好奇。

import tkinter as tk 

class GUI(tk.Tk): 
    def __init__(self): 
     tk.Tk.__init__(self) 

     mainLabel = tk.Label(self, text='Example for pop up input box') 
     mainLabel.pack() 

     mainButton = tk.Button(self, text='Click me', command=self.on_click) 
     mainButton.pack() 

     top = self.top = tk.Toplevel(self) 
     myLabel = tk.Label(top, text='Enter your username below') 
     myLabel.pack() 

     self.myEntryBox = tk.Entry(top) 
     self.myEntryBox.pack() 

     mySubmitButton = tk.Button(top, text='Hello', command=self.send) 
     mySubmitButton.pack() 

     top.withdraw() 

    def send(self): 
     self.username = self.myEntryBox.get() 
     self.myEntryBox.delete(0, 'end') 
     self.top.withdraw() 
     print(self.username) 

    def on_click(self): 
     self.top.deiconify() 

gui = GUI() 
gui.mainloop() 
+0

你的版本像魅力一样工作,是我写MyDialog()类的方式吗?按钮出现时不需要我点击或在标签之间切换。 – George 2012-04-07 07:12:52

+0

我删除了'self',在我的原始代码上,它似乎还没有工作。但我想我正在接近解决方案。谢谢@Honest Abe – George 2012-04-07 07:15:30

+0

@George它可能是; - ]。你没有尝试过wait_window吗? – 2012-04-07 07:18:46