繁体   English   中英

在python中一次打开多个窗口

[英]Multiple windows open at once in python

我已经搜索并在python的父窗口中找到了一些东西,但这不是我想要的。 我正在尝试制作一个简单的程序,当关闭上一个窗口时,它会打开一个窗口,然后再打开另一个窗口。 我还尝试实现某种循环或休眠时间,以在默认情况下(如果用户未这样做)销毁窗口。 这就是我所拥有的(我是新来的,请不要笑)

 from tkinter import *
 import time
 root = Tk()


 i = 0

 if i < 1:
     root.title("title")
     logo = PhotoImage(file="burger.gif")
     w1 = Label(root, image=logo).pack()
     time.sleep(3)
     root.destroy()
     i = i + 1


 if i == 1:
     root.title("title")
     photoTwo = PhotoImage(file="freedom.gif")
     labelTwo = Label(root, image=photoTwo).pack()
     time.sleep(3)
     root.destroy()
     i = i + 1





 mainloop.()

也许您正在寻找这样的东西:

from tkinter import *
import time




def openNewWindow():
    firstWindow.destroy()


    secondWindow = Tk()
    secondWindow.title("Second Window")

    photoTwo = PhotoImage(file="freedom.gif")
    labelTwo = Label(secondWindow, image=photoTwo).pack()

    secondWindow.mainloop()



firstWindow = Tk()


firstWindow.title("First Window")

logo = PhotoImage(file="burger.gif")
w1 = Label(firstWindow, image=logo).pack()

closeBttn = Button(firstWindow, text="Close!", command=openNewWindow)
closeBttn.pack()

firstWindow.mainloop()

这将在用户单击的第一个窗口中创建一个按钮。 然后调用openNewWindow函数,该函数销毁该窗口,并打开第二个窗口。 我不确定是否可以使用窗口退出按钮来执行此操作。

要创建更可持续的窗口创建,请使用以下命令:

from tkinter import *
import time

def openThirdWindow(previouswindow):
    previouswindow.destroy()


    thirdWindow = Tk()
    thirdWindow.title("Third Window")

    photoTwo = PhotoImage(file="freedom.gif")
    labelTwo = Label(thirdWindow, image=photoTwo).pack()

    thirdWindow.mainloop()

def openSecondWindow(previouswindow):
    previouswindow.destroy()


    secondWindow = Tk()
    secondWindow.title("Second Window")

    photoTwo = PhotoImage(file="freedom.gif")
    labelTwo = Label(secondWindow, image=photoTwo).pack()

    closeBttn = Button(secondWindow, text="Close!", command= lambda: openThirdWindow(secondWindow))
    closeBttn.pack()

    secondWindow.mainloop()


def openFirstWindow():
    firstWindow = Tk()


    firstWindow.title("First Window")

    logo = PhotoImage(file="burger.gif")
    w1 = Label(firstWindow, image=logo).pack()

    closeBttn = Button(firstWindow, text="Close!", command= lambda: openSecondWindow(firstWindow))
    closeBttn.pack()

    firstWindow.mainloop()

openFirstWindow()

这会将每个窗口的打开置于单独的功能中,并通过按下按钮将窗口的名称传递给下一个功能。 另一种方法是将窗口名称设置为全局,但这很麻烦。

函数“ lambda:”调用该函数,如果要通过命令传递某些内容,则必须在tkinter中键入该函数。

我们首先启动整个过程,首先调用“ openFirstWindow()”

暂无
暂无

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

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