Home > Software design >  how to create another window after some seconds from creating one in TKinter
how to create another window after some seconds from creating one in TKinter

Time:02-13

so I started learning Tkinter and i want to open another window after 3 seconds form opening the first one . but when i run the code the first window hide under the seconde window and the second window is empty. how to show the first window but after 3 seconds show the second window?

the code:

from tkinter import *
from PIL import ImageTk,Image
import time


root = Tk()
root.title('you got hacked')
root.iconbitmap('D:/pythonFiles/aim tren/tree.ico')
root.eval('tk::PlaceWindow . center')

def openwin2():
    root.destroy()
    #first win
    win1 = Tk()
    win1.title('???')
    win1.iconbitmap('D:/pythonFiles/aim tren/tree.ico')
    win1.eval('tk::PlaceWindow . center')

    text1 = Label(win1, text="do...")
    text1.grid(row=0, column=1, padx=10, pady=10)

    myimg = Image.open("i.png")
    resize_image = myimg.resize((50, 50), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(resize_image)
    label = Label(image=img)
    label.myimg = img
    label.grid(row=0, column=0, padx=10, pady=5)

    

    
    #second win
    
    win2 = Tk()
    win2.title('???')
    win2.iconbitmap('D:/pythonFiles/aim tren/tree.ico')
    win2.eval('tk::PlaceWindow . center')

    text2 = Label(win2, text="you...")
    text2.grid(row=0, column=1, padx=10, pady=10)

    myimg = Image.open("i.png")
    resize_image = myimg.resize((50, 50), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(resize_image)
    label = Label(image=img)
    label.myimg = img
    label.grid(row=0, column=0, padx=10, pady=5)

    
    
    time.sleep(3)
    win1.destroy()
    win2.mainloop()
    win1.mainloop()



text_erorr = Label(root, text="your pc has been hacked, you need to fix the pc before the virus get to 'system32'\n you need to be fast and answer the next questions.")
button_ok = Button(root, text="ok", padx=25, bg='lightgray', command= openwin2)

text_erorr.grid(row=0, column=1, columnspan=2, padx=15, pady=2)
button_ok.grid(row=1, column=3, padx=10, pady=10)

myimg = Image.open("tree.png")
resize_image = myimg.resize((50, 50), Image.ANTIALIAS)
img = ImageTk.PhotoImage(resize_image)
label = Label(image=img)
label.myimg = img
label.grid(row=0, column=0, padx=10, pady=5)

root.mainloop()

CodePudding user response:

Use root.after("time in ms", create_window) to wait x seconds and declare a new window variable with window = tk.Toplevel(root):

import tkinter as tk

root = tk.Tk()

def create_window():
    new_window = tk.Toplevel(root)

root.after(3000, create_window)

root.mainloop()
  • Related