简体   繁体   English

如何同时打开多个 Tk() windows?

[英]How do I open multiple Tk() windows at the same time?

My problem is simple, but i really don't know what the issue is.我的问题很简单,但我真的不知道问题是什么。 I'm trying to open more than one window almost at the same time, but if i do it like that:我试图几乎同时打开多个 window,但如果我这样做:

from tkinter import *
import threading
import time

class new_window:
    def build(self, killtime):
        self.w = Tk()
        self.w.update()
        time.sleep(killtime)
        self.w.destroy()
    def __init__(self, killtime):
        threading.Thread(target=self.build(killtime)).start()

a = new_window(2)
time.sleep(2)
b = new_window(2)

it doesn't behave like: "open, wait, open" but instead like: "open, wait until killed, wait, open"它的行为不像:“打开,等待,打开”,而是:“打开,等到被杀,等待,打开”

What i mean is that the delay starts after the first window is closed, not after the window started.我的意思是延迟在第一个 window 关闭后开始,而不是在 window 开始后开始。 I thought a Thread would help me out, but it didn't.我以为线程会帮助我,但事实并非如此。

Hopefully one of you knows how to fix that.希望你们中的一个人知道如何解决这个问题。

You don't really need to use the threading module.您实际上并不需要使用threading模块。

You can use the .after() method to open a new window a little after.您可以稍后使用 .after .after()方法打开一个新的 window。

Here is your code:这是您的代码:

from tkinter import *
window = Tk()
window.title("window1")
def open_new_window():
    window2 = Toplevel()
    window2.title("Window2")
window.after(1000, open_new_window)
window.mainloop()

Hope this helps!希望这可以帮助!

Edit: The code above opens one window, then stops doing anything.编辑:上面的代码打开一个 window,然后停止做任何事情。 If you want new windows to keep opening with a small delay in between, you can use the below code:如果您希望新的 windows 保持打开,中间有一小段延迟,您可以使用以下代码:

from tkinter import *
window = Tk()
window.title("window1")
def open_new_window():
    window2 = Toplevel()
    window2.title("Window2")

    window.after(1000, open_new_window)
open_new_window()

window.mainloop()

Hope this helps!希望这可以帮助!

I did it like this now.我现在是这样做的。 But i still marked the Answer of @TheMaker as the Solution, cause its more compact and easier to understand.但我仍然将@TheMaker 的答案标记为解决方案,因为它更紧凑且更易于理解。

from tkinter import *
import threading
import time
from random import *

root = Tk()
root.configure(background='black')

root.attributes("-fullscreen", True)

class new_window:
    def build(self, delay, x, y):
        self.w = Tk()
        self.w.title("")
        self.ws = root.winfo_screenwidth() # width of the screen
        self.hs = root.winfo_screenheight() # height of the screen
        self.w.geometry(f"250x100+{int(round(self.ws/100*x))}+{int(round(self.hs/100*y))}")
        self.w.update()
        time.sleep(delay)
    def __init__(self, delay, x, y):
        threading.Thread(target=self.build(delay, x, y)).start()

def rdm(delay, count):  
    i = 1
    while i<=count:
        new_window(delay, randint(1, 100), randint(1, 100))
        i+=1
rdm(0.02, 100)

root.mainloop()

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

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