472,347 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,347 software developers and data experts.

Tkinter Confusion

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
4 2064
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
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
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
Many thanks to all.

Feb 18 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gary Richardson | last post by:
Is there a simple way of causing the size of a canvas to change as the window is resized when using the Grid geometry manager? Setting...
4
by: steve | last post by:
In a nutshell, my problem is that I am getting this runtime error, and I have no clue why. Please bear in mind its my first python program: ...
8
by: Harlin Seritt | last post by:
I have the following script. Two widgets call the same function. How can I tell inside of the called function which button called it?: def...
1
by: cm012b5105 | last post by:
Hello i am fairly new to python, I have written an interactive programme a small example of it is here. s = raw_input ("Do you have any children?...
2
by: JyotiC | last post by:
Hi, I am making a gui, in which specifiction are taken from user, which are wriiten to a file. and that file will bw executed. finalstr is a...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has...
4
by: Kevin Walzer | last post by:
I'm trying to manage user preferences in a Tkinter application by initializing some values that can then be configured from a GUI. The values are...
8
by: rahulnag22 | last post by:
I have created a button widget with a button click binding. The button initially has a state=disabled. I can see the greyed out version of the...
4
by: njwilson23 | last post by:
I'm having trouble with tkinter on a new installation of Python (2.6), built with the framework option from source that was downloaded from...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.