[Tutor] Tkinter + frame + grid question
__peter__ at web.de
Mon May 15 10:14:48 EDT 2017
> Thank you for reading this.
> I'd like to place a red frame in the centre of the main window.
> I've discovered two ways to achieve this using place() and pack(padx,
> pady). Grid() does not give the desired result.
> Next I'd like to place some widgets on the frame. Again place() does what
> I want and pack() almost does except the red frame is the size of the
> padding. It now seems to me that this is the way it's supposed to work.
> I'd like to use grid(), but so far, all that I can achieve is to place
> widgets on the main window and not the frame. The frame does not display
> if I use grid().
> I've spent hours on this and an Internet search has not helped at all.
> However, I did learn that grid() and pack() are not compatible, could this
> be the problem? Perhaps I have to let go of the familiar grid() method and
> use pack() instead? Place() does exactly what I want but it's a bit
> cumbersome to use. The more I think about it, the more I think pack() is
> the go.
> Still, I'd like to know, what's the best method?
place() is a pain if the screen or font size differs from what you expect.
I use grid() almost exclusively, but I have to admit that I always have to
play around a bit until I get something close to what I want.
> The following is a summary of what I've tried:
> from tkinter import *
> class App:
> def __init__(self, master):
> frame = Frame(master, width = 300, height = 300, bg = 'red')
> frame.pack(pady = 50)
> #frame.place(x = 50, y = 50)
> Entry(frame).place(x = 100, y = 100, anchor = CENTER)
> #Entry(frame).pack(padx = 20, pady = 20)
> #Entry(frame).grid(row = 10, column = 1)
> #Entry2 = Entry
> #Entry2(frame).grid(row = 20, column = 1)
Columns and rows are logical columns; typically you will use 0 and 1 rather
than 10 and 20.
> root = Tk()
> app = App(root)
Here's a grid()-based contender:
def __init__(self, master):
frame = Frame(master, bg="red")
frame.grid(row=0, column=0, padx=30, pady=30, sticky="nsew")
entry = Entry(frame)
entry.grid(row=0, column=0, padx=30, pady=30)
More information about the Tutor