如何停止在添加小部件时改变帧大小?

2024-03-29 11:29:43 发布

您现在位置:Python中文网/ 问答频道 /正文

在页面框架中,我在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()

Tags: self框架init部件containerdefframetk
2条回答

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是建立在这个基础上的)。在

您可以对框架使用max-sizemin-sixe

master = tk()
master.minsize(width=777, height=777)
master.maxsize(width=777, height=777)

相关问题 更多 >