วิดเจ็ตปุ่ม Tkinter มีประโยชน์มากในแง่ของการจัดการเหตุการณ์และการดำเนินการระหว่างการดำเนินการของแอปพลิเคชัน เราสามารถสร้างปุ่ม Tkinter โดยใช้ตัวสร้างปุ่ม (พาเรนต์, ข้อความ, ตัวเลือก .. ) เมื่อใช้ Constructor เราสามารถสร้างปุ่มได้หลายปุ่มภายในลูป
ตัวอย่าง
ในตัวอย่างนี้ เราจะสร้างหลายปุ่มในช่วงโดยใช้ Python สำหรับ วนซ้ำ
#import required libraries
from tkinter import *
from tkinter import ttk
#Create an instance of Tkinter frame
win= Tk()
#Set the geometry of the window
win.geometry("750x250")
#Create a LabelFrame
labelframe= LabelFrame(win)
#Define a canvas in the window
canvas= Canvas(labelframe)
canvas.pack(side=RIGHT, fill=BOTH, expand=1)
labelframe.pack(fill= BOTH, expand= 1, padx= 30, pady=30)
#Create Button widget in Canvas
for i in range(5):
ttk.Button(canvas, text= "Button " +str(i)).pack()
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงหน้าต่างที่มีปุ่มบางปุ่มภายในออบเจกต์ LabelFrame
