我正在使用wx在python中使用enthought canopy(版本1.5.5,python 2.7)创建一个简单的GUI(或者我认为)。这是程序的代码: `
import wx
import sqlite3
import webbrowser
import wx.lib.inspection
class database():
def __init__(self):
self.db = sqlite3.connect("MarkIt")
self.cursor = self.db.cursor()
def execute(self, query):
#fetching data resulted from a query
self.cursor.execute(query)
return self.cursor.fetchall()
def delete(self, lst):
for i in range(lst):
self.cursor.execute("delete from bookmarks where name=?", lst[i])
def edit(self, lst):
self.cursor.execute("update bookmarks set name=?, link=?, category=? where id=?",(lst[1],lst[2],lst[3],lst[0],))
def filter(self):
return
def sort(self):
return
def add(self):
return
def close(self):
self.db.close()
########################################################################
class RandomPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
########################################################################
class window(wx.Frame):
def __init__(self, parent, title):
#Main window layout
super(window, self).__init__(parent, title = title,size = (800,600))
self.dbase = database()
self.toolbar()
self.panel = wx.Panel(self)
self.create_main_panel()
#self.divider()
self.Show()
#++++++++++TOOLBAR+++++++++#
def toolbar(self):
#Creating the toolbar
tb = self.CreateToolBar()
delete = tb.AddLabelTool(wx.ID_ANY, 'Delete', wx.Bitmap('icons/Delete-48.png'))
edit = tb.AddLabelTool(wx.ID_ANY, 'Edit', wx.Bitmap('icons/edit.png'))
filtertool = tb.AddLabelTool(wx.ID_ANY, 'Filter', wx.Bitmap('icons/filter.png'))
sort = tb.AddLabelTool(wx.ID_ANY, 'Sort', wx.Bitmap('icons/sort.png'))
add = tb.AddLabelTool(wx.ID_ANY, 'Add', wx.Bitmap('icons/add.png'))
tb.Realize()
self.Bind(wx.EVT_TOOL, self.OnDelete, delete)
self.Bind(wx.EVT_TOOL, self.OnEdit, edit)
self.Bind(wx.EVT_TOOL, self.OnFilter, filtertool)
self.Bind(wx.EVT_TOOL, self.OnSort, sort)
self.Bind(wx.EVT_TOOL, self.OnAdd, add)
#TOOLBAR FUNCTIONS#
def OnDelete(self, event):
return
def OnEdit(self, event):
return
def OnFilter(self, event):
return
def OnSort(self, event):
return
def OnAdd(self, event):
return
#--------------------------#
#++++++++++SCREEN DIVIDER++++++++++#
def create_main_panel(self):
#Splitting remaining screen (left -> categories; right -> members of selected categories)
mainbox = wx.SplitterWindow(self)
leftpanel = RandomPanel(mainbox)
rightpanel = RandomPanel(mainbox)
query = """SELECT * FROM categories"""
data = self.dbase.execute(query)
wx.lib.inspection.InspectionTool().Show()
categories = wx.ListBox(leftpanel, -1, size=(-1,-1), choices = [row[1] for row in data])
self.Bind(wx.EVT_LISTBOX, lambda event: self.onListBox(event, rightpanel), categories)
#self.onBox("Recipes", rightpanel)
mainbox.SplitVertically(leftpanel, rightpanel)
mainbox.SetSashGravity(0.12)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(mainbox, 1, wx.EXPAND)
self.SetSizer(sizer)
#self.panel.SetSizer(mainbox)
######---TEST FUNCTION---########
def onBox(self, category, parent):
query = "SELECT book_name, link FROM bookmarks WHERE category=\'"+category+"\'"
data = self.dbase.execute(query)
#book_name = name of the bookmarks; link = eventual link of bookmark
i=0
vbox2=wx.BoxSizer(wx.VERTICAL)
print('a')
for row in data:
#row[0] = book_name
#row[1] = link
#linebox = wx.BoxSizer(wx.HORIZONTAL)
btn = wx.Button(parent,-1,label=row[0])
#self.Bind(wx.EVT_BUTTON,lambda event:self.onClick(event, row[1]), btn)
#self.mainbox.Add(btn)
vbox2.Add(btn,1,wx.ALIGN_LEFT, 20)
#self.SetSizer(linebox)
#self.vbox2.Add(linebox, 1, wx.CENTER)
i+=1
parent.SetSizer(vbox2)
##################################
def onListBox(self, event, parent):
category = event.GetEventObject().GetStringSelection()
query = "SELECT book_name, link FROM bookmarks WHERE category=\'"+category+"\'"
data = self.dbase.execute(query)
#book_name = name of the bookmarks; link = eventual link of bookmark
i=0
vbox2=wx.GridBagSizer()
btn=[]
linebox=[]
print('a')
for row in data:
#row[0] = book_name
#row[1] = link
btn.append(wx.Button(parent,-1,label=row[0]))
#self.Bind(wx.EVT_BUTTON,lambda event:self.onClick(event, row[1]), btn)
#self.mainbox.Add(btn)
#vbox2.Add(btn,1,wx.ALIGN_RIGHT, 20)
#self.SetSizer(linebox)
i+=1
for j in range(len(btn)):
linebox.append(wx.BoxSizer(wx.HORIZONTAL))
linebox[j].Add(btn[j], 1, wx.CENTER)
vbox2.Add(linebox[j], (0,int(j)), (1,1), 1, wx.CENTER)
parent.SetSizer(vbox2)
def onClick(self, event, link):
webbrowser.open(link)
if __name__ == "__main__":
app = wx.App(False)
frame = window(None, "MarkIt")
app.MainLoop()
`
当前的问题是,当我单击ListBox的一个元素时,它会在同一位置创建所有按钮,无论是sizer,除非我指定按钮的确切位置(这在以后的行中会很不方便) 。创建按钮的功能是:
def onListBox(self, event, parent):
category = event.GetEventObject().GetStringSelection()
query = "SELECT book_name, link FROM bookmarks WHERE category=\'"+category+"\'"
data = self.dbase.execute(query)
#book_name = name of the bookmarks; link = eventual link of bookmark
i=0
vbox2=wx.GridBagSizer()
btn=[]
linebox=[]
print('a')
for row in data:
#row[0] = book_name
#row[1] = link
btn.append(wx.Button(parent,-1,label=row[0]))
#self.Bind(wx.EVT_BUTTON,lambda event:self.onClick(event, row[1]), btn)
#self.mainbox.Add(btn)
#vbox2.Add(btn,1,wx.ALIGN_RIGHT, 20)
#self.SetSizer(linebox)
i+=1
for j in range(len(btn)):
linebox.append(wx.BoxSizer(wx.HORIZONTAL))
linebox[j].Add(btn[j], 1, wx.CENTER)
vbox2.Add(linebox[j], (0,int(j)), (1,1), 1, wx.CENTER)
parent.SetSizer(vbox2)
感谢您的帮助和时间!