Francisco
Francisco

Reputation: 561

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

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

Upvotes: 0

Views: 19021

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

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.

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. 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:

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)

Upvotes: 5

Related Questions