[英]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()
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.