使窗格内的小部件在调整大小时拉伸。

huangapple go评论62阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年7月10日 16:13:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定