Tkinter Confusion

M

MartinRinehart

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:

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

Marc 'BlackJack' Rintsch

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:

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
 
7

7stud

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:

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()
 
7

7stud

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

petercable

Most of the other questions have already been answered, so I'll tackle
this one:

Google's great, but it has no truth meter. Do I inherit from Frame? Or
is that a big mistake. (Both positions repeated frequently.)

Inherit from Frame if you want your class to be a packable widget. If
you only intend to pack widgets in a supplied container, no need to
subclass Frame...

Pete
 
7

7stud

Most of the other questions have already been answered, so I'll tackle
this one:



Inherit from Frame if you want your class to be a packable widget. If
you only intend to pack widgets in a supplied container, no need to
subclass Frame...

Pete

Whether you should inherit from Frame or not depends on wether you
want to write your code with a procedural style or with an object
oriented style. You can write Tkinter programs with a procedural
style(see previous examples), or you can write them with an object
oriented style:


import Tkinter as tk

class MyLabelFrame(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)

self.config(background='green')

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

label1.pack() #makes label visible
label2.pack() #makes label visible


class MyButtonFrame(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)

self.config(background='black', padx=20, pady=20)

button1 = tk.Button(self, text='suprise 1',
command=self.sayhi)
button2 = tk.Button(self, text='suprise 2',
command=self.saybye)

button1.pack()
button2.pack()

def sayhi(self):
print 'hi'

def saybye(self):
print 'bye'




class MyApp(object):
def __init__(self, *frames):
root = tk.Tk()
root.geometry('600x400')
root.config(background='red')

frame1 = MyLabelFrame(root)
frame2 = MyButtonFrame(root)

frame1.pack(side=tk.TOP)
frame2.pack(side=tk.BOTTOM)

root.mainloop()

app = MyApp()


If you don't know what object oriented programming is, then stick to
the simplicity of the procedural style in the previous examples.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Translater + module + tkinter 1
PyTut: Tkinter #1 0
Tkinter inheritance mess? 3
TKinter 1
tkinter problem with treeview 0
Using Tkinter 1
Is it necessary to call Tk() when writing a GUI app with Tkinter? 30
Tkinter 3

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top