在鼠标单击下获取 wxpython 滑块的值 [英] Get wxpython slider's value under mouse click
问题描述
我希望模拟大多数媒体播放器的滑块 - 单击滑块小部件上的任意位置会将视频跳过到该位置.如何在鼠标单击下获取滑块的值并将值设置为该值?
I wish to emulate or most media players' sliders - where clicking anywhere on the slider widget skips the video to that position. How can I get the slider's value under the mouse click and set the value to that?
默认情况下,当点击滑块时,它一次只滚动一个 Pagesize,不会滚动到用户点击的位置.
By default, when clicked on the slider, it only scrolls by a Pagesize at a time, not scrolls to the position user clicked.
推荐答案
以下代码在 Windows XP 中有效,但是我不知道如何通过实验以外的其他方式获得 GAP
常量.GAP
值表示小部件边缘和绘制滑块的实际开始之间的空白空间.
The following code works in Windows XP, however I have no idea how to get the GAP
constant in another way than by experimenting. The GAP
value indicates what is the empty space between edge of the widget and actual start of the drawn slider.
import wx
GAP = 12
class VideoSlider(wx.Slider):
def __init__(self, gap, *args, **kwargs):
wx.Slider.__init__(self, *args, **kwargs)
self.gap = gap
self.Bind(wx.EVT_LEFT_DOWN, self.OnClick)
def linapp(self, x1, x2, y1, y2, x):
return (float(x - x1) / (x2 - x1)) * (y2 - y1) + y1
def OnClick(self, e):
click_min = self.gap
click_max = self.GetSize()[0] - self.gap
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
self.SetValue(result)
e.Skip()
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.slider = VideoSlider(parent=self.panel, size=(300, -1), gap=GAP)
self.slider.Bind(wx.EVT_SLIDER, self.OnSlider)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.slider)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
def OnSlider(self, e):
print(self.slider.GetValue())
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
这篇关于在鼠标单击下获取 wxpython 滑块的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!