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

สร้างหน้าต่าง Tkinter ขยายเต็มหน้าจอ (Maximized Window) อัตโนมัติ

มี 2 วิธีสร้างหน้าต่าง Tkinter ขยายเต็มหน้าจออัตโนมัติ:

  • ใช้เมธอด state() ของ Tkinter เรียกด้วยแอตทริบิวต์ "zoomed"
root.state("zoomed")
  • ใช้เมธอด attributes พร้อมพารามิเตอร์ "-fullscreen" ตั้งเป็น True

โดยค่าเริ่มต้น Tkinter สร้างหน้าต่างขนาดคงที่ สามารถปรับขนาดด้วย geometry method เช่น root.geometry("700 x 350")

ตัวอย่างที่ 1: ใช้ state('zoomed')

from tkinter import *

root = Tk()
Label(root, text="Welcome to Tutorialspoint", font="Calibri, 20").pack(pady=20)
root.state('zoomed')
root.mainloop()

ผลลัพธ์

สร้างหน้าต่าง Tkinter ขยายเต็มหน้าจอ (Maximized Window) อัตโนมัติ

ตัวอย่างที่ 2: ใช้ attributes('-fullscreen', True)

from tkinter import *

root = Tk()
Label(root, text="Welcome to Tutorialspoint", font="Calibri, 20").pack(pady=20)
root.attributes('-fullscreen', True)
root.mainloop()

ผลลัพธ์

สร้างหน้าต่าง Tkinter ขยายเต็มหน้าจอ (Maximized Window) อัตโนมัติ