By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,264 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Tkinter Confusion

P: n/a
Everything I've read about Tkinter says you create your window and
then call its mainloop() method. But that's not really true. This is
enough to launch a default window from the console:
>>>from Tkinter import *
foo = Tk()
Google's great, but it has no truth meter. Do I inherit from Frame? Or
is that a big mistake. (Both positions repeated frequently.) Do I use
Tk() or toplevel()? (Support for both and if a cogent explanation of
the differences exists, I didn't find it.)

Here's the application. I'm creating a visual parser for my beginner's
language. The starting position is a list of Statement objects, each
being a list of Token objects. The statement is presented as a list of
buttons with abbreviated token types ('Con_Int' for a CONSTANT_INTEGER
token). Click the button and a dialog-like info display pops up with
all the details about the token. During parsing, each recognition
condenses tokens into productions, shortening the Statement. (Example:
three Token buttons are replaced by one Addition production button.)
An application window provides for stepping through the parsing and
provides utility commands such as "Close all those token windows I've
got lying all over".

Much less complex than IDLE, but GvR and cohorts seem to understand
what's really going on. I don't. Help appreciated.
Feb 17 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sun, 17 Feb 2008 11:36:25 -0800, MartinRinehart wrote:
Everything I've read about Tkinter says you create your window and
then call its mainloop() method. But that's not really true. This is
enough to launch a default window from the console:
>>>>from Tkinter import *
foo = Tk()
Depends on the platform if this shows a window.
Do I use Tk() or toplevel()? (Support for both and if a cogent
explanation of the differences exists, I didn't find it.)
`Tk` is the main window, `Toplevel` for additional windows. Don't create
several `Tk` instances. That usually causes very weird side effects.

Ciao,
Marc 'BlackJack' Rintsch
Feb 17 '08 #2

P: n/a
On Feb 17, 12:36*pm, MartinRineh...@gmail.com wrote:
Everything I've read about Tkinter says you create your window and
then call its mainloop() method. But that's not really true. This is
enough to launch a default window from the console:
>>from Tkinter import *
foo = Tk()
You shouldn't care what happens in an interactive python session. In
fact, you can take years off your life trying to figure out why the
output of an interactive session is different from the output of a
python program. Here is how to create a basic window with one widget:

import Tkinter as tk

root = tk.Tk()

label = tk.Label(root, text='hello world')
label.pack() #makes widget visible

root.mainloop()
Adding in some more details:
import Tkinter as tk

root = tk.Tk()
root.geometry('600x400')
root.config(background='red')

label = tk.Label(root, text='hello world', background='gray')
label.pack() #makes widget visible

root.mainloop()
Google's great, but it has no truth meter. Do I inherit from Frame?
A frame is used to group widgets. You can have multiple frames each
containing a group of widgets. That will allow you to place the group
as a whole at a specific location in the window. Here's how to use
frames to organize widgets:

import Tkinter as tk

root = tk.Tk()
root.geometry('600x400')
root.config(background='red')

frame1 = tk.Frame(root)
label1 = tk.Label(frame1, text='hello world', background='gray')
label2 = tk.Label(frame1, text='goodbye', background='gray')

label1.pack() #makes label visible
label2.pack() #makes label visible
frame1.pack(side=tk.BOTTOM) #makes frame visible

frame2 = tk.Frame(root)
label3 = tk.Label(frame2, text='yes', background='yellow')
label4 = tk.Label(frame2, text='no', background='yellow')
label5 = tk.Label(frame2, text='maybe', background='yellow')

label3.pack()
label4.pack()
label5.pack()
frame2.pack(side=tk.TOP)

frame3 = tk.Frame(root)
label6 = tk.Label(frame3, text='a', background='blue')
label7 = tk.Label(frame3, text='b', background='blue')
label8 = tk.Label(frame3, text='c', background='blue')

label6.pack()
label7.pack()
label8.pack()
frame3.pack(side=tk.LEFT)

root.mainloop()

Do I use
Tk() or toplevel()? (Support for both and if a cogent explanation of
the differences exists, I didn't find it.)
Tk() for you first window; Toplevel() for any additional windows you
want to open:
import Tkinter as tk

root = tk.Tk()
root.geometry('300x200+50+50') #+x+y positions window
root.config(background='red')

label = tk.Label(root, text='hello world', background='gray')
label.pack()

window2 = tk.Toplevel()
window2.geometry('300x200+400+50')

root.mainloop()

Feb 18 '08 #3

P: n/a
MartinRineh...@gmail.com wrote:
Do I use
Tk() or toplevel()? (Support for both and if a cogent explanation of
the differences exists, I didn't find it.)
If you close the window created by Tk(), the program terminates. If
you close a window created by Toplevel() only that window closes. The
Tk() window remains open and the program continues to execute.
Feb 18 '08 #4

P: n/a
Many thanks to all.

Feb 18 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.