3

Studying Tkinter and I've only found tutorials on Tkinter without OOP, but looking at the Python.org documentation it looks like it's all in OOP. What's the benefit of using classes? It seems like more work and the syntax looks night and day from what I've learned so far.

2 Answers 2

4

This is going to be a really generic answer and most of the answers to this will be opinionated anyways. Speaking of which,the answer will likely be downvoted and closed because of this.

Anyways... Let's say you have a big GUI with a bunch of complicated logic sure you could write one huge file with hundreds, if not thousands of lines, and proxy a bunch of stuff through different functions and make it work. But, the logic is messy.

What if you could compartmentalize different sections of the GUI and all the logic surrounding them. Then, takes those components and aggregate them into the sum which makes the GUI?

This is exactly what you can use classes for in Tkinter. More generally, this is essentially what you use classes for - abstracting things into (reusable - instances) objects which provide a useful utility.

Example:

An app I built ages ago with Tkinter when I first learned it was a file moving program. The file moving program let you select the source / destination directory, had logging capabilities, search functions, monitoring processes for when downloads complete, and regex renaming options, unzipping archives, etcetera. Basically, everything I could think of for moving files.

So, what I did was I split the app up like this (at a high level)

1) Have a main which is the aggregate of the components forming the main GUI

Aggregates were essentially a sidebar, buttons / labels for selection various options split into their own sections as needed, and a scrolled text area for operation logging + search.

So, the main components were split like this:

2) A sidebar which had the following components

  • Section which contained the options for monitoring processes

  • Section which contained options for custom regular expressions or premade ones for renaming files

  • Section for various flag such as unpacking

3) A logging / text area section with search functionality build in + the options to dump (save) log files or view them.

That's a high level description of the "big" components which were comprised from the smaller components which were their own classes. So, by using classes I was able to wrap the complicated logic up into small pieces that were self contained.

Granted, you can do the same thing with functions, but you have "pieces" of a GUI which you can consider objects (classes) which fit together. So, it just makes for cleaner code / logic.

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

2 Comments

that makes perfect sense, unfortunately im pretty green to tkinter and the resources out there are really skim and most of it is old. I understand the basics of tkinter but when seen in OOP it looks really different. Do you have any resources you could refer me to for learning?
Well, tkinter is old. The best resources are really the source code and a few places like the ttk documentation, I used effbot but it's as you noted ancient, standard python documentation, docs.python.org/3/library/tk.html, actual TK docs at tkdocs.com. etcetera for quick references of options for the widgets and what things are for. As far as learning tkinter just pick an app try to build it and learn as you go. You'll make a ton of mistakes in terms of structure. Then, build it again and then again improving upon it you'll learn good and bad structures this way.
0

Like what pythonista just said...

OOP makes your GUI code more organized and if you need to create new windows eg.toplevel() you will find it extremely useful because you won't need to write all that code again and again and again... Plus if you have to use variables that are inside another function you will not need to declare it as a global. OOP with Tkinter is the best approach

Comments

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.