WX.EVT_LISTBOX_d单击单击此处可从数据库获取不同的信息

2024-04-25 07:54:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,当我确实点击了列表框,在我的程序中,我确实点击了列表框就会出现一段视频,并对视频进行了解释。我在数据库中制作了一个示例“name_link”,它将出现在列表框中。expl1,expl2,expl3。每个名字的链接都有不同的信息。然而,每次我点击其中一个名字链接时,就会出现这种情况,视频只出现在Video3上,系统永远不会显示关于视频的解释。当我点击名字链接视频1视频2新兴或总是视频3。我被困在这部分了。在

单击期间的此部分:

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)

数据库如下:

^{pr2}$

Tags: posselfevent数据库size视频链接def
2条回答

我试着创造一些这样的代码。在

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

其工作为视频,视频可以播放。但对于列数据库中的信息解释,它没有显示出来。我想从tb4的解释打印栏得到信息。@nepix32公司

有几个问题:

  1. 您只需要绑定wx.EVT_LISTBOX_DCLICK一次。当事件被触发时,您要读出的是双击时选择了which item (^{})。使用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()

相关问题 更多 >