调整窗口大小时,调整 Tkinter Listbox 小部件的大小
Tkinter Listbox 小部件用于显示垂直堆叠菜单的可滚动外框。在窗口中,用户可以从该小部件选择一个或多个项目。在 Tkinter 中,所有小部件都垂直或水平对齐,有时当调整窗口大小时,安排小部件的位置看起来很困难。
我们可以使用 expand=True 和 fill=BOTH 属性配置 Listbox 小部件属性。这些属性确保小部件在垂直和水平方向上都能延伸。然而,expand 允许小部件在可用空间中增长。
示例
#Import tkinter library
from tkinter import *
#Create an instance of Tkinter frame or window
win= Tk()
#Set the geometry of tkinter frame
win.geometry("750x250")
listbox=Listbox(win) #Create a listbox widget
listbox.pack(padx=10,pady=10,fill=BOTH, expand=True)
#fill=BOTH stretch the widget both vertically and horizontally
#expand=True, expand the widget in the available space
listbox.insert(1, "Python")
listbox.insert(2, "Java")
listbox.insert(3, "C++")
listbox.insert(4, "Rust")
listbox.insert(5, "GoLang")
listbox.insert(6, "C#")
listbox.insert(7, "JavaScript")
listbox.insert(8, "R")
listbox.insert(9, "Php")
win.mainloop()输出
运行以上代码将显示一个编程语言列表。

当调整窗口大小时,该 Listbox 将保持其相对于窗口的宽度和高度。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP