windows

A

Ajay

hi!

i have two tkinter windows on top of each other. i would like it so that
the user has to complete the fields in the top windows before they can
interact with the window below it. that is, i dont want the user to close
the top window or minimize it or send it into the background.

how would i do it?

thanks

cheers
 
D

Diez B. Roggisch

Ajay said:
hi!

i have two tkinter windows on top of each other. i would like it so that
the user has to complete the fields in the top windows before they can
interact with the window below it. that is, i dont want the user to close
the top window or minimize it or send it into the background.

Look for grab, and what it does.
 
P

Peter Abel

Ajay said:
hi!

i have two tkinter windows on top of each other. i would like it so that
the user has to complete the fields in the top windows before they can
interact with the window below it. that is, i dont want the user to close
the top window or minimize it or send it into the background.

how would i do it?

thanks

cheers

There are documentations which say that Tkinter.Toplevel has
a methode .transient(root) which should make the Toplevel window
transient to its root window.
But I never managed to make it work.(W2K Python 2.2.2).
But here is a hackaround:

import Tkinter

#----------------------
def deiconify(widget):
#----------------------
widget.deiconify()
widget.grab_set()

#----------------------
def withdraw(widget):
#----------------------
widget.grab_release()
widget.withdraw()

# The root window
root=Tkinter.Tk()

# The Toplevel window
tl=Tkinter.Toplevel(root,width=400,height=200,bg='white')

# Withdraw Toplevel, when destroyed
tl.protocol("WM_DELETE_WINDOW",lambda w=tl:withdraw(w))

# Make Toplevel not resizable
tl.resizable(width=False,height=False)

# Default state of Toplevel is withdrawn
tl.withdraw()

# Give Toplevel window a title
tl.title('Toplevel-Window')

# A Button on the root window to make Toplevel window visible
Tkinter.Button(root,text='Toplevel on',command=lambda w=tl:deiconify(w)).pack()

# Start mainloop
root.mainloop()

Regards
Peter
 
P

Peter Abel

Ajay said:
hi!

i have two tkinter windows on top of each other. i would like it so that
the user has to complete the fields in the top windows before they can
interact with the window below it. that is, i dont want the user to close
the top window or minimize it or send it into the background.

how would i do it?

thanks

cheers

There are documentations which say that Tkinter.Toplevel has
a methode .transient(root) which should make the Toplevel window
transient to its root window.
But I never managed to make it work.(W2K Python 2.2.2).
But here is a hackaround:

import Tkinter

#----------------------
def deiconify(widget):
#----------------------
widget.deiconify()
widget.grab_set()

#----------------------
def withdraw(widget):
#----------------------
widget.grab_release()
widget.withdraw()

# The root window
root=Tkinter.Tk()

# The Toplevel window
tl=Tkinter.Toplevel(root,width=400,height=200,bg='white')

# Withdraw Toplevel, when destroyed
tl.protocol("WM_DELETE_WINDOW",lambda w=tl:withdraw(w))

# Make Toplevel not resizable
tl.resizable(width=False,height=False)

# Default state of Toplevel is withdrawn
tl.withdraw()

# Give Toplevel window a title
tl.title('Toplevel-Window')

# A Button on the root window to make Toplevel window visible
Tkinter.Button(root,text='Toplevel on',command=lambda w=tl:deiconify(w)).pack()

# Start mainloop
root.mainloop()

Regards
Peter
 
E

Eric Brunel

Peter said:
There are documentations which say that Tkinter.Toplevel has
a methode .transient(root) which should make the Toplevel window
transient to its root window.
But I never managed to make it work.(W2K Python 2.2.2).

Read tk commands manual:

"""
wm transient window ?master?
If master is specified, then the window manager is informed that window is
a transient window (e.g. pull-down menu) working on behalf of master (where
master is the path name for a top-level window). Some window managers will use
this information to manage window specially.
"""

Nothing is said about which window should have the grab. And in fact, Windows
actually does something when a window is made transient; try this in an
interactive Python session:

You can first notice that the minimize and maximize buttons have disappeared
from the secondary window's title bar, and that the close button has shrinked a
little. Then, if you try to put the main window above the secondary one, you'll
see that you can't. But you can still make the main window active.

So transient is only part of the solution. You also want to set the grab on the
secondary window, and then wait for the secondary window to close. Here is an
example doing that:

--modal.py-------------------------------------------------
from Tkinter import *

root = Tk()

def go():
wdw = Toplevel()
Entry(wdw).pack()
wdw.transient(root)
wdw.grab_set()
root.wait_window(wdw)
print 'done!'

Button(root, text='Go', command=go).pack()
Button(root, text='Quit', command=root.quit).pack()

root.mainloop()
-----------------------------------------------------------

So this is the magical sequence to make a window modal in Tkinter: transient,
grab_set, wait_window.

[snip]

HTH
 

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

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top