0

Trying to use grid geometry instead of pack(), but using a frame without use of pack() has me lost. All I Want is a frame around it so that I can have a border and label around certain sections. I suspect the width and height parameters are the problem here.

from tkinter import *

class App:

    def __init__(self,master):

        frame = Frame(master,height=20,width=25)

        #Multiple buttons, over n rows and columns, these are just here to demonstrate my syntax
        self.action = Button(frame,text="action",command=self.doAction)
        self.action.grid(row=n,column=n)

    def doAction(self):
        print('Action')

root = Tk()

app = App(root)

root.mainloop()

2 Answers2

1

The frame you create in the first statement of the constructor is never placed anywhere in its parent window. Since the other widgets are children of this widget, they won't be displayed either.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Whoops. Now, as an extension, how would I do this efficiently (bearing in mind that this will not be the only frame in the window), or if that's too long to write, why does this not work? `frame = Frame(master) frame.place(master)` – everyonestartssomewhere Sep 03 '13 at 00:52
  • @everyonestartssomewhere: Normally, you'll have a main function that creates several major sections, and it is responsible for calling grid, pack or place on those sections. Then, each section is responsible for calling grid, pack or place on it's children, and so on. In your case, I would make `App` a subclass of `Frame` rather than create a frame inside of it. Then, I would pack, place or grid it in the same scope that it is created (ie: `app = App(root); app.pack(side="top", fill="both", expand=True)`). See http://stackoverflow.com/a/17470842/7432 for an example. – Bryan Oakley Sep 03 '13 at 02:21
0

may be you are trying to do something like this.

class App:
    def __init__(self,master):

        frame = Frame(master,height=20,width=25)
        frame.grid()
        #Multiple buttons, over n rows and columns, these are just here to demonstrate my syntax
        for i in range(n):
            frame.columnconfigure(i,pad=3)
        for i in range(n):
            frame.rowconfigure(i,pad=3)

        for i in range(0,n):
            for j in range(0,n):
                self.action = Button(frame,text="action",command=self.doAction)
                self.action.grid(row=i,column=j)

    def doAction(self):
        print('Action')
xor
  • 2,668
  • 2
  • 28
  • 42