当我点击列表框时遇到问题,在我的程序中,我点击了列表框会出现一个视频并解释视频。我在数据库中做了一个样本" name_link"它将出现在列表框中。 expl1,expl2,expl3。每个name_link都有不同的信息。但是,每当我点击其中一个name_link时,就会出现这种情况,视频只显示Video3,系统从不显示有关视频的说明。当我点击name_link 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
答案 0 :(得分:0)
有几个问题:
您只想绑定wx.EVT_LISTBOX_DCLICK
一次。触发事件时,您希望在双击时读取which item (GetSelection
)。使用GetSelections
进行多项选择。
错误的缩进:在内循环(self.player…
)和return self.Bind…
应该在最里面的循环中。正如现在所写,它将绑定到最后一个元素。如第1点所述,这不是以任何方式完成的方式。
在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()
答案 1 :(得分:0)
我尝试创建这样的代码。
def playVideo(self, evt, temp):
self.player.Load(evt.GetClientObject())
self.Play(True)
self.pjs.Clear()
self.pjs.AppendText(evt.GetClientObject())
它的视频工作,视频可以播放。但是对于来自列数据库的信息说明,它不显示。我想从tb4中的列解释打印信息。 @ nepix32