【问题标题】:Tkinter Button does not appear on TopLevel?Tkinter 按钮没有出现在 TopLevel 上?
【发布时间】:2012-04-20 14:21:39
【问题描述】:

这是我为这个问题写的一段代码:Entry text on a different window?

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()

  1. 在此按钮之后添加另一个按钮,第二个按钮实际出现。我想这可能是因为我没有调用同一个函数,但我调用了同一个函数,它做的事情和它看起来完全一样......
  2. 在它们之间添加一个空标签不起作用。按钮仍未被绘制。

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

【问题讨论】:

  • 这在 64 位 win7 和 activepython 2.6.7(在将 tkinter 更改为 Tkinter 之后)从 IDE(Stani 的 SPE)或双击开始时完美运行。它可能是特定于操作系统的
  • 但是我写的其他程序上的常规按钮很好,甚至当我创建第二个(具有相同功能)时,它也会出现,(只是中间一个(点击我)一个是失踪)......这没什么大不了的,但我只是好奇和好奇。或者我的安装有问题。
  • 您的代码在我的 Macintosh 上运行良好,使用 python 2.7。也许你有一个错误版本的 python/tkinter?
  • @BryanOakley 如果我想实现一个弹出窗口,无论是滑动条,输入框等,单选按钮,这是正确的方法吗?对代码本身有什么建议吗?
  • @George:没关系。但我建议不要使用全局变量。与其让对话框自行销毁,不如让它只销毁实际的小部件,但让对象保持活动状态。然后,从您的主逻辑中调用 inputDialog.get_string()del inputDialog 之类的东西。

标签: python button tkinter


【解决方案1】:

我看到了 hello 按钮,但我使用的是 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()

【讨论】:

  • 你的版本就像一个魅力,是我写MyDialog()类的方式吗?该按钮无需我“单击”或在选项卡之间切换即可出现。
  • 我删除了self,在我的原始代码中,它似乎还没有工作。但我想我越来越接近解决方案了。谢谢@Honest Abe
  • @George 可能是 ;-]。你试过不用wait_window吗?
  • @Abe 只是累了,现在结果相同。我想如果有什么我会在你的结构中实现它,至少它是有效的。或切换到wxpython :D btw: 不知道为什么@Honest Abe 不起作用... 我也想知道Linux是否也遇到这个问题,我下周一回学校实验室试试。
  • @George 我猜这与您在主程序中执行一半而在课堂中执行另一半的事实有关。我建议以一种或另一种方式来做这件事(比如我的例子,或者根本不上课)。晚安:-]
猜你喜欢
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多