So I have a main menu set up with a button that opens a separate options window. It works fine, except for the fact that I can't find a way to keep the value of my spincontrol widget after the menu is closed. This is my current code:
self.spincontrol = wx.SpinCtrl(self.optionmenupanel, value=OtherClass.variable, size=(60,-1))
On closure of the options menu, this runs:
OtherClass.variable = self.spincontrol.GetValue()
str(OtherClass.variable)
The thing is, I get the error:
TypeError: String or Unicode type required
The variable should be a string, I even used str()
to make sure. So why is this still happening, and is there a different or better way to do this?
I presume your problem stems from the fact that SpinCtrl
can accept either str
/unicode
and int
(accepting text makes sense for the TextCtrl
part, int
makes sense for the "spin" part. However, SpinCtrl.GetValue()
will always return an int
, so it would make sense to treat OtherClass.variable
always as int
.
So best would be to use:
val = OtherClass.variable
self.spinctrl = ...(..., value="", ...) # value only can be ``str`` !
self.spinctrl.SetValue(val) # will accept ``str`` and ``int``
self.spinctrl.Bind(wx.EVT_SPINCTRL, self.on_spin)
def on_spin(self, evt):
res = self.spin.GetValue()
assert isinstance(res, int)
OtherClass.variable = res