繁体   English   中英

Tkinter Button 没有出现在 TopLevel 上?

[英]Tkinter Button does not appear on TopLevel?

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

mySubmitButton发生的事情真的很奇怪,该按钮似乎不想在第一次启动时出现,但是当您单击它时它会出现。 即使您单击它并从按钮上松开它,也不会发送它。 我怀疑这是否只发生在 Mac 上,或者它只发生在我的电脑上,因为这是一个非常小的问题。 或者这是我对我的代码所做的愚蠢的事情。

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

我错过了什么吗? 我用谷歌搜索并在 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. 在这个按钮之后添加另一个按钮,第二个按钮实际上出现了。 我想这可能是因为我没有调用同一个 function,但我调用了同一个,它做的事情和它看起来完全一样......
  2. 在它们之间添加一个空的 label 是行不通的。 该按钮仍未绘制。

在此处输入图像描述

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

我看到问候按钮,但我正在拨打 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM