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

วิธีสร้างหน้าต่าง Tkinter ที่ปรับขนาดได้โดยไม่มีแถบชื่อเรื่อง


เพื่อสร้างหน้าต่าง Tkinter ไม่มีแถบชื่อเรื่อง ให้ใช้คุณสมบัติ overrideredirect(boolean) ซึ่งปิดใช้งานแผงนำทางด้านบนของหน้าต่าง Tkinter อย่างไรก็ตาม มันไม่ให้ผู้ใช้ปรับขนาดหน้าต่างทันที

หากต้องการสร้างหน้าต่างปรับขนาดได้โดยไม่มีแถบชื่อเรื่องด้วยโปรแกรม ให้ใช้ Widget Sizegrip(parent) ใน Tkinter Widget Sizegrip เพิ่มความยืดหยุ่นให้แอปพลิเคชัน อนุญาตให้ผู้ใช้ดึงและปรับขนาดหน้าต่างหลัก เพื่อทำงานกับ Sizegrip ให้ Bind ปุ่มเมาส์และฟังก์ชันที่ปรับขนาดหน้าต่างเมื่อดึง grip

ตัวอย่าง

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x350")

# Remove the Title bar of the window
win.overrideredirect(True)

# Define a function for resizing the window
def moveMouseButton(e):
    x1=winfo_pointerx()
    y1=winfo_pointery()
    x0=winfo_rootx()
    y0=winfo_rooty()
    win.geometry("%s x %s" % ((x1-x0),(y1-y0)))

# Add a Label widget
label=Label(win,text="Grab the lower-right corner to resize the window")
label.pack(side="top", fill="both", expand=True)

# Add the gripper for resizing the window
grip=ttk.Sizegrip()
grip.place(relx=1.0, rely=1.0, anchor="se")
grip.lift(label)
grip.bind("", moveMouseButton)

win.mainloop()

การรันโค้ดจะแสดงหน้าต่างโดยไม่มีแถบชื่อเรื่อง สามารถปรับขนาดได้โดยดึง grip จากมุมล่างขวา

ผลลัพธ์

วิธีสร้างหน้าต่าง Tkinter ที่ปรับขนาดได้โดยไม่มีแถบชื่อเรื่อง