英文:
Make widgets within a pane stretch while resizing
问题
我有一个MRE,其中有一个内部有两个Labelframes水平堆叠的Panedwindow
。在每个Labelframe内部,有一些小部件,我希望它们在其Labelframe的宽度上进行拉伸,无论是在初始时还是通过手柄调整大小时。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x200")
mainframe = ttk.Frame(root)
pw = ttk.Panedwindow(mainframe, orient=tk.HORIZONTAL)
lf1 = ttk.Labelframe(pw)
lf2 = ttk.Labelframe(pw)
pw.add(lf1)
pw.add(lf2)
entry = ttk.Entry(lf1)
entry2 = ttk.Entry(lf1)
select = ttk.Combobox(lf2, values=("VAL1", "VAL2"))
mainframe.grid(sticky="nsew")
pw.grid(sticky="nsew")
entry.grid(sticky="ew")
entry2.grid(sticky="ew")
select.grid(sticky="ew")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
mainframe.grid_rowconfigure(0, weight=1)
mainframe.grid_columnconfigure(0, weight=1)
pw.grid_rowconfigure(0, weight=1)
pw.grid_columnconfigure(0, weight=1)
entry.grid_columnconfigure(0, weight=1)
entry2.grid_columnconfigure(0, weight=1)
select.grid_columnconfigure(0, weight=1)
root.mainloop()
目前,第一个Labelframe中的输入框被正确拉伸,因为第一个窗格默认调整到其内容的宽度,将第二个窗格完全拉伸。但我的问题是,组合框没有被拉伸,我希望这些小部件在通过手柄调整大小时能自动调整到Labelframes的宽度。
我已经尝试了我知道的一切。我已经阅读了Panedwindow的文档,我觉得我需要对它进行一些操作,以便给两个窗格定义参数。但阅读Panedwindow的文档并没有给我提供任何有用的提示。
谢谢。
英文:
I have this MRE where I have a Panedwindow
with inside two labelframes stacked horizontally. Inside each labelframe there are widgets that I would like to be stretched to the width of their labelframe, both at the start and when resizing the panes through the handle.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x200")
mainframe = ttk.Frame(root)
pw = ttk.Panedwindow(mainframe, orient=tk.HORIZONTAL)
lf1 = ttk.Labelframe(pw)
lf2 = ttk.Labelframe(pw)
pw.add(lf1)
pw.add(lf2)
entry = ttk.Entry(lf1)
entry2 = ttk.Entry(lf1)
select = ttk.Combobox(lf2, values=("VAL1", "VAL2"))
mainframe.grid(sticky="nsew")
pw.grid(sticky="nsew")
entry.grid(sticky="ew")
entry2.grid(sticky="ew")
select.grid(sticky="ew")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
mainframe.grid_rowconfigure(0, weight=1)
mainframe.grid_columnconfigure(0, weight=1)
pw.grid_rowconfigure(0, weight=1)
pw.grid_columnconfigure(0, weight=1)
entry.grid_columnconfigure(0, weight=1)
entry2.grid_columnconfigure(0, weight=1)
select.grid_columnconfigure(0, weight=1)
root.mainloop()
Right now the entries in the first labelframe are correctly stretched because the first pane adjusts to the width of its content by default, leaving the second one fully stretched. But my problem is that the combobox is not being stretched and I would like the widgets to automatically adjust to the width of the labelframes while resizing them through the handle.
I have tried everything I know. I have read that panedwindow acts as a geometry manager and I feel that I need to do something with it, giving me the possibility to define parameters to the two panes. But reading the panedwindow man page didn't give me any useful hint.
Thank you.
答案1
得分: 1
你需要的是:
lf1.grid_columnconfigure(0, weight=1)
lf2.grid_columnconfigure(0, weight=1)
还请注意以下行不是必要的:
pw.grid_rowconfigure(0, weight=1)
pw.grid_columnconfigure(0, weight=1)
entry.grid_columnconfigure(0, weight=1)
entry2.grid_columnconfigure(0, weight=1)
select.grid_columnconfigure(0, weight=1)
英文:
What you need is:
lf1.grid_columnconfigure(0, weight=1)
lf2.grid_columnconfigure(0, weight=1)
Note also that the following lines are not necessary:
pw.grid_rowconfigure(0, weight=1)
pw.grid_columnconfigure(0, weight=1)
entry.grid_columnconfigure(0, weight=1)
entry2.grid_columnconfigure(0, weight=1)
select.grid_columnconfigure(0, weight=1)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论