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

วิธีปรับขนาด Tkinter Listbox ให้พอดีกับเนื้อหา


Tkinter มี Widget Listbox ซึ่งมีประโยชน์ในการแสดงชุดข้อมูลขนาดใหญ่ในรูปแบบรายการ เพื่อกำหนดค่า Listbox ให้ใช้เมธอด configure(*options) เปลี่ยนคุณสมบัติ เช่น สีพื้นหลัง สีตัวอักษร และคุณสมบัติอื่นๆ คุณสมบัติ width ใช้กำหนดความกว้างของ Listbox หากตั้ง width=0 จะปรับขนาดให้พอดีกับความยาวเนื้อหาใน Listbox

ตัวอย่าง

# Import the required libraries
from tkinter import *

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

# Add a Listbox widget with number as the list items
listbox =Listbox(win)
listbox.insert(END,"C++", "Java", "Python", "Rust", "GoLang", "Ruby", "JavaScript", "C# ", "SQL", "Dart")
listbox.pack(side=LEFT, fill=BOTH)

# Configure the Listbox widget to set the width to its content
listbox.configure(background="skyblue4", foreground="white", font=('Aerial 13'), width=0)
win.mainloop()

ผลลัพธ์

รันโค้ดเพื่อแสดง Listbox ที่ปรับขนาดตามเนื้อหา

วิธีปรับขนาด Tkinter Listbox ให้พอดีกับเนื้อหา