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

วิธีจัดตำแหน่ง Checkbutton ใน ttk ให้ชิดซ้าย

เพื่อจัด checkbuttons ให้ชิดซ้าย ใช้พารามิเตอร์ anchor ตั้งค่าเป็น "w" (west) ตัวอย่างนี้แสดงวิธีทำ

ขั้นตอน

  • นำเข้า tkinter และสร้าง instance ของ tkinter frame

  • กำหนดขนาด frame ด้วย geometry

  • สร้าง LabelFrame รวม checkbuttons เป็นกลุ่ม

  • สร้าง checkbutton ภายใน LabelFrame ตั้ง anchor='w'

  • สร้าง checkbutton อีก 3 ตัว anchor="w" เช่นกัน จะจัดชิดซ้ายทั้งหมด

  • รัน mainloop

ตัวอย่าง

from tkinter import *

root = Tk()
root.geometry("700x350")

frame = LabelFrame(root, text="Select the Subjects", padx=20, pady=20)
frame.pack(pady=20, padx=10)

C1 = Checkbutton(frame, text="Mathematics", width=200, anchor="w").pack()
C2 = Checkbutton(frame, text="Physics", width=200, anchor="w").pack()
C3 = Checkbutton(frame, text="Chemistry", width=200, anchor="w").pack()
C4 = Checkbutton(frame, text="Biology", width=200, anchor="w").pack()

root.mainloop()

ผลลัพธ์

วิธีจัดตำแหน่ง Checkbutton ใน ttk ให้ชิดซ้าย