如果一个窗口已经打开了,那么另一个窗口就不会打开

2024-04-18 15:47:52 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我只想打开一个新的窗口弹出窗口,并阻止其他按钮进程,试图打开另一个窗口弹出窗口,除非该窗口弹出窗口已经关闭。 这是我的示例代码:

你知道吗主页.py你知道吗

from tkinter import *
from cekWin import ui1,ui2,ui3
from GUI1 import GUI1
from GUI2 import GUI2
from GUI3 import GUI3

class Home(GUI1,GUI2,GUI3):
    def HomeMenu():
        ui = Tk()
        buttonUI = Button(ui,text = "Table",command = lambda: Home.process())
        buttonUI.place(x = 90,y = 70)

        buttonUI2 = Button(ui,text = "Input",command = lambda: Home.process2())
        buttonUI2.place(x = 180,y = 70)

        buttonUI3 = Button(ui,text = "Read",command = lambda: Home.process3())
        buttonUI3.place(x = 270,y = 70)

        ui.mainloop()

    def process():
        global ui2
        global ui3
        global ui1
        if ui2 == True:
           print("Another Windows is Opened")
        elif ui3 == True:
           print("Another Windows is Opened")
        else:
           GUI1.Table()
           ui1 = True     

    def process2():
        global ui2
        global ui3
        global ui1
        if ui1 == True:
           print("Another Windows is Opened")
        elif ui3 == True:
           print("Another Windows is Opened")
        else:
           GUI2.Input()
           ui2 = True

    def process3():
        global ui2
        global ui3
        global ui1
        if ui2 == True:
           print("Another Windows is Opened")
        elif ui1 == True:
           print("Another Windows is Opened")
        else:
            GUI3.Read()
            ui3 = True
 Home.HomeMenu()

桂1.py

from tkinter import *
from cekWin import *

class GUI1:
    def Table():
        ui = Tk()
        ui.protocol('WM_DELETE_WINDOW', lambda:GUI1.doSomething(ui))
        ui.mainloop()
    def doSomething(ui):
        global ui1
        global ui2
        global ui3
        ui1 = False
        ui.destroy()

桂2.py

from tkinter import *
from cekWin import *
class GUI2:
    def Input():
        ui = Tk()
        ui.protocol('WM_DELETE_WINDOW', lambda:GUI2.doSomething(ui))
        ui.mainloop()
    def doSomething(ui):
        global ui1
        global ui2
        global ui3
        ui2 = False
        ui.destroy()

桂3.py

from tkinter import *
from cekWin import *

class GUI3:
    def Read():
        ui = Tk()
        ui.protocol('WM_DELETE_WINDOW', lambda:GUI3.doSomething(ui))        
        ui.mainloop()
    def doSomething(ui):
        global ui1
        global ui2
        global ui3
        ui3 = False
        ui.destroy()

你知道吗塞克温.py你知道吗

ui1 = False
ui2 = False
ui3 = False

当在类Home上的process()、process2()和process3()中打开另一个弹出窗口时,该过程用于锁定按钮进程。如果我在同一个文件/脚本中创建所有类是可行的,但是如果我把它们放在一起就不可行了。那么,原因是什么?谢谢


Tags: lambdafromimporttrueuihomedefglobal
1条回答
网友
1楼 · 发布于 2024-04-18 15:47:52

如果我理解你想要什么,那么下面的注释脚本应该可以帮助你:

from tkinter import *

class App:
    def __init__(self, root):
        self.root = root
        self.button = Button(self.root, text="Ok", command=self.command)
        self.toplevel = Toplevel(self.root) #here we declare the window
        self.button.pack()
        self.toplevel.withdraw() #we hide the window
        self.toplevel.protocol("WM_DELETE_WINDOW", self.close)
        #above we overwrite the delete window event (which is triggered when pressing the x) to our own callback
    def command(self):
        self.toplevel.deiconify() #we show the window on button press
    def close(self):
        self.toplevel.withdraw() #we hide the window on close instead of removing it completely

root = Tk()
App(root)
root.mainloop()

相关问题 更多 >