From: Alex T. <al...@tw...> - 2005-06-04 20:38:30
|
I was looking for an easy way to get a dialog box which presented some info, and then let the user choose one from a set of options, by clicking on one of a set of buttons. I was sure there was a way provided in Pythoncard (or at least in wxPython) to do this - but can't find it. All I want is to get a dialog like Can I go to the movies tonight ? +-------+ +-------+ +-------+ | Yes | | No | | Maybe | +-------+ +-------+ +-------+ without needing to build a new custom dialog each time. So, I've built a couple of functions to do that for me - see code below to see the kind of thing I want. They could be improved and generalized a bit - but there's still this nagging feeling I'm missing some easy way .... anyone ? If no one tells me about an easy way that already exists, I'll improve these a bit and post them somewhere ... testdialogs.py (needs a resource file, which only needs one button, so I didn't include it !!) ========= #!/usr/bin/python """ __version__ = "$Revision: 1.5 $" __date__ = "$Date: 2004/04/30 16:26:12 $" """ from PythonCard import model import multiButtonDialog class MyBackground(model.Background): def on_initialize(self, event): # if you have any initialization # including sizer setup, do it here pass def on_Button1_mouseClick(self, event): result = multiButtonDialog.multiButtonDialog(self, 'some question', ['OK', 'Not OK', "Cancel"], "Test Dialog Title") print "Dialog result:\naccepted: %s\ntext: %s" % (result.accepted, result.text) result = multiButtonDialog.multiButtonDialog(self, 'Dad, can I go to the movies tonight', \ ['Yes', 'No', 'Maybe', 'Ask me later', 'Ask your mum'], "Movies Dialog Title") print "Dialog result:\naccepted: %s\ntext: %s" % (result.accepted, result.text) if __name__ == '__main__': app = model.Application(MyBackground) app.MainLoop() multiButtonDialog.py ============== """ __version__ = "$Revision: 1.3 $" __date__ = "$Date: 2004/08/12 19:14:23 $" """ # dialog to present a block of text and a number of alternative buttons from PythonCard import model import controls rsrc = {'type':'CustomDialog', 'name':'Template', 'title':'Template', 'position':(176, 176), 'size':(367, 230), 'components': [ ## {'type':'TextArea', {'type':'StaticText', 'name':'Text', 'position':(10, 10), 'size':(341, 123), 'actionBindings':{}, }, {'type':'Button', 'id':5101, 'name':'Button', 'position':(269, 145), 'actionBindings':{}, 'label':'template', }, ] # end components } # end CustomDialog class MyDialog(model.CustomDialog): def __init__(self, parent, txt, buttons): model.CustomDialog.__init__(self, parent, rsrc) self.components.Text.text = txt self.components.Button.visible = False self.components.Button.enabled = False if len(buttons) == 0: buttons = ["OK"] bx, by = self.components.Button.size dx = bx + 20 # check if all buttons will fit in window owx, owy = self.size startx, starty = self.components.Button.position if len(buttons)*dx + bx + 40 > owx: wx = len(buttons)*dx + bx + 40 self.size = (wx, owy) startx, starty = (wx-bx-20, owy-by-30) # AGT - why 30 ?? tsx, tsy = self.components.Text.size self.components.Text.size = (wx-20, tsy) localbuttons = buttons localbuttons.reverse() count = 0 for b in localbuttons: n = controls.copyButton(self, "Button", "Button"+str(count), b) self.components[n.name].position = startx-count*dx, starty self.components[n.name].visible = True self.components[n.name].enabled = True count += 1 self.accepted = False self.text = "" def on_mouseClick(self, event): self.text = event.target.userdata if self.text == "Cancel": self.accepted = False else: self.accepted = True self.Close() def multiButtonDialog(parent, txt, buttons, title=""): rsrc["title"] = title dlg = MyDialog(parent, txt, buttons) result = dlg.showModal() result.accepted = dlg.accepted result.text = dlg.text dlg.destroy() return result controls.py ======= #!/usr/bin/python # controls.py - a set of funcitonsd to manipulate contorls at run time from PythonCard import model from wxPython import wx import string, copy from types import * class copyButton: def __init__(self, Background, ButtonName, newname, Text="new button"): Flds = ['position', 'size', 'backgroundColor', 'foregroundColor', 'command', 'font'] aWidget = Background.components[ButtonName] d = {} d['type'] = aWidget.__class__.__name__ for key in Flds: # attributes # I'm not exactly sure why I have to special-case these tuples if key == 'bitmap': # this should get recreated from the file attribute pass elif key in ['position', 'size']: d[key] = getattr(aWidget, key) elif getattr(aWidget, key) is not None: d[key] = getattr(aWidget, key) d['userdata'] = Text d['label'] = Text d['name'] = newname Background.components[newname] = d self.name = newname -- Alex Tweedly http://www.tweedly.net -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.322 / Virus Database: 267.5.2 - Release Date: 03/06/2005 |