I am learning to create simple GUIs with python using Tkinter. I am currently using the python documentation as reference (link).
First thing I am trying to do is understand the sample code there. I am looking at this piece of code in particular, which creates a dialog box with two buttons, one to print something on the console and other to close the program:
from Tkinter import *
class Application(Frame):
def say_hi(self):
print "hi there, everyone!"
def createWidgets(self):
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
self.QUIT.pack({"side": "left"})
self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi
self.hi_there.pack({"side": "left"})
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
I understand that the function createWidgets places all the elements in the screen (which is somehow created with the def init statement), but I don't understand why the main screen is created using that init name. I thought this was a convention, but when I changed 'init' for 'main' the buttons were not placed in the screen, so I don't know if the main screen must always be created using that name or it was I who did something wrong there.
I also don't understand why every function is created using (self) (What does that mean?) or why .mainloop is called at the end (I suppose to start the whole program) when I'm not seeing any kind of main loop defined anywhere in the sample code.
I have a basic understanding of how objects work but I am finding python a bit confusing when dealing with things like these. I have looked at the python documentation on this but it seems a bit vague to me.
Any sort of link to more specific documentation (Something less vague than the python one I'm using) will be greatly appreciated.