Search code examples
pythonvideohyperlinklistboxwxpython

WX.EVT_LISTBOX_DCLICK click for different video


I have a problem when I do click on the listbox, In my program, I did click on the listbox will appear a video. I made a sample in the database are "name_link" which will appear in the listbox. Video1, Video2, Video3. Of each name_link have different video. However, that happens every time I click on one of these name_link, video that appears only Video3. When I click name_link video1 video2 emerging or always Video3. I am so stuck in this section.

This section during click:

self.opt = wx.ListBox(pan1, -1, pos=(10, 210), size=(480, 250), style= wx.TE_MULTILINE | wx.BORDER_SUNKEN)

def playFile(self, event):
    self.player.Play()

def OnEnter(self, event):
    self.opt.SetLabel(self.PatMatch()) 

def PatMatch(self):
    con = sqlite3.connect('test.db')    
    with con:
        cur = con.cursor()
        for row in cur.execute("Select * From Video"):
            klmt = self.inpt.GetValue()
            if row[1] in klmt.lower():  
                self.opt.Append(row[2])         
               self.player.Load(row[4])
        return self.Bind(wx.EVT_LISTBOX_DCLICK, self.playFile, self.op)

The database like this :

id  word        name_link   explain     link
--- ------      ----------- ---------   --------
1   python      Video1      test        C:\Users\Ihsan\Downloads\Video1.MP4
2   python      Video2      test1       C:\Users\Ihsan\Downloads\Video2.MP4
3   python      Video3      test2       C:\Users\Ihsan\Downloads\Video3.MP4

Solution

  • You should use ClientData for the item in the list. Check the following quick code. By using ClientData you can display different labels for items and store the necessary data for each item.

    import wx
    
    db = [
              {"id": 1, "name_link": "Video 1", "link": "C:\\first_video.mp4"},
              {"id": 2, "name_link": "Video 2", "link": "C:\\second_video.mp4"},
              {"id": 3, "name_link": "Video 3", "link": "C:\\third_video.mp4"},
          ]
    
    class MyFrame(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self, parent=None, title="List of Videos")
    
            self.opt = wx.ListBox(self)
            self.fillOpt()
    
            self.opt.Bind(wx.EVT_LISTBOX_DCLICK, self.startVideo)
            self.Show()
    
        def fillOpt(self):
            for row in db:
                self.opt.Append(row['name_link'], row['link']) # Label, ClientData
    
        def startVideo(self, event):
            wx.MessageBox(parent=self, message=event.GetClientData(), caption="Video")
    
    
    if __name__ == '__main__':
        app = wx.App(False)
        frame = MyFrame()
        app.MainLoop()