gpt4 book ai didi

python - tkinter - 添加小部件时如何停止改变框架的大小?

转载 作者:太空狗 更新时间:2023-10-29 23:58:10 24 4
gpt4 key购买 nike

在页面框架中,我在 innerFrame 中对两个框架进行了加权,以便它们各自占据屏幕的一半,但是,当我向这些框架之一添加一个小部件时,我使用了一个列表框作为示例它很大,其中一个框架现在比另一个框架占据更多。我该如何做到这一点,以便在添加小部件时框架不会改变大小,并且每个框架都保持窗口大小的一半?

Without Listbox

With Listbox

这是我的代码:

import tkinter as tk
from tkinter import ttk

class Program(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)

tk.Tk.iconbitmap(self, default = "")
tk.Tk.wm_title(self, "")

container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)

self.frames = {}
for F in (Page, Other):

frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")

self.show_frame(Page)

def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()


class Page(tk.Frame):

def __init__(self, parent, controller):


tk.Frame.__init__(self, parent)

innerFrame = tk.Frame(self, bg="red")
innerFrame.place(relx=.5, rely=.5, anchor="c", relwidth=1.0, relheight=1.0)


innerFrame.grid_rowconfigure(0, weight=1)
innerFrame.grid_rowconfigure(1, weight=1)
innerFrame.grid_columnconfigure(0, weight=1)

#First Half
frameOne = tk.Frame(innerFrame, bg="pink")
frameOne.grid(row=0, sticky="NSWE")

#Second Half
frameTwo = tk.Frame(innerFrame, bg="green")
frameTwo.grid(row=1, sticky="NSWE")

lb = tk.Listbox(frameTwo)
lb.pack(fill="both", expand=True)


class Other(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)



app = Program()
app.state('zoomed')
app.mainloop()

最佳答案

Grid 接受参数uniform,它可以取任意值。具有相同值的所有行(或所有列)都被视为“统一组”的一部分。这会强制行(或列)的大小与其权重成比例。

如果您在一个框架中有两行,两行具有相同的权重,并且都属于同一统一组,它们将分别占用可用空间的 50%。

在您的情况下,您可以这样做:

innerFrame.grid_rowconfigure(0, weight=1, uniform="x")
innerFrame.grid_rowconfigure(1, weight=1, uniform="x")

(同样,“x”是任意的;它可以是任何值,只要两行的值相同即可)

tk 的官方文档(构建 tkinter 的基础)是这样描述的(参见 http://tcl.tk/man/tcl8.5/TkCmd/grid.htm#M24)

The -uniform option, when a non-empty value is supplied, places the row in a uniform group with other rows that have the same value for -uniform. The space for rows belonging to a uniform group is allocated so that their sizes are always in strict proportion to their -weight values.

...

When multiple rows or columns belong to a uniform group, the space allocated to them is always in proportion to their weights. (A weight of zero is considered to be 1.) In other words, a row or column configured with -weight 1 -uniform a will have exactly the same size as any other row or column configured with -weight 1 -uniform a. A row or column configured with -weight 2 -uniform b will be exactly twice as large as one that is configured with -weight 1 -uniform b.


注意:uniform 选项没有出现在 tkinter 文档中,但它是一个完全有效的选项。多年来,它一直是 tk(tkinter 建立在其上)的一部分。

关于python - tkinter - 添加小部件时如何停止改变框架的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35161101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com