Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีสร้าง Splash Screen ด้วย Tkinter

สมมติว่าเราต้องการสร้าง Splash Screen ด้วย Tkinter ขั้นตอนมีดังนี้:

  • สร้าง Splash Screen พร้อม Label ภายใน

  • ทำให้ Splash Screen ไม่มีขอบ (Borderless) โดยใช้เมธอด overrideredirect

  • สร้างฟังก์ชันสำหรับหน้าต่างหลัก (Main Window) ซึ่งจะปรากฏหลัง Splash Screen

  • ใช้เมธอด after เพื่อกำหนดเวลาที่ Main Window จะปรากฏ

ตัวอย่าง

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

การรันโค้ดข้างต้นจะสร้าง Splash Screen และหลังจากสักครู่ จะแสดง Main Window

ผลลัพธ์

วิธีสร้าง Splash Screen ด้วย Tkinter

วิธีสร้าง Splash Screen ด้วย Tkinter