【问题标题】:Resizing one widget inside another widget in Tkinter在 Tkinter 的另一个小部件中调整一个小部件的大小
【发布时间】:2014-09-01 05:30:37
【问题描述】:

我正在为穆斯堡尔光谱学(化学)开发一个模拟软件,但在设计 UI 时,我在使用父小部件调整子小部件的大小时遇到​​了问题。当窗口最大化时,父框架会填充额外的空间,但子窗口小部件不会改变其大小。

from Tkinter import *
import ttk
import tkFileDialog as FileDialog
from PIL import Image, ImageTk
import tkFont
import matplotlib
from numpy import sin, pi

root = Tk()
# Main Container
content = ttk.Frame(root)
bold = tkFont.Font(family='Helvetica', weight='bold')
LeftFrame = ttk.Frame(content, borderwidth=5, relief="groove", width=200)
RightFrame = ttk.Frame(content, borderwidth=5, relief="groove", width=750)
text = Text(LeftFrame, height=40, width=30, padx=5, pady=5)

..... Some Codes .....

# Geometry
LeftFrame.grid(column=0, row=1, rowspan=2, sticky=(N, S, E, W))
RightFrame.grid(column=1, row=1, columnspan=3, rowspan=2, sticky=(N, S, E, W))
namelbl.grid(column=0, row=1, sticky=(N, S, E, W), padx=5)
text.grid(column=0, row=2, sticky=(N, S, E, W))

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
content.columnconfigure(0, weight=1)
content.columnconfigure(1, weight=3)
content.columnconfigure(2, weight=3)
content.columnconfigure(3, weight=3)
content.rowconfigure(1, weight=1)

root.state("zoomed")

root.mainloop()

所以,我的问题是如何以相同的速率更改 text 小部件(子级)和 LeftFrame(父级)的大小。在我的情况下,只有 LeftFrame 正在扩展,而 text 是恒定的。

【问题讨论】:

    标签: python tkinter widget


    【解决方案1】:

    您没有为框架内的小部件提供sticky 选项,这会阻止小部件填充它们已提供的区域。此外,您没有给出内部框架行和列的权重。

    任何时候你使用网格,你都应该为父小部件的行和列指定权重。如果您在框架内有框架,则每个框架都是独立的,并且需要适当配置其行和列。

    【讨论】:

    • 我想我将sticky 选项提供给框架text.grid(column=0, row=2, sticky=(N, S, E, W)) 内的小部件。我已经给content.columnconfigure(0, weight=1) 列赋予了权重。能否请您详细说明一下,因为 Tkinter 上的教程很少
    • LeftFrame.columnconfigure(0, weight=1) 为我工作。非常感谢!!!我建议你为 Tkinter 教程发布一个网站,因为与其他语言相比,互联网上可用的资源很少。
    • 如果您不介意,请您看看我的一个问题,因为我们无法解决代码的问题。提前致谢。 stackoverflow.com/q/24748589/1581133
    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 2015-02-06
    • 2011-05-18
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多