Reputation: 92
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.
Thanks !!!
Upvotes: 2
Views: 393
Reputation: 20361
Entry(master)
defines an entry box inside the parent window master
.
e.pack
defines the location in the parent window that e
will appear- if we do not set its geometry it will not appear. There are several geometry managers, but pack()
just fits it in wherever there is space.
Text(root)
defines a simple Text
widget in which... well, you store text in it. It is using the Tk()
window root
as its parent.
text.pack()
same as 2, only with the Text
widget, not an Entry
one.
mainloop()
Will start the event loop of a previously defined Tk()
window. (See below).
root.mainloop()
starts the event loop of the Tk()
window root
. i.e. You're blocking your main program until something happens on the UI that will trigger events.
Upvotes: 2
Reputation: 24812
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.
Upvotes: 0
Reputation: 7349
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
:
http://effbot.org/tkinterbook/
Upvotes: 0