简体   繁体   English

检查顶层窗口是否从python tkinter的外部函数打开

[英]Check if a Toplevel window is open from outer function in python tkinter

What I want to to do is to avoid opening the same window if it's already open.我想要做的是避免打开已经打开的同一个窗口。 So, I have a window and a button, and a button that opens another window, I want to click the button again and if the second window it's already open, change the focus to the second window or if it's not open, open it.所以,我有一个窗口和一个按钮,以及一个打开另一个窗口的按钮,我想再次单击该按钮,如果第二个窗口已经打开,请将焦点更改为第二个窗口,或者如果它没有打开,请打开它。

I tried with secondwindow.winfo_exists() but due that the function of the button that launches the second window is out the secondwindow function, it returns me that my secondwindow is not defined.我尝试使用secondwindow.winfo_exists()但由于启动第二个窗口的按钮的功能在 secondwindow 函数之外,它返回我的 secondwindow 未定义。

Way apart of this, I added a second button to check if the second window is created instead of checking calling the same function again.除此之外,我添加了第二个按钮来检查是否创建了第二个窗口,而不是再次检查调用相同的函数。

Any way to do this?有没有办法做到这一点? This is part of the code I have used:这是我使用的代码的一部分:

def startwind1():
    wind1 = tk.Tk()
    wind1.title('Window 1')
    w1button1 = ttk.Button(wind1,text='Launch Window 2',command=startwind2).pack()
    w1button2 = ttk.Button(wind1,text='Check if Window 2 exists',command=checkwind2).pack()
    wind1.mainloop()

def startwind2():
    wind2 = tk.Toplevel()
    wind2.title('Window 2')

def checkwind2():
    if wind2.winfo_exists() == 1:
        print('Window 2 exists')
    else:
        print('Window 2 not exists )

Hope you can help me!希望你能帮我!

Inside startwind2 you have to use global wind2 to assign window to global variable and then it will be available in other functions.startwind2您必须使用global wind2将 window 分配给全局变量,然后它才能在其他函数中使用。

It also need to create wind2 = None so variable will be available even before you create window.它还需要创建wind2 = None所以即使在您创建窗口之前变量也将可用。

import tkinter as tk
from tkinter import ttk

wind2 = None

def startwind1():
    #global wind2
    #wind2 = None

    wind1 = tk.Tk()
    wind1.title('Window 1')

    w1button1 = ttk.Button(wind1,text='Launch Window 2', command=startwind2)
    w1button1.pack()

    w1button2 = ttk.Button(wind1,text='Check if Window 2 exists',command=checkwind2)
    w1button2.pack()

    wind1.mainloop()

def startwind2():
    global wind2

    wind2 = tk.Toplevel()
    wind2.title('Window 2')

def checkwind2():
    if (wind2 is not None) and wind2.winfo_exists():
        print('Window 2 exists')
    else:
        print('Window 2 not exists')

startwind1()

You can also do this with an if statement.您也可以使用 if 语句执行此操作。 It's a different approach but works well, and is very simple.这是一种不同的方法,但效果很好,而且非常简单。

win1 = None  

def startwind1():
    if wind1 is not None and win1.winfo_exists():
        pass

    else:
        global wind1
        wind1 = tk.Tk()
        wind1.title('Window 1')
        wind1.mainloop()

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

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