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

发布于 2024-10-05 15:21:47 字数 131 浏览 1 评论 0原文

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

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

I'm new to Tkinter, and I've got a Listbox widget that I'd like to automatically-resize when changing the main window's size.

Essentially I would like to have a fluid height/width Listbox. If someone can point me to some documentation or provide a bit a code / insight, I'd appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

池予 2024-10-12 15:21:47

您想要阅读几何管理器 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")

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

You want to read up on the geometry managers pack and grid, which lets you place widgets in a window and specify whether they grow and shrink or not. There's a third geometry manager, place, but it's not used very often.

Here's a simple example:

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()

If you wish to use grid instead of pack, remove the two lines that call pack and replace them with these four lines:

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")

Note that with grid you have to take the extra step to configure the weight for the row and column that contains the listbox, otherwise tkinter won't allocate any extra space to the widget.

圈圈圆圆圈圈 2024-10-12 15:21:47

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

规格:

Windows 7、Python 3.8.1、tkinter 版本:8.6

.pack()

我发现最简单的方法是使用 .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()

另一种技术是使用 .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')   

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

如果您的列表框放置在框架内,则需要配置框架所在的 ,并配置列表框所在的

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')

。 .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

The two main ways to allow a listbox to stretch when the window is resized are using the .pack() or .grid() methods.

SPECS:

Windows 7, Python 3.8.1, tkinter version: 8.6

.pack()

I found the easiest way to do this is by using the .pack() method, and utilizing the fill= & expand=True options.

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

If your listbox is placed in a frame, the frame will also need to use the fill= & expand=True options.

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()

The alternative technique is to use the .grid() method and utilize thesticky= option. In addition, you will need to configure the row and column that the listbox resides in.

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')   

The sticky option causes the listbox to stick to the "North" (Top), "South" (Bottom), "East" (Right), and "West" (Left) sides of the cell as it is stretched.

If your listbox is placed within a frame, you will need to configure the column and row that the frame is in, along with configure the column and row that the listbox is in.

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()

Now there is one other technique, but some people frown on it. The third technique is to utilize the .pack() method and .grid() method in the same script. You can mix different geometry management method in the same script as long as only a one management type is used per container. You can see an example of this below.

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)                                          

You can see above that the frames used .pack() while the listbox and buttons used .grid(). This was possible because the frames resided within the root container, while the listbox and buttons resided within their respective frames.


To check you tkinter version use:

import tkinter as tk
print(tk.TkVersion)

If you would like to learn about the differences between fill and expand, please see the following link.
https://effbot.org/tkinterbook/pack.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文