Python 设置 tk.Frame 宽度和高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44826267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:30:27  来源:igfitidea点击:

Setting tk.Frame width and height

pythontkinter

提问by Skitzafreak

So I am creating a GUI and I am trying to make it so that everything fits on the screen appropriately. I've drawn a rough sketch of what I want each part of the GUI to look like an it's size, so I know the rough dimensions of everything.

因此,我正在创建一个 GUI,并且我正在尝试制作它,以便所有内容都能适当地显示在屏幕上。我已经绘制了我希望 GUI 的每个部分看起来像它的大小的粗略草图,所以我知道所有东西的粗略尺寸。

The first problem I am having however is setting up the Left half of the screen.

然而,我遇到的第一个问题是设置屏幕的左半部分。

So the Left half consists of a Frame, which we'll call MainFrame, that consists of 2 Frames, which we'll call LabelFrameand ButtonFrame

所以左半由一个框架,我们将调用MainFrame,即由2架,我们会打电话LabelFrameButtonFrame

  • MainFrameneeds to be 385 pixels wide, and 460 pixels tall.
  • LabelFrameshould be 375 pixels wide, and 115 pixels tall.
  • ButtonFrameneeds to be 375 pixels wide, and 330 pixels tall.
  • MainFrame需要 385 像素宽,460 像素高。
  • LabelFrame应该是 375 像素宽,115 像素高。
  • ButtonFrame需要 375 像素宽,330 像素高。

My issue is I have no idea how to set these sizes to the frames.

我的问题是我不知道如何将这些尺寸设置为框架。

I've tried self.config(width = num, height = num)obviously replacing num with the appropriate values, but that didn't do anything.

self.config(width = num, height = num)显然已经尝试用适当的值替换 num ,但这没有做任何事情。

I know with the window itself there is a .geometrymethod, but I haven't been able to find an equivalent for tk.Frame

我知道窗口本身有一种.geometry方法,但我无法找到 tk.Frame 的等效方法

回答by CommonSense

Use grid_propagate(0)or pack_propagate(0), depenending on geometry manager in use. 0is just False, that told tkinter to shut off geometry propagation.

使用grid_propagate(0)pack_propagate(0),取决于使用的几何管理器。0只是False,告诉 tkinter 关闭几何传播。

By default, propagation is on, and a container grows/shrinks to be just big enough to hold its contents.

默认情况下,传播处于开启状态,并且容器会增长/缩小到刚好足以容纳其内容。

And I assume that your desired layout something like this:

我假设你想要的布局是这样的:

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk

root = tk.Tk()

MainFrame = tk.Frame(root, width=385, height=460, relief='raised', borderwidth=5)
LabelFrame = tk.Frame(MainFrame, width=375, height=115, relief='raised', borderwidth=5)
ButtonFrame = tk.Frame(MainFrame, width=375, height=330, relief='raised', borderwidth=5)

some_label = tk.Label(LabelFrame, text='Simple Text')
some_button = tk.Button(ButtonFrame, text='Quit', command=root.destroy)

for frame in [MainFrame, LabelFrame, ButtonFrame]:
    frame.pack(expand=True, fill='both')
    frame.pack_propagate(0)

for widget in [some_label, some_button]:
    widget.pack(expand=True, fill='x', anchor='s')

root.mainloop()

and with gridmanager the difference only in a loop section (note stickyand row/column configure):

grid经理的区别仅在循环部分(注意stickyrow/column configure):

...
for frame in [MainFrame, LabelFrame, ButtonFrame]:
    # sticky='nswe' acts like fill='both'
    frame.grid(sticky='nswe')
    frame.rowconfigure(0, weight=1)
    frame.columnconfigure(0, weight=1)
    frame.grid_propagate(0)

for widget in [some_label, some_button]:
    # sticky='wse' acts like fill='x' + anchor='s'
    widget.grid(sticky='wse')

root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
...

回答by Bryan Oakley

While I never recommend building a UI to a fixed pixel size, if that's what you're doing than the simplest solution is to use place. placegives you absolute control over the location and size of widgets, and will not cause frames to grow or shrink to fit their contents.

虽然我从不建议将 UI 构建为固定像素大小,但如果这是您所做的,那么最简单的解决方案是使用place. place使您可以绝对控制小部件的位置和大小,并且不会导致框架增长或缩小以适应其内容。

import tkinter as tk

root = tk.Tk()
root.geometry("800x480")

mainframe = tk.Frame(root, background="bisque")
labelframe = tk.Frame(mainframe, background="pink")
buttonframe = tk.Frame(mainframe, background="yellow")

mainframe.place(x=0, y=0, anchor="nw", width=385, height=460)
labelframe.place(x=0, y=0, anchor="nw", width=375, height=115)
buttonframe.place(x=0, y=116, anchor="nw", width=375, height=330)

root.mainloop()

You will page a huge penalty if you ever need to run this on a device with a different resolution or different fonts because it will be a nightmare to rearrange everything. Even if you're targeting a fixed size outer window, I personally recommend using gridor packon the inside and using relative sizes (eg: make the mainframe fill half the width and all of the height)

如果您需要在具有不同分辨率或不同字体的设备上运行它,您将面临巨大的损失,因为重新排列所有内容将是一场噩梦。即使您的目标是固定尺寸的外窗,我个人还是建议使用gridpack在内侧并使用相对尺寸(例如:使大型机填充一半的宽度和全部的高度)

回答by Mike - SMT

I believe you are looking for column and row weights using columnconfigure()and rowconfigure()

我相信您正在使用columnconfigure()和寻找列和行权重rowconfigure()

Try something like this.

尝试这样的事情。

import tkinter as tk


class MintApp(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.root = parent
        self.root.columnconfigure(0, weight=0)
        self.root.rowconfigure(0, weight=0)        


        main_frame = tk.Frame(self.root, width=385, height=460, background="black")
        main_frame.columnconfigure(0, weight=0)
        main_frame.rowconfigure(0, weight=0)
        main_frame.grid(row=0, column=0)

        label_frame = tk.Frame(main_frame, width=375, height=115, background="blue")
        label_frame.grid(row=0, column=0)

        button_frame = tk.Frame(main_frame, width=375, height=330, background="green")
        button_frame.grid(row=1, column=0)



if __name__ == "__main__":
    root = tk.Tk() 
    MyApp = MintApp(root)

    tk.mainloop()