[英]Tkinter TopLevel Destroy not being detected
因此,我給了自己一個小項目,我正在嘗試制作一個小工具來連接OKEX交易所。 現在,我目前正在使用GUI,並且已經決定使用Tkinter。 經過大量的研究和其他方面的研究,我提出了以下建議,但是現在我變得有些困惑。
我有2個類,一個用於主窗口,一個用於登錄窗口。 但是,主窗口的某些功能取決於我提交登錄詳細信息后發生的情況。 現在,我知道Toplevel是用於在Tkinter中創建其他窗口的,通常您可以使用.destroy()關閉這些窗口,並且如果我想在主窗口類中進行此活動,則需要使用Toplevel.protocol( “ WM_DELETE_WINDOW”,function_name)調用...但這對我不起作用。
如果我使用右上角的十字關閉,它會按預期工作,但是如果我使用調用.destroy()的函數關閉,則不會,有人可以向我解釋為什么這不能按預期進行嗎? 也許我錯過了什么?
我要在用戶(我)輸入詳細信息后將第一幀中的文本更改為“登錄”,但是我需要先登錄並通過用戶對象來包含這些詳細信息。
無論如何,這是代碼,請幫幫我! 有問題的代碼是LoginBox類中的myquit函數,以及MainBox類中的goToLogin函數:)
from tkinter import *
from tkinter.ttk import *
class LoginBox:
def __init__(self, master): # Master is the frame that is passed in.
# Create Frame
self.master = master
self.master.title('~ Please Login ~')
def login_function(self):
user = "xxx"
secret_key = "yyy"
print("User - API Key: " + user)
print("User - Secret Key: " + secret_key)
# Perhaps check the login ...
# if it's a success then quit the function
self.myquit()
def myquit(self):
self.master.destroy()
class MainBox:
def __init__(self, master):
# Set the root window
self.master = master
self.master.geometry("500x500")
self.master.title("OkBot v0.1")
self.master.resizable(False, False)
# Initialize the frames
self.uiFrame1 = Frame(self.master) # The Top Layer -- Login Text + Login Button
# uiFrame1 Initialize --Login Text + Login Button
self.ui1_button = Button(self.uiFrame1, text="Login", command=self.goToLogin).grid(row=0, column=3, sticky=E, padx=1)
# Create Topview for popup , pass in User Object to LoginBox
def goToLogin(self):
loginMaster = Toplevel(self.master)
loginMaster.protocol("WM_DELETE_WINDOW", self.checkLogin) # This is if they close via X
loginGUI = LoginBox(loginMaster)
def checkLogin(self):
print("This function was called -- The Protocol for destroyed windows works")
# Initialize the objects and start the program
mainWindow = Tk()
myProgram = MainBox(mainWindow)
mainWindow.mainloop()
如果我使用右上角的十字關閉,它會按預期工作,但是如果我使用調用.destroy()的函數關閉,則不會,有人可以向我解釋為什么這不能按預期進行嗎? 也許我錯過了什么?
loginMaster.protocol("WM_DELETE_WINDOW", self.checkLogin)
僅告訴loginMaster.protocol("WM_DELETE_WINDOW", self.checkLogin)
在窗口管理器銷毀窗口時該怎么做。 當您調用某個函數調用destroy()
,該函數不涉及窗口管理器,因此不會調用您的回調。
如果希望在銷毀窗口時發生某些事情,則應綁定到<Destroy>
事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.