#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Question about classes.


    Since i have to build a python gui program, with many features like: Updating label text from sql database, setting image's as buttons and background.. I was wondering do i need to use classes? I will use tkinter to do such a program, but i have seen many people using classes for their gui programs. My problem is that i don't understand how they work and it's really complicated for me .

    So can i make a good flexible program without classes? Why are people using classes when they can make a program without them?

    take a look of this code i found online:
    Code:
    class Application(Frame):
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.parent = master
            self.initUI()
    
        def initUI(self):
            self.outputBox = Text(self.parent, bg='yellow', height= 10, fg='green', relief=SUNKEN, yscrollcommand='TRUE')
            self.outputBox.pack(fill='both', expand=True)
            self.button1 = Button(self.parent, text='button1', width=20, bg ='blue', fg='green', activebackground='black', activeforeground='green')
            self.button1.pack(side=RIGHT, padx=5, pady=5)
            self.button2 = Button(self.parent, text='button2', width=25, bg='white', fg='green', activebackground='black', activeforeground='green')
            self.button2.pack(side=LEFT, padx=5, pady=5)
    What does Class aplication(frame) do? Is it possible to do same, but without class?

    Can you please explain me a bit, i am really confused :/.. Is class alternative or only way?
    Thanks in advance soo much!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Sure, one can write programs functionally the same without classes. Why use a class when I'll make only one instance of the object???

    Many programs have more code than merely the GUI. The GUI provides a user interface to the essence of the program. Classes provide name spaces if nothing more. So do modules.
    Code:
    class MyException(ValueError):
        pass
    
    if None:
        print('wonderful!')
    else:
        raise MyException('Whoops!')
    How can MyException possibly work? There's no code! MyException inherits all the functionality (methods and attributes) of ValueError.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo