繁体   English   中英

调整窗口大小时调整 Tkinter 列表框小部件的大小

[英]Resize Tkinter Listbox widget when window resizes

我是 Tkinter 的新手,我有一个Listbox小部件,我想在更改主窗口的大小时自动调整它的大小。

基本上我想要一个流畅的高度/宽度列表框。 如果有人可以向我指出一些文档或提供一些代码/见解,我将不胜感激。

您想阅读几何管理器packgrid ,它允许您将小部件放置在窗口中并指定它们是否增长和缩小。 还有第三个几何管理器place ,但它不经常使用。

这是一个简单的例子:

import tkinter as tk

root = tk.Tk()
scrollbar = tk.Scrollbar(root, orient="vertical")
lb = tk.Listbox(root, width=50, height=20, yscrollcommand=scrollbar.set)
scrollbar.config(command=lb.yview)

scrollbar.pack(side="right", fill="y")
lb.pack(side="left",fill="both", expand=True)

for i in range(0,100):
    lb.insert("end", "item #%s" % i)

root.mainloop()

如果您希望使用grid而不是pack ,请删除调用pack的两行并将其替换为以下四行:

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

scrollbar.grid(row=0, column=1, sticky="ns")
lb.grid(row=0, column=0, sticky="nsew")

请注意,使用 grid 您必须采取额外的步骤来配置包含列表框的行和列的weight ,否则 tkinter 不会为小部件分配任何额外的空间。

在调整窗口大小时允许列表框拉伸的两种主要方法是使用.pack().grid()方法。

眼镜:

Windows 7,Python 3.8.1,tkinter 版本:8.6

。盒()

我发现最简单的方法是使用.pack()方法,并利用fill= & expand=True选项。

import tkinter as tk

root=tk.Tk()                                              #Creates the main window

listbox=tk.Listbox(root)                                  #Create a listbox widget

listbox.pack(padx=10,pady=10,fill=tk.BOTH,expand=True)    #fill=tk.BOTH, stretch vertically and horizontally
                                                          #fill=tk.Y, stretch vertically
                                                          #fill=tk.X, stretch horizontally

如果您的列表框放置在框架中,框架还需要使用fill= & expand=True选项。

import tkinter as tk

root=tk.Tk()

frame1=tk.Frame(root)
frame1.pack(fill=tk.BOTH, expand=True)

listbox=tk.Listbox(frame1)
listbox.pack(padx=10,pady=10,fill=tk.BOTH,expand=True)

。网格()

另一种技术是使用.grid()方法并利用sticky=选项。 此外,您需要配置列表框所在的

import tkinter as tk

root=tk.Tk()  #create window
root.columnconfigure(0,weight=1)    #confiugures column 0 to stretch with a scaler of 1.
root.rowconfigure(0,weight=1)       #confiugures row 0 to stretch with a scaler of 1.

listbox=tk.Listbox(root)
listbox.grid(row=0,column=0,padx=5,pady=5,sticky='nsew')   

sticky选项使列表框在拉伸时在单元格的“北”(上)、“南”(下)、“东”(右)和“西”(左)侧。

如果你的列表框放在一个框架之内,你将需要配置的框架是,与配置沿着列表框在不在。

import tkinter as tk

root=tk.Tk()               #create window
root.columnconfigure(0,weight=1)  
root.rowconfigure(0,weight=1)

frame1=tk.Frame(root)
frame1.grid(row=0,column=0,sticky='nsew')
frame1.columnconfigure(0,weight=1)
frame1.rowconfigure(0,weight=1)

listbox=tk.Listbox(frame1)
listbox.grid(row=0,column=0,padx=5,pady=5,sticky='nsew')

.pack() 和 .grid()

现在还有另一种技术,但有些人对此不以为然。 第三种技术是在同一脚本中使用.pack()方法和.grid()方法。 只要每个容器只使用一种管理类型,您就可以在同一个脚本中混合不同的几何管理方法。 你可以在下面看到一个例子。

import tkinter as tk

root=tk.Tk()               #create window

frame1=tk.Frame(root)                                #container: root
frame1.pack(fill=tk.BOTH,expand=True)                               
frame1.columnconfigure(0,weight=1)
frame1.rowconfigure(0,weight=1)
frame1.rowconfigure(1,weight=1)

listbox=tk.Listbox(frame1)                            #container: frame1
listbox.grid(row=0,rowspan=2,column=0,padx=5,pady=5,sticky='nsew') 

btn1=tk.Button(frame1,text='Demo1')                   #container: frame1        
btn1.grid(row=0,column=1, padx=5, pady=5)                          

btn2=tk.Button(frame1,text='Demo2')                   #container: frame1     
btn2.grid(row=1,column=1, padx=5, pady=5)                          

frame2=tk.Frame(root)                                 #container: root 
frame2.pack()

btn3=tk.Button(frame2,text='Demo3')                   #container: frame2
btn3.grid(row=0,column=0)                                          

您可以在上面看到框架使用.pack()而列表框和按钮使用.grid() 这是可能的,因为框架位于容器中,而列表框和按钮位于各自的框架中。


要检查您的 tkinter 版本,请使用:

import tkinter as tk
print(tk.TkVersion)

如果您想了解fillexpand之间的区别,请参阅以下链接。 https://effbot.org/tkinterbook/pack.htm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM