Reputation: 1521
I am trying to get some value from a wx dialog which I run from my pygame app. I am totally new to wxPython and never did any OOP, so I need some help. Now it is kind of working, at least the dialog shows up and closes successfully. But I don't have an idea what I need to add to my dialog methods to handle the input from the dialog after it is closed. I place the relevant code here. It uses examples from http://zetcode.com/wxpython/dialogs/
My dial.py
module :
import wx
class OptionsDial(wx.Dialog):
def __init__(self, *args, **kw):
super(OptionsDial, self).__init__(*args, **kw)
self.InitUI()
self.SetSize((300, 200))
self.SetTitle("Import options")
def InitUI(self):
pnl = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
sb = wx.StaticBox(pnl, label='Import')
sbs = wx.StaticBoxSizer(sb, orient=wx.VERTICAL)
sbs.Add(wx.RadioButton(pnl, label='PNG', style=wx.RB_GROUP))
sbs.Add(wx.RadioButton(pnl, label='TIFF'))
sbs.Add(wx.RadioButton(pnl, label='JPEG'))
pnl.SetSizer(sbs)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
okButton = wx.Button(self, label='Ok')
closeButton = wx.Button(self, label='Close')
hbox2.Add(okButton)
hbox2.Add(closeButton, flag=wx.LEFT, border=5)
vbox.Add(pnl, proportion=1, flag=wx.ALL|wx.EXPAND, border=5)
vbox.Add(hbox2, flag=wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, border=10)
self.SetSizer(vbox)
okButton.Bind(wx.EVT_BUTTON, self.OnClose)
closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
def OnClose(self, e):
self.Destroy()
self.Close()
Note that I had to add self.Close()
line in onClose method, otherwise it doesn't work at all. And in my main program I call it like that (it doesn't read the selected option, now I just try to make it return some different result depending on what button is pressed now and I am stuck):
def options() :
ff = "PNG"
app = wx.App(None)
dlg = dial.OptionsDial(None)
if dlg.ShowModal() == wx.ID_OK :
ff = "JPG"
return ff
Now my function returns "PNG" always. So how do I make it return a value depending on : 1. Selected radio button (actually I need the index of the radio button only) 2. Pressed button (Ok or Close in this case)
Upvotes: 0
Views: 124
Reputation: 3177
Sadly the zetcode wx.Dialog
example at the end of the page is neither pretty smart nor sensible. Do not destroy the Dialog in the dialog.
def OnClose(self, e):
# self.Destroy()
self.Close()
Destroy it afterwards, so that you can readout settings beforehand.
def options() :
ff = "PNG"
app = wx.App(None)
dlg = dial.OptionsDial(None)
res = dlg.ShowModal()
if res == wx.ID_OK :
if dlg.radio_png.GetValue() == True:
# Yay it is PNG!
ff = 'PNG'
# and so on for other possibilities
dlg.Destroy() # now it is time to destroy it
return ff
Of course to be able to readout your Radio selection, you have to make the RBs accessible from outside of the wx.Dialog
instance.
self.radio_png = wx.RadioButton(pnl, label='PNG', style=wx.RB_GROUP))
...
sbs.Add(self.radio_png)
Upvotes: 1