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 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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: 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 stackoverflow.com/a/17470842/7432 for an example.
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')

1 Comment

Very good code, I was not specific enough in my description, sorry. But thank you anyway, I like this code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.