Problems with current value of wx.SpinCtrl and EVT_SPIN

B

blackno666

Hello,

I am new to Python/wxPython and am experiencing first problems. I have
a dialog which includes a SpinCtrl and a Slider. I want the Slider to
affect the SpinCtrl and vice versa
(http://wiki.wxpython.org/index.cgi/ChallengeDemos#Part1).

The code I wrote does, however, not work correctly. The event EVT_SPIN
is never handled (even though I wrote a handler), and in the handler of
EVT_SCROLL_CHANGED I do not obtain the modified value, but the value
before the control has changed.

Any feedback would be greatly appreciated.
To clarify my problem, here's the code (Python2.4, wxPython 2.6) :

import wx

class AppFrame(wx.Frame):
def OnSpinGeneral(self, event):
b = wx.MessageBox("never reached")

def OnSpin(self, event):
# I tried to use Skip so that the changing of the value would
take place before
# I invoke GetValue(), however this has no effect
event.Skip()

print self.spin.GetValue()
self.slider.SetValue(self.spin.GetValue())

def OnScrollChanged(self, event):
self.spin.SetValue(self.slider.GetValue())

def __init__(self):
wx.Frame.__init__( self,
None, -1, "",
#size=FRAME_SIZE,
style=wx.DEFAULT_FRAME_STYLE )
self.sizer = wx.BoxSizer( wx.VERTICAL )



self.spin = wx.SpinCtrl(self, min = 0, max = 50)
self.slider = wx.Slider(self, 1, 6, 0, 50)

self.sizer.Add(self.spin)
self.sizer.Add(self.slider)

self.Bind(wx.EVT_SCROLL_CHANGED, self.OnScrollChanged,
self.slider)
self.Bind(wx.EVT_SPIN_UP, self.OnSpin, self.spin)
self.Bind(wx.EVT_SPIN_DOWN, self.OnSpin, self.spin)
self.Bind(wx.EVT_SPIN, self.OnSpinGeneral, self.spin)

self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.Show(1)

app = wx.PySimpleApp()
frame = AppFrame()
app.MainLoop()
 
P

Peter Hansen

blackno666 said:
I am new to Python/wxPython and am experiencing first problems. I have
a dialog which includes a SpinCtrl and a Slider. I want the Slider to
affect the SpinCtrl and vice versa
(http://wiki.wxpython.org/index.cgi/ChallengeDemos#Part1).

The code I wrote does, however, not work correctly. The event EVT_SPIN
is never handled (even though I wrote a handler), and in the handler of
EVT_SCROLL_CHANGED I do not obtain the modified value, but the value
before the control has changed.

Does it *appear* to work? That is, if you run it, does the slider move
if you click on one of the spinner buttons, and does the spinner value
change when you move the slider (and release the button)? Because
that's what happens for me, so I'm not sure what it is that you expected
it to do... (wxPython 2.6.1.0 on WinXP SP2)

-Peter
 
B

blackno666

Yes, it "works". However buggy.

When the slider is set to 0 and the up button is pressed in the
SpinCtrl, the value in the SpinCtrl will be 1, but the slider will not
move. There's also a discrepancy between the value displayed in the
SpinCtrl and the value output by

print self.spin.GetValue().

When playing with this code I also noticed that I was not able change
the behaviour of the up and down buttons. I expected that if I modify
OnSpin in the following way:

def OnSpin(self, event):
pass

That pressing the spin buttons would have no effect. However, the value
is still increased or decreased....
 
B

blackno666

Just found a solution to the problem:

when using wx.EVT_SPINCTRL instead of wx.EVT_SPIN_UP, wx.EVT_SPIN_DOWN
or wx.EVT_SPIN the program behaves correctly.

wxWidget documentation for wxSpinCtrl states that "You may also use the
wxSpinButton event macros, however the corresponding events will not be
generated under all platforms"; I ran the program on WinXP SP2.
However, the events seem to be handled (at least EVT_SPIN_UP,
wx.EVT_SPIN_DOWN). So I do not understand why handling these events
leads to a buggy behaviour.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,178
Latest member
SusanaHam4

Latest Threads

Top