r2e*_*ans 6 python wxwidgets slider
在该文档中进行wx.Slider(wxPython的用于PY2,wxPython的用于PY3,wxWidgets的),这里列出命名为微件控制wx.SL_SELRANGE,定义为允许“用户选择在滑块(仅MSW)的范围内”。对我来说,这指的是双控制,同一轴上的两个滑块以定义低/高范围。我无法让它显示两个控件。
开始使用的基本代码。在这一点上,我什至不担心方法、事件或诸如此类的东西,只是为了展示一些东西。
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# ... sizers and other stuff
self.myslider = wx.Slider(self.notebook_1_pane_2, wx.ID_ANY, 0, -100, 100, style=wx.SL_SELRANGE)
# ...
self.myslider.SetSelection(10, 90)
Run Code Online (Sandbox Code Playgroud)
有了所有这些,我能够让它显示的最多的是一条蓝线,它跨越了我期望的位置。

wxPython 文档都在谈论它,但是用户应该如何能够“选择滑块上的范围”,如此处所示(取自shiny)?

我错过了什么?是否有wx.Slider具有此功能的 wxPython 的合理公开示例?
PS:
系统:win81_64、python-2.7.10、wxPython-3.0.2.0
小智 1
我为此做了一个自定义实现,部分使用了这个问题中的代码。左键单击滑块区域设置范围的左边框,右键单击设置右边框。拖动滑块移动选择。 left_gap并right_gap指示小部件边缘和绘制滑块的实际开始之间的空白空间是多少。正如来源所示,这些必须通过实验来找出。
class RangeSlider(wx.Slider):
def __init__(self, left_gap, right_gap, *args, **kwargs):
wx.Slider.__init__(self, *args, **kwargs)
self.left_gap = left_gap
self.right_gap = right_gap
self.Bind(wx.EVT_LEFT_UP, self.on_left_click)
self.Bind(wx.EVT_RIGHT_UP, self.on_right_click)
self.Bind(wx.EVT_SCROLL_PAGEUP, self.on_pageup)
self.Bind(wx.EVT_SCROLL_PAGEDOWN, self.on_pagedown)
self.Bind(wx.EVT_SCROLL_THUMBTRACK, self.on_slide)
self.slider_value=self.Value
self.is_dragging=False
def linapp(self, x1, x2, y1, y2, x):
proportion=float(x - x1) / (x2 - x1)
length = y2 - y1
return round(proportion*length + y1)
# if left click set the start of selection
def on_left_click(self, e):
if not self.is_dragging: #if this wasn't a dragging operation
position = self.get_position(e)
if position <= self.SelEnd:
self.SetSelection(position, self.SelEnd)
else:
self.SetSelection(self.SelEnd, position)
else:
self.is_dragging = False
e.Skip()
# if right click set the end of selection
def on_right_click(self, e):
position = self.get_position(e)
if position >= self.SelStart:
self.SetSelection(self.SelStart, position)
else:
self.SetSelection(position, self.SelStart)
e.Skip()
# drag the selection along when sliding
def on_slide(self, e):
self.is_dragging=True
delta_distance=self.Value-self.slider_value
self.SetSelection(self.SelStart+delta_distance, self.SelEnd+delta_distance)
self.slider_value=self.Value
# disable pageup and pagedown using following functions
def on_pageup(self, e):
self.SetValue(self.Value+self.PageSize)
def on_pagedown(self, e):
self.SetValue(self.Value-self.PageSize)
# get click position on the slider scale
def get_position(self, e):
click_min = self.left_gap #standard size 9
click_max = self.GetSize()[0] - self.right_gap #standard size 55
click_position = e.GetX()
result_min = self.GetMin()
result_max = self.GetMax()
if click_position > click_min and click_position < click_max:
result = self.linapp(click_min, click_max,
result_min, result_max,
click_position)
elif click_position <= click_min:
result = result_min
else:
result = result_max
return result
Run Code Online (Sandbox Code Playgroud)