Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python WX.EVT\u列表框\u单击从数据库获取不同信息_Python_Sqlite_Hyperlink_Listbox_Wxpython - Fatal编程技术网

Python WX.EVT\u列表框\u单击从数据库获取不同信息

Python WX.EVT\u列表框\u单击从数据库获取不同信息,python,sqlite,hyperlink,listbox,wxpython,Python,Sqlite,Hyperlink,Listbox,Wxpython,我有一个问题,当我点击列表框,在我的程序中,我点击列表框会出现一个视频,并解释有关视频。我在数据库中做了一个示例,它是“name\u link”,将出现在列表框中。解释1,解释2,解释3。每个名称的链接都有不同的信息。然而,每当我点击其中一个名链接时,就会出现这种情况,该视频只出现在Video3上,而系统从不显示关于视频的解释。当我单击名称链接video1 video2或总是Video3时。我被困在这一部分了 单击此部分时: tb4 = wx.StaticText(self, -1, label

我有一个问题,当我点击列表框,在我的程序中,我点击列表框会出现一个视频,并解释有关视频。我在数据库中做了一个示例,它是“name\u link”,将出现在列表框中。解释1,解释2,解释3。每个名称的链接都有不同的信息。然而,每当我点击其中一个名链接时,就会出现这种情况,该视频只出现在Video3上,而系统从不显示关于视频的解释。当我单击名称链接video1 video2或总是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

有几个问题:

  • 您只需要一次绑定
    wx.EVT\u列表框\u DCLICK
    。触发事件时,双击时选择了要读取的事件。使用
    GetSelections
    进行多项选择

  • 错误缩进:在内部循环(
    self.player…
    )中,使用
    返回self.Bind…
    ,它应该在最内部的循环中。现在编写时,它将绑定一次到最后一个元素。如第1点所述,无论如何,这不是它的实现方式

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

  • 请参阅,了解如何明智地使用列表框

    编辑:添加了代码示例

    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())
    

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

    我正在尝试创建一些这样的代码。”def playVideo(self,evt,temp):'self.player.Load(evt.GetClientObject())self.Play(True)self.pjs.Clear()self.pjs.AppendText(evt.GetClientObject())如果用于视频,视频可以播放。但是对于来自列数据库的信息解释,它并没有显示出来。我想要tb4中解释栏的信息@NEPIX32你最初的问题和你对问题的回答让我的大脑受伤。无论如何,编辑我的答案以显示我会做什么(尝试猜测您在程序中尝试做什么)。