Computer >> Máy Tính >  >> Lập trình >> Python

Làm thế nào để tạo Splash Screen bằng Tkinter?


Giả sử chúng ta muốn tạo một màn hình giật gân bằng tkinter. Để tạo màn hình asplash, chúng ta sẽ làm theo các bước dưới đây -

  • Tạo một màn hình giật gân với một số nhãn trong đó.

  • Làm cho màn hình giật gân không có đường viền bằng cách sử dụng chuyển hướng được ghi đè phương pháp.

  • Tạo một chức năng cho cửa sổ chính sẽ xuất hiện một lúc ngay sau màn hình hiển thị.

  • Hiện đang sử dụng sau , chúng ta có thể xác định thời gian mà cửa sổ chính sẽ xuất hiện.

Ví dụ

#Importing the tkinter library
from tkinter import *

#Create an instance of tkinter frame
splash_win= Tk()

#Set the title of the window
splash_win.title("Splash Screen Example")

#Define the size of the window or frame
splash_win.geometry("700x200")

#Remove border of the splash Window

splash_win.overrideredirect(True)

#Define the label of the window
splash_label= Label(splash_win, text= "Hello World!", fg= "green",
font= ('Times New Roman', 40)).pack(pady=20)
def mainWin():
   splash_win.destroy()
   win= Tk()
   win.title("Main Window")
   win.geometry("700x200")
   win_label= Label(win, text= "Main Window", font= ('Helvetica', 25), fg= "red").pack(pady=20)

#Splash Window Timer

splash_win.after(5000, mainWin)

mainloop()

Chạy đoạn mã trên sẽ tạo ra kết quả và sẽ hiển thị màn hình giật gân sau một thời gian, cửa sổ chính.

Đầu ra

Làm thế nào để tạo Splash Screen bằng Tkinter?

Làm thế nào để tạo Splash Screen bằng Tkinter?