未检测到Tkinter TopLevel销毁 [英] Tkinter TopLevel Destroy not being detected

查看:61
本文介绍了未检测到Tkinter TopLevel销毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我给了自己一个小项目,我正在尝试制作一个小工具来连接OKEX交易所.现在,我目前正在使用GUI,并且已经决定使用Tkinter.经过大量的研究而不是什么,我提出了以下建议,但现在我变得有些困惑.

So I've given myself a little project and I'm trying to make a little tool to connect to the OKEX exchange. Now I'm currently working on the GUI and I've decided to use Tkinter. After lots of research and what-not, I've come up with the following, but now I've become a bit stuck.

我有2个类,一个用于主窗口,一个用于登录窗口.但是,主窗口的某些功能取决于我提交登录详细信息后发生的情况.现在我知道Toplevel是用于在Tkinter中创建其他窗口的,通常您可以使用.destroy()关闭这些窗口,并且如果我想在主窗口类中接听此事件,则需要使用Toplevel.protocol("WM_DELETE_WINDOW",function_name)调用...但这对我不起作用.

I've got 2 classes, one for the main window and one for the login window. However, some of the functions of the main window rely on what happens after I've submitted the login details. Now I know that Toplevel is for creating additional windows in Tkinter, and you normally close these windows with .destroy() , and if I want to pick up on this event in the main window class then I need to use the Toplevel.protocol("WM_DELETE_WINDOW", function_name) call ...but this isn't working for me.

如果我使用右上角的十字关闭,它会按预期工作,但是如果我使用名为.destroy()的函数关闭,则无法正常工作吗,有人可以向我解释为什么这不能按预期进行吗?也许我错过了什么?

It works as expected if I close using the cross in the top right, but not if I close with my function that calls .destroy() , can anyone explain to me why this isn't working as intended? Perhaps I have missed something ?

我想在用户(我)输入详细信息后将第一帧中的文本更改为已登录",但是我需要先登录并通过用户对象来包含这些详细信息.

I want to change the text in the first frame to "Logged In" after the user (me) enters in their details, but I need to login first and pass through a user object to contain those details.

无论如何,这是代码,请帮帮我!有问题的代码是LoginBox类中的myquit函数,以及MainBox类中的goToLogin函数:)

Anyways, here's the code, please help me out! The code in question is the myquit function in the LoginBox class , and the goToLogin function in the MainBox class :)

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()的函数关闭,则无法正常工作吗,有人可以向我解释为什么这不能按预期进行吗?也许我错过了什么?

It works as expected if I close using the cross in the top right, but not if I close with my function that calls .destroy() , can anyone explain to me why this isn't working as intended? Perhaps I have missed something ?

loginMaster.protocol("WM_DELETE_WINDOW",self.checkLogin)仅告诉tkinter在窗口管理器销毁窗口时该怎么做.当您调用某个调用 destroy()的函数时,该函数不涉及窗口管理器,因此不会调用您的回调.

loginMaster.protocol("WM_DELETE_WINDOW", self.checkLogin) only tells tkinter what to do when the window manager destroys the window. When you call some function which calls destroy(), that doesn't involve the window manager and therefore your callback is not called.

如果希望在销毁窗口时发生某些事情,则应绑定到< Destroy> 事件.

If you want something to happen when the window is destroyed, you should bind to the <Destroy> event.

这篇关于未检测到Tkinter TopLevel销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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