473,721 Members | 2,201 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???

Thanks,

Don


#!/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)

self.grab_set()

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
pass

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 ,
default=ACTIVE)
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)

box.pack()

#
# standard button semantics

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

self.withdraw()
self.update_idl etasks()
self.apply()
self.cancel()

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

#
# 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 ,
default=ACTIVE)
#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) :
pass
def show_ui():
welcome = WelcomeDialog(r oot, "test1")

global root
root = Tk()
root.after_idle (show_ui)
root.mainloop()
Jul 6 '06 #1
3 3607
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

http://pyref.infogami.com/naming-and-binding

</F>

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)
root.mainloop()
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
declaration

Ciao,
Marc 'BlackJack' Rintsch
Jul 6 '06 #3
dwelch91 wrote:
I'm trying unsuccessfully to do something in Tk that I though would be
easy.
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()
try:
self.topPane=gl obals()["Pane%s"% self.paneNumber](self)
except KeyError:
pass
self.topPane.pa ck(side=Tkinter .TOP)

if __name__=="__ma in__":
w=Wizard()
w.mainloop()

Jul 8 '06 #4

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

Similar topics

3
3339
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 the first window without first closing the second. The program works just fine in Windows XP...
5
1607
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
3
9555
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 appreciated. Peace, Sean McIlroy
0
2798
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 to add, but aren't exactly sure how at this time: Nested groups Use tags to be able to change sub...
1
3598
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 Panel. I just want it to bring up one. Is there an easy way to do that?
0
13470
bartonc
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 modules that are fantom errors. # Py2Exe version 6.3 setup file for wxPython GUI programs. #...
44
4995
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 the generated tmp files has a size of 0kB. I then close PythonWin and it is then written to.
5
3443
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 program, it "appears" to work fine until I invoke one of the derived dialogs. Then, I get the "body" of...
2
2745
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 appreciated TIA dn
0
8860
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9376
Oralloy
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9086
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8028
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5994
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2599
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2146
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.