473,549 Members | 2,699 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter and dialogs

I'm trying unsuccessfully to do something in Tk that I though would be
easy. After Googling this all day, I think I need some help. I am
admittedly very novice with Tk (I started with it yesterday), so I am
sure I am overlooking something simple.

The basic idea is that my application will consist of a series of modal
dialogs, that are chained together in "wizard" fashion. There will be
some processing in between dialogs, but for the most part, the user will
do some interaction with each dialog and then click "Next" or
"Previous". My thinking was that the main (root) Tk window would be
hidden and that each dialog would be modal and child to that hidden
window. Is this a reasonable way to do this in Tkinter?

I grabbed the "Dialog" class from effbot's site figuring that was good
starting point. I did modify it somewhat to convert to the grid layout
manager, based on advice in the New Mexico Tech Tkinter Reference (by
John Shipman).

When I run this (Ubuntu 6.06), I get no windows, not even the root Tk one.

Any ideas???



#!/usr/bin/env python
from Tkinter import *

root = None

class Dialog(Toplevel ):

def __init__(self, parent, title = None):
Toplevel.__init __(self, parent)
self.transient( parent)

if title:
self.title(titl e)

print repr(parent)
self.parent = parent
self.result = None

#body = Frame(self)
#self.initial_f ocus = self.body(body)
#body.pack(padx =5, pady=5)

self.initial_fo cus = self.body(self)

self.buttonbox( )

self.bind("<Ret urn>", self.ok)
self.bind("<Esc ape>", self.cancel)


if not self.initial_fo cus:
self.initial_fo cus = self

self.protocol(" WM_DELETE_WINDO W", self.cancel)

self.geometry(" +%d+%d" % (parent.winfo_r ootx()+50,
parent.winfo_ro oty()+50))

self.initial_fo cus.focus_set()

self.wait_windo w(self)
# construction hooks

def body(self, master):
# create dialog body. return widget that should have
# initial focus. this method should be overridden

def buttonbox(self) :
# add standard button box. override if you don't want the
# standard buttons

box = Frame(self)

