Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
use of classes in python
#1
Hi!
I am relatively new to Python and mostly self-taught. I am working on a project in tkinter, and I have noticed that a lot of code examples like this one use a class (usually called App). I have a basic knowledge of classes, but I don't really understand their use here, or why it is necessary.
Can anyone explain this?

Edit:
Thanks for the answers! They were really helpful.
Reply
#2
You dont ever have to use classes. You could just make self.root a global variable root and just a function called OnDoubleClick() and have the same effect.

As your program grows larger and larger, you kinda realize that the no class method isnt all that great and that modularizing the code into classes and modules makes more sense. It isnt all that required in the example you linked to...but after you use classes its hard to not use them. However, adding content to their example is much easier. All you need to do is add a method to the class or create a new class and put the object in that class, etc. It also a lot more easier to read.

As for their use here...the dunder init method (__init__) is all the content that is prep work at the time of App() where the single method is for double click events. The if __name__ == "__main__":is a way to only run code if that is executed directly...albeit not imported to another script as a module it will not run. Its a way to run a module in a test mode per se in a specific event.

I would personally prefer it like this where you send the class root...as other classes might need the root window as well. As well as split up and better document methods.
import tkinter as tk
import tkinter.ttk as ttk

class App:
    def __init__(self, window):
        self.setup_tree()
        self.tree.bind("<Double-1>", self.OnDoubleClick)
    def setup_tree(self):
        '''setup Treeview and insert values'''
        self.tree = ttk.Treeview()
        self.tree.pack()
        for i in range(10):
            self.tree.insert("", "end", text="Item %s" % i)
    def OnDoubleClick(self, event):
        '''handle double click event'''
        item = self.tree.identify('item',event.x,event.y)
        print("you clicked on", self.tree.item(item,"text"))

root = tk.Tk()
App(root)
root.mainloop()
Recommended Tutorials:
Reply
#3
Classes are part of Object-oriented programming approach, where data and function are encapsulated together.

The linked example can also be done using functional programming. But it is preferred to use object-oriented programming with GUI to make programs more dynamic and with much less code.

In procedural or functional programming, the programmer has no choice but to pre-determine control-flow of the program compromising its flexibility.

I recommend you learn OOP. And since Python supports all major programming paradigm you should have no problem.

Give a read:
Comparing the four Python coding styles
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Classes rob101 4 468 Feb-05-2024, 06:51 PM
Last Post: rob101
  Understanding Python classes PythonNewbee 3 1,150 Nov-10-2022, 11:07 PM
Last Post: deanhystad
Sad Python classes PythonNewbee 4 995 Nov-09-2022, 01:19 PM
Last Post: deanhystad
  Inheritance vs Instantiation for Python classes mr_byte31 7 2,789 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  Understanding Python super() for classes OmegaRed94 1 1,793 Jun-09-2021, 09:02 AM
Last Post: buran
  Python classes Python_User 15 4,753 Aug-04-2020, 06:57 PM
Last Post: Python_User
  Python Classes leodavinci1990 1 2,055 Nov-27-2019, 07:25 AM
Last Post: buran
  Using classes? Can I just use classes to structure code? muteboy 5 4,978 Nov-01-2017, 04:20 PM
Last Post: metulburr
  python classes prsdll6 14 7,253 Aug-17-2017, 07:26 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020