1

I'm new to Python, but not to programming. In this tutorial, the author initializes a constructor in his class like so:

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent) 

I understand it, and you do the same in C#, and Java. However, in this tutorial, why doesn't the author do the same? He only initializes the application. Like this:

app = Tk()

In what situation would I do the second over the first? Which in your opinion is better?

  • See also e.g. http://stackoverflow.com/q/7300072/3001761 – jonrsharpe May 30 '15 at 13:10
  • Thank, but the guy in the video never really does all the code in the link you provided before app = Tk() why? –  May 30 '15 at 13:12
  • I haven't watched the video, but presumably they don't need/want the level of control that provides. Subclassing the Tkinter components gives you lots of additional options, but if you don't actually *need* those options (e.g. you're just creating a simple UI) it's probably not worth the effort. Alternatively, as it's a tutorial, they may be choosing to focus on other aspects of Tkinter development without getting involved with the OOP side. Only they can answer for sure, though! – jonrsharpe May 30 '15 at 13:14
  • Thanks, I get it now. –  May 30 '15 at 13:14

2 Answers2

0

PERSONALLY I always do the second, because when you make longer applications it's just much easier. Here is an example of using the second one:

from Tkinter import *

app = Tk()    #app is a variable, so in the future, you can define Tk() as anything you want. Most people call it root
frame = Frame(app)
frame.pack()

bottomframe = Frame(app)
bottomframe.pack( side = BOTTOM )

redbutton = Button(frame, text="Red", fg="red")
redbutton.pack( side = LEFT)

greenbutton = Button(frame, text="Brown", fg="brown")
greenbutton.pack( side = LEFT )

bluebutton = Button(frame, text="Blue", fg="blue")
bluebutton.pack( side = LEFT )

blackbutton = Button(bottomframe, text="Black", fg="black")
blackbutton.pack( side = BOTTOM)

app.mainloop()

Good luck. And sorry i couldn't answer your question exactly

0

It's important to emphasize that it's not the same as Java. In Java if subclass contractor doesn't specify call to specific contractor of the super class the compiler will automatically call the accessible no-args constructor in the super class.

In Python that won't happen. If you won't specify call for super class contractor then there won't be any call. Therefore in your code the first line in the subclass contractor is the call for super class contractor:

Tkinter.Tk.__init__(self,parent) 

It's important to check and understand this issues when learning new language.

  • I understand it's not the same as Java, but what I mean is, both Java and C# have constructors in the GUI application, however, as you pointed out, given certain conditions, they preform differently. –  May 30 '15 at 13:43