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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4318103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:13:20  来源:igfitidea点击:

Resize Tkinter Listbox widget when window resizes

pythonlistboxresizetkinter

提问by garen

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

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

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.

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

回答by Bryan Oakley

You want to read up on the geometry managers packand 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.

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

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

回答by WannaB_robot

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

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

SPECS:

Windows 7, Python 3.8.1, tkinter version: 8.6

眼镜:

Windows 7,Python 3.8.1,tkinter 版本:8.6

.pack()

。盒()

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

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

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

如果您的列表框放置在框架中,框架也需要使用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()

。网格()

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

替代技术是使用该.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')   

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

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

If your listbox is placed within a frame, you will need to configure the columnand rowthat the frameis in, along with configure the columnand rowthat the listboxis 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()

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

现在还有另一种技术,但有些人对此不以为然。第三种技术是在同一脚本中使用.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)                                          

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 rootcontainer, while the listbox and buttons resided within their respective frames.

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



To check you tkinter version use:

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

import tkinter as tk
print(tk.TkVersion)

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

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