简体   繁体   English

如何测试以查看顶级窗口是否打开?

[英]How to test to see if a top-level window is open?

I'm feeling somewhat like Python programming may not be my thing.... 我感觉有些Python编程可能不是我的事。

I have created a tkinter GUI that uses a button callback to open another window (other searches say this window should be a top-level window) and it works pretty good, how-ever each time the button is pressed it opens another identical (as far as I can tell) window. 我创建了一个tkinter GUI,它使用按钮回调打开另一个窗口(其他搜索说该窗口应该是顶级窗口),并且效果很好,但是每次按下按钮时,它都会打开另一个相同的窗口(如据我所知)窗口。

Question: how can I test to see if a window (opened with the button) already exists and thus prevent duplicates from being generated??? 问题:如何测试是否已经存在一个窗口(使用按钮打开),从而防止重复生成?

NOTE: I am not (yet) a OOP programmer so please avoid that paradigm if possible... 注意:我还不是OOP程序员,因此请尽可能避免使用该范式...

regards, 问候,

Bill W. 比尔·W

I am not (yet) a OOP programmer so please avoid that paradigm if possible... 我还不是OOP程序员,因此请尽可能避免使用该范式...

Sooner or later, you'll have to understand object-oriented programming if you want to program in Python successfully. 早晚要想成功地使用Python进行编程,就必须了解面向对象的编程。 The alternative (global variables and functions everywhere) is not definitely a good approach. 替代方法(到处都有全局变量和函数)绝对不是一个好方法。 Even Tkinter has lots of classes with its respective methods, so it looks like it is necessary for your purpose. 甚至Tkinter也有很多类及其各自的方法,因此看来对于您的目的而言是必要的。

Back to your question, a solution could be setting the Toplevel window as an attribute of a class where you wrap all your application, and only open a new window if this attribute is None. 回到您的问题,一个解决方案可能是将“顶级”窗口设置为包装所有应用程序的类的属性,并且仅在此属性为“无”时才打开一个新窗口。 To set this attribute to None when you close the window, you can use the protocol method to set a callback. 要在关闭窗口时将此属性设置为None,可以使用protocol方法设置回调。

This is a small working example where you can see how it would work: 这是一个小的工作示例,您可以在其中查看其工作方式:

import Tkinter as tk

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.button = tk.Button(self, text="Open a new window", command=self.openwindow)
        self.button.pack()
        self.toplevel = None
    def openwindow(self):
        if self.toplevel is None:
            self.toplevel = tk.Toplevel(self)
            self.toplevel.protocol('WM_DELETE_WINDOW', self.removewindow)
    def removewindow(self):
        self.toplevel.destroy()
        self.toplevel = None

app = App()
app.mainloop()

You can use this method winfo_exists() to check if the window exists. 您可以使用此方法winfo_exists()来检查窗口是否存在。

if the return value is 0, it means the window doesn't exist. 如果返回值为0,则表示该窗口不存在。

if the return value is 1, it means the window exists. 如果返回值为1,则表示该窗口存在。

Sample Code... 示例代码...

from tkinter import *

mainWindow = Tk()
mainWindow.title("This is main Window")

# create a top-level window
newWindow = Toplevel(mainWindow)
newWindow.title("This is Toplevel window")

print("Before destroying window = " + str(newWindow.winfo_exists()))
newWindow.destroy()
print("After destroying window = " + str(newWindow.winfo_exists()))

mainloop()

Output is... 输出是...

# Before destroying window = 1
# After destroying window = 0

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

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