2 Replies - 4586 Views - Last Post: 02 December 2010 - 01:41 PM Rate Topic: -----

#1 pidle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-December 10

tkinter : needless object-oriented programming ?

Posted 02 December 2010 - 09:57 AM

Much tkinter code I'm struggling with seems to use OOP needlessly. Below is code from this website
almost verbatum ( changed to tkinker rather than Tkinter as I'm using python 3.1.1 )
    import tkinter
    class App :
        def __init__(self) :
            self.root = tkinter.Tk()
            self.frame = tkinter.Frame(self.root, width=200, height=100)
            self.frame.bind("<Button-1>", self.click)
            self.frame.pack()
            self.root.mainloop()
        def click(self, event) :
            print("Clicked at :", event.x, event.y)
    App()

Same thing can be done non-00 :
    import tkinter
    root = tkinter.Tk()
    frame = tkinter.Frame(root, width = 200, height = 100)
    def click(event) :
        print("Clicked at :", event.x, event.y)
    frame.bind("<Button-1>", click)
    frame.pack()
    root.mainloop()


Above is only 8 lines and a lot less words compared to the OOP 11 lines.
Yet many tkinter examples I find in docs, courses, books are object-oriented; even the
whole routine as in example one.
I thought the purpose of OOP was to package and reuse the code by instantiating a number
of instances. Why OO code a routine that is used once ? This passeth all understanding.
Can anyone explain ?

Someone on the web gave the quote about OOP : " All I wanted was a banana, and I got
a gorilla holding the banana and the entire jungle".
Thanks for any help. Dave, lost in a jungle in Texas

Edited by Dogstopper: :code:

This post has been edited by Dogstopper: 02 December 2010 - 01:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: tkinter : needless object-oriented programming ?

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: tkinter : needless object-oriented programming ?

Posted 02 December 2010 - 10:15 AM

I can't think of a singe example of a 10 line program that needs OOP. Period. In that regard, you are stacking the deck with your question; a fairer question would be "At what point does OOP make sense when programming with Tkinter?" To which I would answer: "Any time you're dealing with multiple windows."

Consider a simple ping GUI: You type an address in a box on one screen, hit a button, and a new screen pops up with a live readout of results for the ping. While you could keep a list of root nodes, frames, and texts, it certainly wouldn't be as simple as keeping a single list of objects which inherit from the Tkiner.Tk class.

This post has been edited by Motoma: 02 December 2010 - 10:15 AM

Was This Post Helpful? 1
  • +
  • -

#3 Steven Smith  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 45
  • Joined: 17-March 08

Re: tkinter : needless object-oriented programming ?

Posted 02 December 2010 - 01:41 PM

OOP is a tool used to more easily convert blocks of code into building blocks for other code, as well as provide a framework for keeping multiple instances of code running without manually tracking them. (among other things)

Your tutorial writers probably recognize both the value of OOP and the fact that ultimately, you will want to code more complex things than their examples, but perhaps reuse the code. For this reason, they gave the example as a class that could be more easily integrated into other code.

In my own experience, I tend to write "scripts" without OOP, and "programs" with it. Scripts to me being a block of code intended to perform some task quickly and then end... programs being the more interactive type of code that might have a GUI, etc. I can tell you there have been countless times where I start scripting out a piece of code and realize that I really would be better suited to have made it a class instead of just a series of functions. I'd add layer after layer of glue code to add the extra functionality I need before I scrap it. I have never written a class and gone back saying... this is too complex, let me convert it.

It's worth mentioning that even in your example, you are using OOP. You are benefitting from the OOP layout of the TKinter class when you define your frame class for example. If you truely wanted code without OOP, you would have to basically include all the TKinter source code in your program, which would run into the hundreds if not thousands of lines.

In short, OOP isn't required in this particular quick example, but don't write it off. You'll probably find it very useful eventually, and it is worth familiarizing yourself with.

Steve
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1