I am not sure if this is the right place to ask such a question but being a new wannabe Python developer i want any one to please explain Python`s Tkinter small program to me.
Program:
from Tkinter import *
master = Tk()
e = Entry(master)
e.pack()
root = Tk()
text = Text(root)
e.focus_set()
text.pack()
def callback():
text.insert(INSERT, e.get())
b = Button(master, text="Start", width=10, command=callback)
b.pack()
mainloop()
root.mainloop()
This program is working perfectly but following the documentation here i am still confused about few things.
- What is Enter(master)
- e.pack()
- Text(root)
- text.pack()
- mainloop()
- root.mainloop()
Thanks !!!
3 Answers 3
Entry(master)defines an entry box inside the parent windowmaster.e.packdefines the location in the parent window thatewill appear- if we do not set its geometry it will not appear. There are several geometry managers, butpack()just fits it in wherever there is space.Text(root)defines a simpleTextwidget in which... well, you store text in it. It is using theTk()windowrootas its parent.text.pack()same as 2, only with theTextwidget, not anEntryone.mainloop()Will start the event loop of a previously definedTk()window. (See below).root.mainloop()starts the event loop of theTk()windowroot. i.e. You're blocking your main program until something happens on the UI that will trigger events.
2 Comments
mainloop() blocks until both top-level windows are closed. Also there is no need to call Tk() twice, see Tkinter example code for multiple windows, why won't buttons load correctly?.If you think about master, from the line:
master = Tk()
as representing a kind of master 'cavity' into which all subsequent widgets will fit
e = Entry(master)
This line assigns to e, an instance of the Entry widget class. This widget will go inside master.
e.pack()
this line actually 'packs' e inside master
with text = Text(root) and text.pack(), essentially the same thing is happening, except that root is now the 'cavity' into which the Text widget is being 'packed'. I have never seen two different variables be assigned to Tk() in a program like this before, but I don't have a whole lot of experience, so this might be alright.
I suggest that for these and all the rest you read the following, it really helped me to understand tkinter:
And this site is a great Tkinter reference and introduction to the basics including widgets like Entry and Text:
1 Comment
What is Enter(master)
as per the documnetation you're giving:
Entry(master=None, **options) (class) [#]
A text entry field.
so basically, you're binding a Text entry field to the UI, i.e. the Tk() instance you assigned to the master variable.
e.pack()
when you "pack" a widget, you actually bind it to the geometry manager that takes care of positioning it into the UI.
Text(root)
now you do the same with a multiline input
text.pack()
and again pack it to the geometry manager.
mainloop()
root.mainloop()
now you're calling the event loops for both UIs, i.e. you're blocking your main program until something happens on the UI that will trigger events. The only thing is that the second call will not work until the first window is closed, as the first one is blocking the main application thread until you close the window.