简体   繁体   English

tkinter网格和框架没有调整大小框架

[英]tkinter grid and frame no resize frame

class input_data(Tkinter.Tk):
    def __init__(self):
        Tkinter.Tk.__init__(self);
        self.resizable(width=False,height=False);
        self.geometry('500x500');
        t = ['frame','input_text'];
        for i in t:
            eval('self.'+i+'()');
def frame(self):
    self.f = Tkinter.Frame(self,width=200,height=200,bg='cyan');
    self.f.grid(row=0,column=0);

def input_text(self):
    self.e = Tkinter.Entry(self.f);
    self.e.grid(row=0,column=0);  

root = input_data();
root.mainloop();

the code looks fine, but the frame does not fit the indicated dedida, but in the following code if you do 代码看起来很好,但框架不符合指示的dedida,但如果你这样做,则在下面的代码中

import Tkinter as tk

class SimpleApp(object):
    def __init__(self, master, **kwargs):
    title = kwargs.pop('title')
    frame = tk.Frame(master, borderwidth=5, bg = 'cyan', **kwargs)
    frame.grid()
    button = tk.Button(frame, text = title)
    button.grid(sticky = tk.SE)
    frame.rowconfigure('all', minsize = 200)
    frame.columnconfigure('all', minsize = 200)

def basic():
    root = tk.Tk()
    app = SimpleApp(root, title = 'Hello, world')
    root.mainloop()
basic()

where I'm failing to make the first code does not work the width and height 我没有使第一个代码无法正常工作的宽度和高度

I don't know what you are trying to do. 我不知道你要做什么。 There are several things that appear wrong in the code. 代码中出现了一些错误。 The first problem is that, as written in the question, it doesn't run at all. 第一个问题是,正如问题所写,它根本不运行。 You're missing an import statement and your indentation is messed up. 你错过了一个import语句,你的缩进搞砸了。

In the first example you are giving the frame a width and a height. 在第一个示例中,您将为框架指定宽度和高度。 However, the default behavior for frames is to automatically resize to fit all of its children. 但是,框架的默认行为是自动调整大小以适合其所有子项。 This is called geometry propagation Thus, the frame will shrink to be just big enough to hold the entry widget. 这称为几何传播因此,框架将缩小到足以容纳条目窗口小部件。

If you want the frame to remain at 200 pixels wide by 200 pixels tall you need to turn geometry propagation off. 如果您希望帧保持200像素宽,200像素高,则需要关闭几何体传播。 You do this with something like: 你这样做的事情如下:

self.f.grid_propagate(False)

This is rarely the recommended way to do things, but if you really want the frame to be exactly some specific size, this is one way to do it. 这很少是推荐的做事方式,但如果你真的希望框架具有一定的特定尺寸,这是一种方法。

If you did not want it to be the exact size, but instead want it to fill the whole window, you need to use the sticky option in the grid command, and set a non-zero weight for row 0 and column 0. For example: 如果您希望它是精确的大小,而是希望它填充整个窗口,则需要使用grid命令中的sticky选项,并为第0行和第0列设置非零权重。例如:

def frame(self):
    self.f = Tkinter.Frame(self,width=200,height=200,bg='cyan');
    self.f.grid(row=0,column=0, sticky="nsew");
    self.grid_rowconfigure(0, weight=1)
    self.grid_columnconfigure(0, weight=1)

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

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