2

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.

  1. What is Enter(master)
  2. e.pack()
  3. Text(root)
  4. text.pack()
  5. mainloop()
  6. root.mainloop()

Thanks !!!

Bryan Oakley
389k53 gold badges585 silver badges741 bronze badges
asked Mar 9, 2014 at 18:51

3 Answers 3

2
  1. Entry(master) defines an entry box inside the parent window master.

  2. 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.

  3. 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.

  4. text.pack() same as 2, only with the Text widget, not an Entry one.

  5. mainloop() Will start the event loop of a previously defined Tk() window. (See below).

  6. 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.

answered Mar 9, 2014 at 19:07
Sign up to request clarification or add additional context in comments.

2 Comments

@user3339224: "allowing it to appear and be interacted with" is incorrect because the previous 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?.
@J.F.Sebastian Because it was a tutorial script from a website, it was designed so individual components to be explained. Furthermore, he asked specifically for those functions on their own. I think my answer is fine for explaining to a beginner, therefore.
0

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:

Thinking in Tkinter

And this site is a great Tkinter reference and introduction to the basics including widgets like Entry and Text:

http://effbot.org/tkinterbook/

answered Mar 9, 2014 at 19:04

1 Comment

Thank you, your answer helped me resolving an issue. You are right, Tk() should be used once but now as my concept got cleared i tried using it and its working well when i used it once. Before it was opening two separate canvases and now its working in the same dialog/canvas.
0

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.

answered Mar 9, 2014 at 19:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.