Tkinter Button没有出现在TopLevel上吗? [英] Tkinter Button does not appear on TopLevel?

查看:186
本文介绍了Tkinter Button没有出现在TopLevel上吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我为这个问题编写的一段代码:在另一个窗口上输入文本?

This is a piece of code I write for this question: Entry text on a different window?

mySubmitButton ,该按钮似乎不希望在首次启动时出现,而是在您单击时出现。即使您单击它并将其从按钮上松开,也不会发送它。我怀疑这是仅在Mac上发生还是仅在我的计算机上发生,因为这是一个非常小的问题。

It is really strange what happened at mySubmitButton, it appears that the button does not want to appear when it is first started, it will, however appear when you click on it. Even if you click on it and release it away from the button, that way it won't be send. I am suspecting if this only happen on a mac, or it only happen to my computer, because it is a very minor problem. Or it is something silly I did with my code.

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

我失踪了吗什么东西我在Google上搜索并找到了这个在daniweb上提问和回答。而且我对它们进行了区分,无法弄清楚他的操作是固定的,但是我确实看到该行已更改为 command = root.quit 。但这仍然与我的有所不同...

Am I missing something? I googled and found this question and answer on daniweb. And I do a diff on them, can't figure out what he did "fixed", but I did see the line is changed to command=root.quit. But it is different from mine anyway...

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

Here is the full source code, and there is no error message, but the button is just missing.

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。

PS: I am using Mac OS 10.5.8, and Tk 8.4.7.

推荐答案

我看到了hello按钮,但是我在Windows 7上。

I see the hello button, but I'm on windows 7.

我快速重写了您的示例。我会很好奇它是否对您有所帮助。

I did a quick re-write of your example. I'll be curious if it makes any difference for you.

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

这篇关于Tkinter Button没有出现在TopLevel上吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