473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 2136
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 sticky='NESW', as in the following code, doesn't work though...
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: "localvariable 'currentAbility' referenced before...
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 say_hello(): print 'hello!' print widget root =...
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? ") if s== 'yes': print "Thats great" elif...
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 string f = open('crossManual.sh','w')...
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 the following problems, probably all related, that...
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 set up as a dict, like so: self.prefs= {...
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 button in the GUI. But If I click on the button it...
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 python.org. I'm running OS 10.4 on a PowerPC G4. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.