Python tkinter grid manager

gridpythonpython-3.xstickytkinter

I just learned how to use tkinter in Python (3.2.2), and I'm having some problem using the grid manager. When I put button.grid(sticky=SE), for example, the button is not being put in the bottom-right and is just being put in the upper-left, ignoring the sticky value. What am I doing wrong here? I tried to search it but I couldn't really find out what I am doing wrong.

Best Answer

You probably need to set a minimum size for the widget containing the button. If you don't, the container widget may shrink to occupy only the space required to display the button. If so, the sticky option will be meaningless since the container widget gives no space to show any difference.

For example, using a tk.Frame as the container widget:

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()

yields

enter image description here


PS. I don't have tkinter installed in Python3.2 so I can't test this, but I think the only change you need to make this work with Python3.2 is

import tkinter as tk

instead of

import Tkinter as tk