简体   繁体   English

Python Tkinter 框架布局、权重和调整大小,网格几何管理器

[英]Python Tkinter Frame layout, weight, and resizing, Grid geometry manager

I guess I am not understanding something about the way Grid works...我想我不了解 Grid 的工作方式......

My question seems related to this SO question: Python tkinter Text widget fill fixed sized frame using grid我的问题似乎与这个 SO 问题有关: Python tkinter Text widget fill fixed sized frame using grid

To which the answer was that weight needed to be given to an item in order have it take up the extra space, using the functions grid_columnconfigure() and grid_rowconfigure() .答案是需要使用函数grid_columnconfigure()grid_rowconfigure()为项目赋予权重以使其占用额外空间。

Below is the code for a simple example.下面是一个简单示例的代码。 What I am trying to achieve is to be able to specify the size of frame0 , have it be stuck to all sides of the root window, and then have the child frame, frame1 be stuck to frame0 on left, top, right, and to stretch/shrink when the main window of the app is resized.我想要实现的是能够指定frame0的大小,让它粘在根 window 的所有边上,然后让子框架frame1粘在frame0的左、上、右和到调整应用程序的主要 window 大小时拉伸/收缩。

So, initially, I would expect the application to launch at basically 500x350 size.因此,最初,我希望应用程序以基本上 500x350 的大小启动。 frame1 would be as high as it naturally is and basically 500 pixels wide (minus a little for padding). frame1将与自然高度一样高,基本上是 500 像素宽(减去一点填充)。

After acw1668's comment, I updated the code to configure root and set it's weight to 1. The size of Entry now varies to take up any horizontal space, just as I wanted.在 acw1668 的评论之后,我更新了代码以配置root并将其权重设置为 1。Entry 的大小现在可以根据我的需要改变以占据任何水平空间。 But the GUI still launches at "natural" size, though I have given an explicit size to frame0 .但是 GUI 仍然以“自然”大小启动,尽管我已经为frame0指定了明确的大小。 I'm not understanding why the geometry manager does not treat 500x350 as the natural size and allocate that?我不明白为什么几何管理器不将 500x350 视为自然大小并分配它?

Can anybody see how to get the initial launch sized as expected?任何人都可以看到如何获得预期的初始启动大小吗?

#!/usr/bin/env python3

from tkinter import *
from tkinter import ttk

root = Tk()
root.title("Layout Problem")
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)

frame0 = ttk.Frame(root, padding=4, width=500, height=350)
frame0.grid_columnconfigure(0, weight=1)
frame0.grid_rowconfigure(0, weight=1)
frame0.grid(column=0, row=0, sticky=(W, N, E, S))

frame1 = ttk.Frame(frame0, padding=4)
frame1.grid_columnconfigure(1, weight=1)
frame1['borderwidth'] = 2
frame1['relief'] = "ridge"
frame1.grid(column=0, row=0, sticky=(W, N, E))

# add widgets to 'frame1'
ttk.Label(frame1, text="Label: ").grid(row=0, column=0, sticky=W)

entryValue = StringVar()
ttk.Entry(frame1, textvariable=entryValue)\
    .grid(row=0, column=1, sticky=(W, E))
entryValue.set("Entry")

ttk.Button(frame1, text="Button")\
    .grid(row=0, column=2, sticky=E)

root.mainloop()

For reference, below is the code after incorporating acw1668's suggestions.作为参考,下面是结合了 acw1668 的建议后的代码。

The GUI now launches at expected size. GUI 现在以预期大小启动。 There is sort of a secondary issue in that now the frame doesn't shrink below 500 px width (ie, the Entry will not collapse back to natural size) though you can force the application window to be smaller.有一个次要问题,现在框架不会缩小到 500 像素宽度以下(即,条目不会折叠回自然大小),但您可以强制应用程序 window 变小。 That's sort of a separate issue.这是一个单独的问题。

I can see there is some subtlety to this whole layout and resizing business and I've yet got a ways to go. ;)我可以看到整个布局和调整业务大小有一些微妙之处,我还有办法到达 go。;)

Hopefully this question will have some pedagogic value to others following the same trail:希望这个问题对遵循相同路径的其他人具有一定的教学价值:

#!/usr/bin/env python3

from tkinter import *
from tkinter import ttk

root = Tk()
root.title("Layout Problem")
root.grid_columnconfigure(0, weight=1, minsize=500)
root.grid_rowconfigure(0, weight=1, minsize=350)

frame0 = ttk.Frame(root, padding=4)
frame0.grid(column=0, row=0, sticky=(W, N, E, S))
frame0.grid_columnconfigure(0, weight=1)
frame0.grid_rowconfigure(0, weight=1)

frame1 = ttk.Frame(frame0, padding=4)
frame1.grid_columnconfigure(1, weight=1)
frame1['borderwidth'] = 2
frame1['relief'] = "ridge"
frame1.grid(column=0, row=0, sticky=(W, N, E))

# add widgets to 'frame1'
ttk.Label(frame1, text="Label: ").grid(row=0, column=0, sticky=W)

entryValue = StringVar()
ttk.Entry(frame1, textvariable=entryValue)\
    .grid(row=0, column=1, sticky=(W, E))
entryValue.set("Entry")

ttk.Button(frame1, text="Button")\
    .grid(row=0, column=2, sticky=E)

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM