Hello.
I am trying to implement a wxSlider control, but am running into difficulty.
The slider's GetValue() method returns the default value rather than the
value that reflects the slider position. (I'm using wxPython 2.4.2.4 on
Win2K.) That renders the slider rather useless.
I've attached a code sample below.
···
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
"""This file implements the OptionsSettings class, which defines the
main Options > Settings Dialog Box and values. """
__author__ = 'David Woods <dwoods@wcer.wisc.edu>'
from wxPython import wx
import gettext
_ = gettext.gettext
class OptionsSettings(wx.wxDialog):
""" Options > Settings Dialog Box """
def __init__(self, parent):
""" Initialize the Program Options Dialog Box """
wx.wxDialog.__init__(self, parent, -1, _("Transana Settings"),
wx.wxDefaultPosition, wx.wxSize(500, 240), style=wx.wxCAPTION |
wx.wxSYSTEM_MENU | wx.wxTHICK_FRAME)
# Define a wxNotebook for the Tab structure
lay = wx.wxLayoutConstraints()
lay.top.SameAs(self, wx.wxTop, 0)
lay.left.SameAs(self, wx.wxLeft, 0)
lay.right.SameAs(self, wx.wxRight, 0)
lay.bottom.SameAs(self, wx.wxBottom, 27)
notebook = wx.wxNotebook(self, -1, size=self.GetSizeTuple()) #,
style=wx.wxNB_FIXEDWIDTH)
notebook.SetConstraints(lay)
panelTranscriber = wx.wxPanel(self, -1,
size=notebook.GetSizeTuple())
# Add the Video Setback widgets to the Transcriber Settings Tab
lblTranscriptionSetback = wx.wxStaticText(panelTranscriber, -1,
_("Transcription Setback: (Auto-rewind interval for Ctrl-S)"),
style=wx.wxST_NO_AUTORESIZE)
lay = wx.wxLayoutConstraints()
lay.top.SameAs(panelTranscriber, wx.wxTop, 10)
lay.left.SameAs(panelTranscriber, wx.wxLeft, 10)
lay.width.AsIs()
lay.height.AsIs()
lblTranscriptionSetback.SetConstraints(lay)
choiceList = ['0', '2', '5']
self.transcriptionSetback = wx.wxChoice(panelTranscriber, -1,
size=wx.wxSize(35, 25), choices=choiceList)
self.transcriptionSetback.SetSelection(1)
lay = wx.wxLayoutConstraints()
lay.top.Below(lblTranscriptionSetback, 3)
lay.left.SameAs(panelTranscriber, wx.wxLeft, 10)
lay.width.AsIs()
lay.height.AsIs()
self.transcriptionSetback.SetConstraints(lay)
# Add the Video Speed Slider widget to the Transcriber Settings Tab
lblVideoSpeed = wx.wxStaticText(panelTranscriber, -1, _("Video
Playback Speed"), style=wx.wxST_NO_AUTORESIZE)
lay = wx.wxLayoutConstraints()
lay.top.Below(self.transcriptionSetback, 10)
lay.left.SameAs(panelTranscriber, wx.wxLeft, 10)
lay.width.AsIs()
lay.height.AsIs()
lblVideoSpeed.SetConstraints(lay)
self.videoSpeed = wx.wxSlider(panelTranscriber, -1, 10, 1, 20,
style=wx.wxSL_HORIZONTAL | wx.wxSL_AUTOTICKS)
lay = wx.wxLayoutConstraints()
lay.top.Below(lblVideoSpeed, 3)
lay.left.SameAs(panelTranscriber, wx.wxLeft, 10)
lay.right.SameAs(panelTranscriber, wx.wxRight, 10)
lay.height.AsIs()
self.videoSpeed.SetConstraints(lay)
self.lblVideoSpeedSetting = wx.wxStaticText(panelTranscriber, -1,
'1.0')
lay = wx.wxLayoutConstraints()
lay.top.Below(self.transcriptionSetback, 10)
lay.right.SameAs(panelTranscriber, wx.wxRight, 10)
lay.width.AsIs()
lay.height.AsIs()
self.lblVideoSpeedSetting.SetConstraints(lay)
self.videoSpeed = wx.wxSlider(panelTranscriber, -1, 10, 1, 20,
style=wx.wxSL_HORIZONTAL | wx.wxSL_AUTOTICKS)
lay = wx.wxLayoutConstraints()
lay.top.Below(lblVideoSpeed, 3)
lay.left.SameAs(panelTranscriber, wx.wxLeft, 10)
lay.right.SameAs(panelTranscriber, wx.wxRight, 10)
lay.height.AsIs()
self.videoSpeed.SetConstraints(lay)
wx.EVT_SCROLL(self, self.OnScroll)
lblVideoSpeedMin = wx.wxStaticText(panelTranscriber, -1, _("0.1"),
style=wx.wxST_NO_AUTORESIZE)
lay = wx.wxLayoutConstraints()
lay.top.Below(self.videoSpeed, 2)
lay.left.SameAs(self.videoSpeed, wx.wxLeft, 0)
lay.width.AsIs()
lay.height.AsIs()
lblVideoSpeedMin.SetConstraints(lay)
lblVideoSpeed1 = wx.wxStaticText(panelTranscriber, -1, _("1.0"),
style=wx.wxST_NO_AUTORESIZE)
lay = wx.wxLayoutConstraints()
lay.top.Below(self.videoSpeed, 2)
# The "center" (1.0) position is 47% (9 / 19) of the way between 0.1
and 2.0. However, 48% looks better on Windows.
lay.left.PercentOf(self.videoSpeed, wx.wxWidth, 48)
lay.width.AsIs()
lay.height.AsIs()
lblVideoSpeed1.SetConstraints(lay)
lblVideoSpeedMax = wx.wxStaticText(panelTranscriber, -1, _("2.0"),
style=wx.wxST_NO_AUTORESIZE)
lay = wx.wxLayoutConstraints()
lay.top.Below(self.videoSpeed, 2)
lay.right.SameAs(self.videoSpeed, wx.wxRight, 0)
lay.width.AsIs()
lay.height.AsIs()
lblVideoSpeedMax.SetConstraints(lay)
panelTranscriber.SetAutoLayout(wx.true)
panelTranscriber.Layout()
notebook.AddPage(panelTranscriber, _("Transcriber Settings"),
wx.true)
# Define the buttons on the bottom of the form
lay = wx.wxLayoutConstraints()
lay.top.Below(notebook, 3)
lay.width.Absolute(85)
lay.left.SameAs(self, wx.wxRight, -268)
lay.bottom.SameAs(self, wx.wxBottom, 0)
btnOK = wx.wxButton(self, -1, _('OK'))
btnOK.SetConstraints(lay)
lay = wx.wxLayoutConstraints()
lay.top.Below(notebook, 3)
lay.width.Absolute(85)
lay.left.RightOf(btnOK, 6)
lay.bottom.SameAs(self, wx.wxBottom, 0)
btnCancel = wx.wxButton(self, -1, _('Cancel'))
btnCancel.SetConstraints(lay)
# Attach events to the Buttons
wx.EVT_BUTTON(self, btnOK.GetId(), self.OnOK)
wx.EVT_BUTTON(self, btnCancel.GetId(), self.OnCancel)
self.Layout()
self.SetAutoLayout(wx.true)
self.ShowModal()
def OnOK(self, event):
""" Method attached to the 'OK' Button """
# TODO: We need to save the values set here, and load them on
Program Startup.
self.Close()
def OnCancel(self, event):
""" Method attached to the 'Close' Button """
self.Close()
def OnScroll(self, event):
print "OnScroll = %g", self.videoSpeed.GetValue()
self.lblVideoSpeedSetting.SetLabel("%1.1f" %
(float(self.videoSpeed.GetValue()) / 10))
class MyApp(wx.wxApp):
def OnInit(self):
frame = OptionsSettings(None)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()