2 Replies - 813 Views - Last Post: 20 October 2010 - 03:37 AM Rate Topic: -----

#1 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

What is the correct way of setting up a GUI?

Posted 18 October 2010 - 05:18 PM

Ok, I have a post here which outlines some code I have been working on. I was hoping for some feedback, but I might be asking a little too much so I'll ask smaller questions in the hope I can get some feedback on building things up properly.

Initially, I'd be keen to know if this is the right way to set a GUI based program up in proper pythonic OO style. I copied this class layout from somewhere on the web, but it doesn't exactly follow the way I have seen other ones done. I don't particularly understand the __init__ or Frame. lines, despite having read quite a bit on TKinter (of which most of the tutorials seem to mainly focus on quick'n'dirty window setup and widget placement).

class Application(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)
        
        self.AllMyClassVariables # get set up here
        
        self.grid()
        
        self.image = Image.open("image004.jpg") # Image I want to display in the window somewhere
        self.pic   = ImageTk.PhotoImage(self.image)
        
        self.createMenu() # function to set up menus

        self.createWidgets() # function to set up widgets



Cheers

Steve

Is This A Good Question/Topic? 0
  • +

Replies To: What is the correct way of setting up a GUI?

#2 Fixion  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 18-October 10

Re: What is the correct way of setting up a GUI?

Posted 18 October 2010 - 05:37 PM

In this example here, a GUI is being created by creating a class to subclass Tkinter's Frame class, which is an object that provides the "canvas" that you build your interface atop of.
Line 2, which has
def __init__(self, master=None):

is defining the function that is used to create this new "canvas" that is being defined in the class it belongs to, "Application" (see line 1).
The third line,
Frame.__init__(self, master)

is calling the super class's (the class "Application" is inheriting from) initializer function. This way, you are saying that an instance of the object type "Application" is therefore a "Frame" from Tkinter.

For instance, in the following code, we would be subclassing list to create a new "list type" that extends the capabilities of a list. That is precisely what is happening with the subclass of Frame.
class SpecialList(list):
    def __init__(self):
        list.__init__(self) # Create a list object. A "SpecialList" is therefore just a list as well.
    def some_function(a_value):
        . . .


Was This Post Helpful? 1
  • +
  • -

#3 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: What is the correct way of setting up a GUI?

Posted 20 October 2010 - 03:37 AM

Thanks - that kind of helps, although I'm not fully sure I understood the bit about "creating a class to subclass Tkinter's Frame class", but I get that __init__ is a constructor, and that the constructor is calling Frame's constructor - I guess I just don't get why its done this way.

In normal iterative code, it goes something like:
root = Tk()
root.mainloop()



The OO way seems like considerably more effort...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1