简体   繁体   English

如何检查它是否正在运行一个类?

[英]How to check if it has a class running?

I made this basic code just to send here to see if I could find a solution.我制作了这个基本代码只是为了发送到这里看看我是否能找到解决方案。

I wanted the code to do the following: when I pressed the button press it calls the other class with the other button and if I press the button again it will check if the class is still showing so as not to add another X button.我希望代码执行以下操作:当我按下按钮时,它会用另一个按钮调用另一个类,如果我再次按下按钮,它将检查该类是否仍在显示,以免添加另一个 X 按钮。 of my code for you to see the result and follows a code base for readers to understand.我的代码供您查看结果并遵循代码库供读者理解。

https://imgur.com/a/UAx6xxd https://imgur.com/a/UAx6xxd

from tkinter import *
class Window:
    def __init__(self,root):
        self.frame = Frame(root)
        self.frame.pack(side = LEFT)
        self.frame1 = Frame(root)
        self.frame1.pack()
        self.But = Button(self.frame,text = 'Press', command = self.ButOk)
        self.But.pack(side = LEFT,anchor = E)

    def ButOk(self):
        self.Aux = False
        self.Aux = not self.Aux
        if self.Aux:
            Window1(root)

class Window1:
    def __init__(self,root):
        self.fram = Frame(root)
        self.fram.pack(side = LEFT)
        self.fram1 = Frame(root)
        self.fram1.pack()

        self.But = Button(self.fram, text = 'X',command = self.close)
        self.But.pack(side =RIGHT,anchor = W)
    def close(self):
        self.Aux1 = False
        self.Aux1 = not self.Aux1
        if self.Aux1:
            self.fram.pack_forget()
root=Tk()
Window(root)
root.mainloop()

I expect a result in which when the user presses the button, it does not generate this "windows".我期望当用户按下按钮时不会生成这个“窗口”的结果。

If I understood your question correctly, you want to ensure that multiple windows are not created from the multiple clicks of the button on your root level window.如果我正确理解了您的问题,您希望确保不会通过多次单击根级窗口上的按钮来创建多个窗口。

Please try to use TopLevel widget when creating multiple windows.创建多个窗口时,请尝试使用TopLevel小部件。 Additionally, you can check if a Toplevel widget exists using the following code:此外,您可以使用以下代码检查 Toplevel 小部件是否存在:

if tkinter.Toplevel.winfo_exists(toplevel_name)==1:
    self.Aux = False

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

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