如果窗户已打开tkinter,则打开另一扇窗户

时间:2017-09-27 17:28:00

标签: python tkinter

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

Home.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()

GUI1.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()

GUI2.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()

GUI3.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()

cekWin.py

ui1 = False
ui2 = False
ui3 = False

在类Home上的process(),process2()和process3()处打开另一个窗口弹出窗口时锁定按钮进程的优点。如果我在同一个文件/脚本中创建所有类,那么它就可以正常工作,如果我将它们分开,它将无法工作。那么,原因是什么?谢谢

1 个答案:

答案 0 :(得分:1)

如果我理解你想要什么,那么下面评论的脚本应该对你有所帮助:

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()