WX.EVT_LISTBOX_DCLICK 单击以从数据库中获取不同的信息

WX.EVT_LISTBOX_DCLICK click for get diffrent information from database

当我点击列表框时出现问题,在我的程序中,我点击列表框会出现一个视频并解释视频。我在数据库中做了一个示例 "name_link" ,它将出现在列表框中。 expl1,expl2,expl3。每个 name_link 都有不同的信息。但是,每次我单击其中一个 name_link 时都会发生这种情况,视频只出现 Video3 并且系统从不显示有关视频的解释。当我点击name_link video1 video2 emergeing or always Video3。我被困在这个部分了。

点击期间此部分:

tb4 = wx.StaticText(self, -1, label='explain', pos=(20, 145))
    wx.TextCtrl(self, -1, pos=(80, 145), size=(220, 120))

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)

数据库是这样的:

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

有几个问题:

  1. 您只想绑定 wx.EVT_LISTBOX_DCLICK 一次。当事件被触发时,你想在双击时读出 which item (GetSelection) 被选中。使用 GetSelections 进行多项选择。

  2. 错误缩进:在内部循环中 (self.player…) 并且 return self.Bind… 应该在最内层循环中。正如现在所写的那样,它将一次绑定到最后一个元素。正如第 1 点所写,这无论如何都不是完成的方式。

  3. Bind中,self.op应该是self.opt

请参阅 wxPython demo on the download page 以了解如何明智地使用 ListBox。

编辑:添加了代码示例

import wx

import sqlite3

class play_model(object):
    def __init__(self):
        # all the sqlite init stuff goes here, it is mocked for the sake of the example
        self.conn = sqlite3.connect(':memory:')
        c = self.conn.cursor()
        c.execute('CREATE TABLE video (word text, name_link text, explain text, link text)')

        newrecs = (('python', 'Video1', 'test1', r'C:\video1.MP4'),
                   ('python', 'Video2', 'test2', r'C:\video2.MP4'),
                   ('notpython', 'Video3', 'test3', r'C:\video3.MP4'),
                   ('python', 'Video4', 'test4', r'C:\video4.MP4'),
                   ('python', 'Video5', 'test5', r'C:\video5.MP4'),
                   )
        for tup in newrecs:
            c.execute('INSERT INTO video VALUES (?, ?, ?, ?)', tup)

        self.map_explain = {}

    def get_videos(self, klmt):
        # you want to get videos matching a parameter?
        sqlstr = 'SELECT * FROM video WHERE video.word = (?)'
        c = self.conn.cursor()
        c.execute(sqlstr, (klmt.lower(),))
        res = c.fetchall()

        return res

class playframe(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        pnl = wx.Panel(self, -1)
        self.explain = wx.StaticText(pnl, -1, 'Click in list to show explanation')
        self.klmt = wx.TextCtrl(pnl, -1, '')
        self.srch = wx.Button(pnl, -1, u'Search…')
        self.vids = wx.ListBox(pnl, -1, style=wx.LB_MULTIPLE)

        szmain = wx.BoxSizer(wx.VERTICAL)
        szmain.Add(wx.StaticText(pnl, -1, 'Search for video category:'), 0, wx.EXPAND|wx.ALL, 4)
        szmain.Add(self.klmt, 0, wx.EXPAND|wx.ALL, 4)
        szmain.Add(self.srch, 0, wx.EXPAND|wx.ALL, 4)
        szmain.Add(self.vids, 1, wx.EXPAND|wx.ALL, 4)
        szmain.Add(wx.StaticText(pnl, -1, 'Explanation for video'), 0, wx.EXPAND|wx.ALL, 4)
        szmain.Add(self.explain, 0, wx.EXPAND|wx.ALL, 4)
        pnl.SetSizer(szmain)
        szmain.Fit(self)

class controller(object):
    def __init__(self, app):
        self.model = play_model()
        self.search_results = []
#         print self.model.get_videos('python')
        self.frm = playframe(None, -1, 'test_playframe')
        self.frm.Show()

        self.frm.srch.Bind(wx.EVT_BUTTON, self.on_srch)
        self.frm.vids.Bind(wx.EVT_LISTBOX, self.onvid_dblclick)

    def onvid_dblclick(self, evt):
        sels = evt.GetEventObject().GetSelections()
        for idx in sels:
            self.frm.explain.SetLabel(self.search_results[idx][2])
            print 'play video:', idx, self.search_results[idx]
    def on_srch(self, evt):
        klmt = self.frm.klmt.GetValue()
        # print klmt
        res = self.model.get_videos(klmt)
        if res:
            self.search_results = res
            self.frm.vids.Clear()
            self.frm.vids.AppendItems([row[1] for row in self.search_results])
        else:
            parent = self.frm
            wx.MessageDialog(parent,
                             'Not found in word category: {0}'.format(klmt),
                             'Category not found').ShowModal()

if __name__ == '__main__':
    app = wx.App(redirect=False)
    controller(app)
    app.MainLoop()

我尝试创建一些这样的代码。

def playVideo(self, evt, temp):
    self.player.Load(evt.GetClientObject())
    self.Play(True)
    self.pjs.Clear()
    self.pjs.AppendText(evt.GetClientObject())

为视频工作,视频可以播放。但是对于列数据库解释的信息,它没有显示。我想要来自 explain 列的信息在 tb4 中打印。 @nepix32