Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

By default, when clicked on the slider, it only scrolls by a Pagesize at a time, not scrolls to the position user clicked.

like image 857
zJay Avatar asked Jul 31 '26 16:07

zJay


1 Answers

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()
like image 57
Fenikso Avatar answered Aug 03 '26 07:08

Fenikso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!