w = Button(box, text="OK", width=10, command=self.ok ,
w.pack(side=LEF T, padx=5, pady=5)
w = Button(box, text="Cancel", width=10, command=self.ca ncel)
w.pack(side=LEF T, padx=5, pady=5)

self.bind("<Ret urn>", self.ok)
self.bind("<Esc ape>", self.cancel)


# standard button semantics

def ok(self, event=None):
if not self.validate() :
self.initial_fo cus.focus_set() # put focus back

self.update_idl etasks()

def cancel(self, event=None):
# put focus back to the parent window
self.parent.foc us_set()

# command hooks

def validate(self):
print "validate"
return True # override

def apply(self):
print "apply"
pass # override
class WelcomeDialog(D ialog):
def body(self, master):

Label(master, text="First:"). grid(row=0, sticky=W)
Label(master, text="Second:") .grid(row=1, sticky=W)

self.e1 = Entry(master)
self.e2 = Entry(master)

self.e1.grid(ro w=0, column=1)
self.e2.grid(ro w=1, column=1)

self.cb = Checkbutton(mas ter, text="Hardcopy" )
self.cb.grid(ro w=2, columnspan=2, sticky=W)

self.w1 = Button(master, text="OK", width=10, command=self.ok ,
#w.pack(side=LE FT, padx=5, pady=5)
self.w1.grid(ro w=3, column=0)
self.w2 = Button(master, text="Cancel", width=10,
command=self.ca ncel)
#w.pack(side=LE FT, padx=5, pady=5)
self.w2.grid(ro w=3, column=1)
def apply(self):
print "apply"
first = int(self.e1.get ())
second = int(self.e2.get ())
self.result = first, second

def buttonbox(self) :
def show_ui():
welcome = WelcomeDialog(r oot, "test1")

global root
root = Tk()
root.after_idle (show_ui)
Jul 6 '06 #1
3 3596
dwelch91 wrote:
I get no windows, not even the root Tk one.
no time to dig deeper into this right now, but the culprit is probably a
combination of this line

self.transient( parent)

and the after_idle approach to create the Dialog (Toplevel) window. the
transient call tells Tkinter to make the new dialogue window dependent
on the parent (which is the root Tk window in this case), but that
window hasn't been created yet when you get to transient, so Tk ends up
waiting for something that obviously never happens...

as a workaround, you can either remove the transient call, or insert an
explicit root.update() before the root.after_idle () call.
root = None
global root
root = Tk()
unrelated to your question: "global" is used in a local scope (a
function or method) to indicate that a variable that you assign to is a
global variable. for the full story, see



Jul 6 '06 #2
In <44********@use net01.boi.hp.co m>, dwelch91 wrote:
When I run this (Ubuntu 6.06), I get no windows, not even the root Tk one.

Any ideas???


global root
root = Tk()
root.after_idle (show_ui)
What is this `after_idle()` call supposed to do? The main loop isn't
running yet. If you just call `show_ui()` you should get some windows.

The ``global`` is unnecessary by the way. It even generates a warning:

test.py:0: SyntaxWarning: name 'root' is assigned to before global

Marc 'BlackJack' Rintsch
Jul 6 '06 #3
dwelch91 wrote:
I'm trying unsuccessfully to do something in Tk that I though would be
It is easy.
The basic idea is that my application will consist of a series of modal
dialogs, that are chained together in "wizard" fashion.
Didn't have time to get into the code you posted. Just think that the
solution I use might be of some help.

#!/usr/bin/env python
import Tkinter
class PrevNextPane(Tk inter.Frame):
def __init__(self,m aster):
self.master=mas ter
Tkinter.Frame._ _init__(self,ma ster)

self.prvBtn=Tki nter.Button(sel f,text="Prev",c ommand=self.do_ prev).grid(row= 0,column=0)

self.nxtBtn=Tki nter.Button(sel f,text="Next",c ommand=self.do_ next).grid(row= 0,column=1)
def do_next(self):
self.master.pan eNumber+=1
self.master.dis playPane()

def do_prev(self):
self.master.pan eNumber-=1
self.master.dis playPane()

class Pane0(Tkinter.F rame):
def __init__(self,m aster):
Tkinter.Frame._ _init__(self,ma ster)
for i in range(5):
Tkinter.Entry(s elf).grid(row=i ,column=0)

class Pane1(Tkinter.F rame):
def __init__(self,m aster):
Tkinter.Frame._ _init__(self,ma ster)
for i in range(5):
Tkinter.Label(s elf,text="Label %s"% i).grid(row=i,c olumn=0)

class Pane2(Tkinter.F rame):
def __init__(self,m aster):
Tkinter.Frame._ _init__(self,ma ster)
for i in range(5):
Tkinter.Button( self,text="BtnP ane2-%s"%
i).grid(row=i,c olumn=0)

class Wizard(Tkinter. Tk):
def __init__(self):
Tkinter.Tk.__in it__(self)
self.topPane=No ne
self.prevNextPa ne=PrevNextPane (self).pack(sid e=Tkinter.BOTTO M)
self.paneNumber =0
self.displayPan e()
def displayPane(sel f):
if self.topPane!=N one:
self.topPane.fo rget()
self.topPane=gl obals()["Pane%s"% self.paneNumber](self)
except KeyError:
self.topPane.pa ck(side=Tkinter .TOP)

if __name__=="__ma in__":

Jul 8 '06 #4

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

Similar topics

by: DoubleM | last post by:
Hi, I'm running Python2.3.3c1 on Mandrake 9.1 The following code is designed to bring up a window with a button labeled "popup". Clicking on the popup buttons triggers a secondary window with a button labeled "ok". The second window is supposed to be modal - it should not be possible to reset the focus back to the first window or close...
by: Peter Moscatt | last post by:
Ok.... I am pretty new to Python (as you may have gathered from previous posts). So now it time for another one of my ridiculous questions.... :-) When using 'Tkinter', what is used as the base window to work from, meaning what widget do I use to place all other widgets onto to create a custom dialog ? Pete
by: Sean McIlroy | last post by:
I'd like to have a function f such that, when f is invoked, a Tk window w is presented in which a number of variables can be modified, and f returns the values that are indicated by the relevant menus/checkbuttons/etc at the time w gets closed. I've tried various ways of doing this, without success. Any assistance would be greatly...
by: Ron Adam | last post by:
I want to be able to easily create reusable shapes in Tkinter and be able to use them in mid level dialogs. So after some experimenting I've managed to get something to work. The following does pretty much what I need, but I think it can be improved on. So could anyone take a look and let me know what you think? Some of the things I want...
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 I am hoping someone knows what I am doing wrong: 1) Pressing the Settings.. Button multiple times, brings up many instances of the Settings...
by: bartonc | last post by:
You can find the original author of the script by ggling " Py2Exe version 6.3 setup" The cool thing about this is that it calls py2exe, just in case you're uncomfortable with the command line. I had to search hi and lo for the example of how to exclude Tkinter. Coming soon: scipy includes and numpy include that gets rid of complaints of missing...
by: bg_ie | last post by:
Hi, I'm in the process of writing some code and noticed a strange problem while doing so. I'm working with PythonWin 210 built for Python 2.5. I noticed the problem for the last py file processed by this script, where the concerned tmp file is only actually written to when PythonWin is closed. In other words, after I run this script, one of...
by: msunderwd | last post by:
Having a problem with "compiling" a Tkinter/python program using py2exe (and pyinstaller, for that matter)... I have several dialogs that are derived from the tkSimpleDialog.Dialog class. These work just fine if run through the interpreter. When I "compile" this with py2exe, I don't see any errors, and when I execute the resulting...
by: devnew | last post by:
i am trying out tkinter to make a gui..i want to have a frame with an embedded file explorer next to a 'open directory' label..i tried out FileDialog and tkFileDialog methods but they open as pop up dialogs.. how do i make this packed into the frame the way button and other widgets can be packed? i am a newbie to tkinter.. any help greatly...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.