Making a second window with Tkinter

G

greenflame

I have a script that will make a window that shows the text I want
using Tkinter. What I need to do is to make another window popup above
the current window showing other text. I tryed:

-----------
from Tkinter imprt *

root = Tk()

L = Label(root, text="Blah")
L.grid(row=0, column=0)

# Other labels are also here but their inclusion is not so relavent.

root.mainloop()

sub = Tk()

subL = Label(root, text="Blah")
subL.grid(row=0, column=0)

sub.mainloop()
 
J

James Stroud

greenflame said:
I have a script that will make a window that shows the text I want
using Tkinter. What I need to do is to make another window popup above
the current window showing other text. I tryed:

-----------
from Tkinter imprt *

root = Tk()

L = Label(root, text="Blah")
L.grid(row=0, column=0)

# Other labels are also here but their inclusion is not so relavent.

root.mainloop()

sub = Tk()

subL = Label(root, text="Blah")
subL.grid(row=0, column=0)

sub.mainloop()
----------
However, when I ran the script it only showed the second window after I
closed the first one. After looking at the code I guess I can see why.
The problem is that I do not know how to fix the issue. Thank you for
you help.

Use Toplevel().

from Tkinter import *

root = Tk()

def callback(e=None):
t = Toplevel()
Label(t, text='blah').pack()

b = Button(root, text='new top', command=callback)
b.pack()
root.mainloop()

Also, do not do this twice in the same script: Tk().

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
G

greenflame

Ok so I played with your script. Here is a script that more closely
mimics what I would like to do except that the way I make the variable
deckstr will be different. The only thing I am confused about is that
it opens the second window in the beginning and when I click on the
button, it does nothing even after I have closed the other window.

from Tkinter import *
from string import join

root = Tk()

def showdeck(deck):
deckwin = Toplevel()
deckstr = join(deck, "\n")
Label(deckwin, text=deckstr, justify=LEFT, anchor=W,
font="Courier").pack(fill=X)

L = Button(root, text="Show Deck", font="Courier",
command=showdeck(list('zxcvbnm')))
L.pack()

root.mainloop()
 
R

Rob Williscroft

greenflame wrote in @h76g2000cwa.googlegroups.com in comp.lang.python:
Ok so I played with your script. Here is a script that more closely
mimics what I would like to do except that the way I make the variable
deckstr will be different. The only thing I am confused about is that
it opens the second window in the beginning and when I click on the
button, it does nothing even after I have closed the other window.

from Tkinter import *
from string import join

root = Tk()

def showdeck(deck):
deckwin = Toplevel()
deckstr = join(deck, "\n")
Label(deckwin, text=deckstr, justify=LEFT, anchor=W,
font="Courier").pack(fill=X)

L = Button(root, text="Show Deck", font="Courier",
command=showdeck(list('zxcvbnm')))

You made the buttons command option None (which is what
showdeck() returns), you need to make command a function
that calls showdeck(list('zxcvbnm')).

L = Button(root, text="Show Deck", font="Courier",
command= lambda : showdeck(list('zxcvbnm')))
L.pack()

root.mainloop()

lambda's docs online:
http://docs.python.org/ref/lambdas.html

Rob.
 
G

greenflame

What you said about why my code is wrong is still a bit fuzzy but it
worked!
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top