views:

95

answers:

2

Hello,

I'm working on a Gui and I'd like to know how to create a class that would implement frame.

e.g.

class WindowContent(Tkinter.?)
    """ This class would create a frame for my program window """


class App(Tkinter.Tk):
    """ main window constructor """
    def __init__(self):
        Tkinter.Tk.__init__(self)
        program_window = WindowContent ?
        self.config(window = window_content) ?

rgds,

A: 

Why do you want a class that creates several frames? Creating one class that creates multiple frames is not a very good solution. You don't need a single class for that.

Either create separate classes for each frame, or just create methods in your app to create each frame. I prefer the latter, but if you want a frame that can be used in multiple contexts it sometimes makes sense to create a class.

When I do a GUI I structure my code like this:

class App(Tkinter.Tk):
    def __init__(self):
        Tkinter.Tk__init__(self)
        self.menubar = self.makeMenubar(...)
        self.frame1 = self.makeFrame1(...)
        self.frame2 = self.makeFrame2(...)

        self.configure(menu=self.menubar)
        self.frame1.grid(...)
        self.frame2.grid(...)

In this way, each major section gets its own method to hide the details of widget creation. You could, of course, have each frame be a custom object but usually that's unnecessary.

Bryan Oakley
@Bryan Thanks for the answer. Actually I already made a class for the menu bar (thanks to your help) so I would prefer to make a class for the frame also. How can I do this please ?
Bruno
A: 

I found the answer :

class WindowProgram(Tkinter.Frame)
    """ This class creates a frame for my program window """
    def __init__(self, parent):
        Tkinter.Frame.__init__(self, parent)

class App(Tkinter.Tk):
    """ application constructor """
    def __init__(self):
        Tkinter.Tk.__init__(self)
        self.window_program = Window_Program(self)
Bruno