Resize Tkinter Listbox widget when window resizes

岁酱吖の 提交于 2019-12-22 04:05:22

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/4318103/resize-tkinter-listbox-widget-when-window-resizes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!