Hello,
I am including at the end of this document (is it better as an attachment?) some code
for a small gui dialog. Since I am quite new to this, if anyone has any suggestions
for improvements to the code, bad coding practices, poor gui design, etc... I'd love
to hear it. This list has been very helpful to me so far, and I hope to be able to
return the favor someday when I get good enough to take the training wheels off. :)
The code makes a simple class, with some parameters, some of which are numbers, some
boolean, and one which is either a string or a number depending on context. There is
a dialog class which allows you to edit/change the values, and a wrapper function of
the form: new_params <== wrapper(old_par ams) which calls the dialog, and returns
the updated params instance.
thanks,
Brian Blais
--
-----------------
bblais@bryant.e du
import wx
import copy
class SimulationParam s(object):
def __init__(self):
self.epoch_numb er=500
self.iter_per_e poch=500
self.epoch_per_ display=1
self.random_see d='clock'
self.keep_every _epoch=0
self.save_input _vectors=0
def __repr__(self):
yesno={0:"No",1 :"Yes",True:"Ye s",False:"No "}
s="Epoch Number: %d\n" % self.epoch_numb er
s=s+"Iter Per Epoch: %d\n" % self.iter_per_e poch
s=s+"Epoch Per Display: %d\n" % self.epoch_per_ display
if (isinstance(sel f.random_seed,s tr)):
s=s+"Random Seed: %s\n" % self.random_see d
else:
s=s+"Random Seed: %d\n" % self.random_see d
s=s+"Keep Every Epoch: %s\n" % yesno[self.keep_every _epoch]
s=s+"Save Input Vectors: %s\n" % yesno[self.save_input _vectors]
return(s)
class SimulationParam sDialog(wx.Dial og):
def __init__(self,p arams,parent=No ne):
self.params=par ams
wx.Dialog.__ini t__(self, parent, -1, "Simulation Parameters")
sizer = wx.BoxSizer(wx. VERTICAL)
box = wx.BoxSizer(wx. HORIZONTAL)
label = wx.StaticText(s elf, -1, "Epoch_Number:" )
box.Add(label, 0, wx.ALIGN_CENTRE |wx.ALL, 5)
self.text1 = wx.TextCtrl(sel f, -1, str(params.epoc h_number), size=(80,-1))
box.Add(self.te xt1, 1, wx.ALIGN_CENTRE |wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIG N_CENTER_VERTIC AL|wx.ALL, 5)
box = wx.BoxSizer(wx. HORIZONTAL)
label = wx.StaticText(s elf, -1, "Iterations Per Epoch:")
box.Add(label, 0, wx.ALIGN_CENTRE |wx.ALL, 5)
self.text2 = wx.TextCtrl(sel f, -1, str(params.iter _per_epoch), size=(80,-1))
box.Add(self.te xt2, 1, wx.ALIGN_CENTRE |wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIG N_CENTER_VERTIC AL|wx.ALL, 5)
box = wx.BoxSizer(wx. HORIZONTAL)
label = wx.StaticText(s elf, -1, "Epoch Per Display:")
box.Add(label, 0, wx.ALIGN_CENTRE |wx.ALL, 5)
self.text3 = wx.TextCtrl(sel f, -1, str(params.epoc h_per_display), size=(80,-1))
box.Add(self.te xt3, 1, wx.ALIGN_CENTRE |wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIG N_CENTER_VERTIC AL|wx.ALL, 5)
box = wx.BoxSizer(wx. HORIZONTAL)
label = wx.StaticText(s elf, -1, "Random Seed:")
box.Add(label, 0, wx.ALIGN_CENTRE |wx.ALL, 5)
self.text4 = wx.TextCtrl(sel f, -1, str(params.rand om_seed), size=(80,-1))
box.Add(self.te xt4, 1, wx.ALIGN_CENTRE |wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIG N_CENTER_VERTIC AL|wx.ALL, 5)
self.cb1 = wx.CheckBox(sel f, -1, "Keep Every Epoch")
self.cb1.SetVal ue(params.keep_ every_epoch)
sizer.Add(self. cb1, 1, wx.GROW|wx.ALIG N_CENTRE|wx.ALL , 5)
self.cb2 = wx.CheckBox(sel f, -1, "Save Input Vectors")
self.cb2.SetVal ue(params.save_ input_vectors)
sizer.Add(self. cb2, 1, wx.GROW|wx.ALIG N_CENTRE|wx.ALL , 5)
btnsizer = wx.StdDialogBut tonSizer()
btn = wx.Button(self, wx.ID_OK)
btn.SetHelpText ("The OK button completes the dialog")
btn.SetDefault( )
btnsizer.AddBut ton(btn)
btn = wx.Button(self, wx.ID_CANCEL)
btn.SetHelpText ("The Cancel button cnacels the dialog. (Cool, huh?)")
btnsizer.AddBut ton(btn)
btnsizer.Realiz e()
sizer.Add(btnsi zer, 1, wx.GROW|wx.ALIG N_CENTRE|wx.ALL , 5)
self.SetSizer(s izer)
sizer.Fit(self)
def SetSimParams(pa rams):
new_params=copy .copy(params)
dlg=SimulationP aramsDialog(par ams)
val=dlg.ShowMod al()
if val == wx.ID_OK:
new_params.epoc h_number=eval(d lg.text1.GetVal ue())
new_params.iter _per_epoch=eval (dlg.text2.GetV alue())
new_params.epoc h_per_display=e val(dlg.text3.G etValue())
if (dlg.text4.GetV alue()=='clock' ):
new_params.rand om_seed='clock'
else:
new_params.rand om_seed=eval(dl g.text4.GetValu e())
new_params.keep _every_epoch=dl g.cb1.GetValue( )
new_params.save _input_vectors= dlg.cb2.GetValu e()
print "ok"
else:
print "cancel"
dlg.Destroy()
return(new_para ms)
if __name__ == '__main__':
app = wx.PySimpleApp( 0)
params=Simulati onParams()
new_params=SetS imParams(params );
print params
print new_params
app.MainLoop()
I am including at the end of this document (is it better as an attachment?) some code
for a small gui dialog. Since I am quite new to this, if anyone has any suggestions
for improvements to the code, bad coding practices, poor gui design, etc... I'd love
to hear it. This list has been very helpful to me so far, and I hope to be able to
return the favor someday when I get good enough to take the training wheels off. :)
The code makes a simple class, with some parameters, some of which are numbers, some
boolean, and one which is either a string or a number depending on context. There is
a dialog class which allows you to edit/change the values, and a wrapper function of
the form: new_params <== wrapper(old_par ams) which calls the dialog, and returns
the updated params instance.
thanks,
Brian Blais
--
-----------------
bblais@bryant.e du
import wx
import copy
class SimulationParam s(object):
def __init__(self):
self.epoch_numb er=500
self.iter_per_e poch=500
self.epoch_per_ display=1
self.random_see d='clock'
self.keep_every _epoch=0
self.save_input _vectors=0
def __repr__(self):
yesno={0:"No",1 :"Yes",True:"Ye s",False:"No "}
s="Epoch Number: %d\n" % self.epoch_numb er
s=s+"Iter Per Epoch: %d\n" % self.iter_per_e poch
s=s+"Epoch Per Display: %d\n" % self.epoch_per_ display
if (isinstance(sel f.random_seed,s tr)):
s=s+"Random Seed: %s\n" % self.random_see d
else:
s=s+"Random Seed: %d\n" % self.random_see d
s=s+"Keep Every Epoch: %s\n" % yesno[self.keep_every _epoch]
s=s+"Save Input Vectors: %s\n" % yesno[self.save_input _vectors]
return(s)
class SimulationParam sDialog(wx.Dial og):
def __init__(self,p arams,parent=No ne):
self.params=par ams
wx.Dialog.__ini t__(self, parent, -1, "Simulation Parameters")
sizer = wx.BoxSizer(wx. VERTICAL)
box = wx.BoxSizer(wx. HORIZONTAL)
label = wx.StaticText(s elf, -1, "Epoch_Number:" )
box.Add(label, 0, wx.ALIGN_CENTRE |wx.ALL, 5)
self.text1 = wx.TextCtrl(sel f, -1, str(params.epoc h_number), size=(80,-1))
box.Add(self.te xt1, 1, wx.ALIGN_CENTRE |wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIG N_CENTER_VERTIC AL|wx.ALL, 5)
box = wx.BoxSizer(wx. HORIZONTAL)
label = wx.StaticText(s elf, -1, "Iterations Per Epoch:")
box.Add(label, 0, wx.ALIGN_CENTRE |wx.ALL, 5)
self.text2 = wx.TextCtrl(sel f, -1, str(params.iter _per_epoch), size=(80,-1))
box.Add(self.te xt2, 1, wx.ALIGN_CENTRE |wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIG N_CENTER_VERTIC AL|wx.ALL, 5)
box = wx.BoxSizer(wx. HORIZONTAL)
label = wx.StaticText(s elf, -1, "Epoch Per Display:")
box.Add(label, 0, wx.ALIGN_CENTRE |wx.ALL, 5)
self.text3 = wx.TextCtrl(sel f, -1, str(params.epoc h_per_display), size=(80,-1))
box.Add(self.te xt3, 1, wx.ALIGN_CENTRE |wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIG N_CENTER_VERTIC AL|wx.ALL, 5)
box = wx.BoxSizer(wx. HORIZONTAL)
label = wx.StaticText(s elf, -1, "Random Seed:")
box.Add(label, 0, wx.ALIGN_CENTRE |wx.ALL, 5)
self.text4 = wx.TextCtrl(sel f, -1, str(params.rand om_seed), size=(80,-1))
box.Add(self.te xt4, 1, wx.ALIGN_CENTRE |wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIG N_CENTER_VERTIC AL|wx.ALL, 5)
self.cb1 = wx.CheckBox(sel f, -1, "Keep Every Epoch")
self.cb1.SetVal ue(params.keep_ every_epoch)
sizer.Add(self. cb1, 1, wx.GROW|wx.ALIG N_CENTRE|wx.ALL , 5)
self.cb2 = wx.CheckBox(sel f, -1, "Save Input Vectors")
self.cb2.SetVal ue(params.save_ input_vectors)
sizer.Add(self. cb2, 1, wx.GROW|wx.ALIG N_CENTRE|wx.ALL , 5)
btnsizer = wx.StdDialogBut tonSizer()
btn = wx.Button(self, wx.ID_OK)
btn.SetHelpText ("The OK button completes the dialog")
btn.SetDefault( )
btnsizer.AddBut ton(btn)
btn = wx.Button(self, wx.ID_CANCEL)
btn.SetHelpText ("The Cancel button cnacels the dialog. (Cool, huh?)")
btnsizer.AddBut ton(btn)
btnsizer.Realiz e()
sizer.Add(btnsi zer, 1, wx.GROW|wx.ALIG N_CENTRE|wx.ALL , 5)
self.SetSizer(s izer)
sizer.Fit(self)
def SetSimParams(pa rams):
new_params=copy .copy(params)
dlg=SimulationP aramsDialog(par ams)
val=dlg.ShowMod al()
if val == wx.ID_OK:
new_params.epoc h_number=eval(d lg.text1.GetVal ue())
new_params.iter _per_epoch=eval (dlg.text2.GetV alue())
new_params.epoc h_per_display=e val(dlg.text3.G etValue())
if (dlg.text4.GetV alue()=='clock' ):
new_params.rand om_seed='clock'
else:
new_params.rand om_seed=eval(dl g.text4.GetValu e())
new_params.keep _every_epoch=dl g.cb1.GetValue( )
new_params.save _input_vectors= dlg.cb2.GetValu e()
print "ok"
else:
print "cancel"
dlg.Destroy()
return(new_para ms)
if __name__ == '__main__':
app = wx.PySimpleApp( 0)
params=Simulati onParams()
new_params=SetS imParams(params );
print params
print new_params
app.MainLoop()
Comment