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
  • 370,779
  • 53
  • 539
  • 685

3 Answers3

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.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
  • @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?](http://stackoverflow.com/q/16115378/4279). – jfs Mar 09 '14 at 21:08
  • @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. – anon582847382 Mar 09 '14 at 21:10
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/

Totem
  • 7,189
  • 5
  • 39
  • 66
  • 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. – user3339224 Mar 09 '14 at 19:14
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.

Community
  • 1
  • 1
zmo
  • 24,463
  • 4
  • 54
  • 90