我是python编程的新手,也是这个论坛的新手。我正在准备一个有2个组合框的wxpython程序(GUI)。第一个是团队成员,第二个是团队成员。所有数据都存储在一个访问数据库中,我成功地获取并将团队列表添加到团队组合框中。
现在我希望团队成员组合框加载团队组合框的文本更改事件。为此我使用EVT_TEXT方法绑定它并调用自定义方法首先从团队组合框中获取团队名称,然后运行查询并将相关团队成员加载到第二个组合框。在很大程度上,我是成功的,但团队成员正在加入团队组合框本身。
请先查看以下代码:
import wx, pyodbc
class MyFrame(wx.Frame):
def __init__(self, Parent, Title):
super(MyFrame, self).__init__(None, title=Title, size=(400,400))
#creating the panel in which all widgets will be stored/created.
#self.panel1 = wx.Panel(self, pos=(1,1),size=(382,100),style=wx.RAISED_BORDER)
#now creating the first Label inside the panel
a = teamData()
#rows = a.runQueryEmpList("Mama Badi")
TmLst = a.runQueryTmList()
abc=[]
for r in TmLst:
abc.append(r.Team_Name)
#static box for the employee details
self.myvbfrm = wx.StaticBox(self,-1,label="Employee Detail:-",pos=(1,1),size=(380,98))
#items for the employee details
LblName = wx.StaticText(self.myvbfrm,-1, 'Team Name:-',(8,20))
LblName2 = wx.StaticText(self.myvbfrm,-1, 'Employee Name:-',(8,50))
# team combobox
TeamList = myComboBox(self.myvbfrm,(200,20),(100,50))
TeamList.addItem(abc)
# employee combobox
#EmpList = wx.ComboBox(self.myvbfrm,-1,"",(200,50),(100,50))
EmpList = myComboBox(self.myvbfrm,(200,50),(100,50))
TeamList.Bind(wx.EVT_TEXT, TeamList.addTeamMember)
#creating the panel in which all widgets will be stored/created.
self.panel2 = wx.Panel(self, pos=(10,175),size=(365,150),style=wx.RAISED_BORDER)
#now creating the first Label inside the panel
myFont = wx.Font(8,wx.DECORATIVE, wx.NORMAL, wx.BOLD,True)
myLblFont = wx.Font(12,wx.DECORATIVE, wx.NORMAL, wx.BOLD,True)
#Lbl.SetFont(myFont)
#Lbl.SetForegroundColour((0,0,0))
#Lbl.SetBackgroundColour((204,204,204))
self.SetBackgroundColour((237,237,237))
LblName.SetFont(myLblFont)
LblName2.SetFont(myLblFont)
# now under this __init__ method i will also initiate the method which
# will create the MenuBar and the Menu Items.
self.AddMenu()
# now i will also have to create a method named as AddMenu so that
# can be run (which will add the Menu Items.
def AddMenu(self):
myMenuBar = wx.MenuBar()
myFileMenu = wx.Menu()
myEditMenu = wx.Menu()
exitBtn = myFileMenu.Append(wx.ID_EXIT, 'Exit App')
editBtn = myEditMenu.Append(wx.ID_MOVE_FRAME, 'Move App')
myMenuBar.Append(myFileMenu, '&File')
myMenuBar.Append(myEditMenu, '&Edit')
self.SetMenuBar(myMenuBar)
self.Centre()
self.Show()
class teamData():
def runQueryEmpList(self,Tname):
self.Tname = Tname
# set up some constants
myDb = 'D:\\Python projects\\Python programs\\trial.accdb'
DRV = '{Microsoft Access Driver (*.mdb)}'
PWD = 'pw'
# connect to db
conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s' % (myDb))
cur = conn.cursor()
# run a query and get the results
SQL = 'SELECT Emp_Name FROM Table1 WHERE Team = ?'
return cur.execute(SQL, self.Tname).fetchall()
cur.close()
conn.close()
def runQueryTmList(self):
# set up some constants
myDb = 'D:\\Python projects\\Python programs\\trial.accdb'
DRV = '{Microsoft Access Driver (*.mdb)}'
PWD = 'pw'
# connect to db
conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s' % (myDb))
cur = conn.cursor()
# run a query and get the results
SQL = 'SELECT Team_Name FROM Teams'
return cur.execute(SQL).fetchall()
cur.close()
conn.close()
class myComboBox(wx.ComboBox):
def __init__(self, parent, lstposition, lstsize):
super(myComboBox, self).__init__(parent, -1, value="", pos=lstposition, size=lstsize)
def addItem(self, Lst=[]):
self.Lst = Lst
for el in self.Lst:
self.Append(el)
def addTeamMember(self,extra):
self.extra = extra
a = teamData()
rows = a.runQueryEmpList(self.GetValue())
Emp_List=[]
for r in rows:
self.Append(r.Emp_Name)
class myFrm(wx.StaticBox):
def __init__(self, parent, lblstring, position, BxSize):
super(myFrm, self).__init__(parent,-1,label=lblstring,pos=position,size=BxSize)
def borderColor(self):
self
app = wx.App()
frame = MyFrame(None, 'Clysdale Activity Tracker')
app.MainLoop()
我明白为什么他们会被添加到团队组合框中(因为我正在追加自己的项目)。我应该如何在addTeamMember()
方法中引用其他组合框?
答案 0 :(得分:1)
通常,您将绑定到框架或面板中的处理程序,并将组合框定义为类变量:
self.TeamList = myComboBox(self.myvbfrm,(200,20),(100,50))
self.TeamList.Bind(wx.EVT_TEXT, self.onUpdate)
self.EmpList = myComboBox(self.myvbfrm,(200,50),(100,50))
然后你可以在处理程序中附加项目:
def onUpdate(self, event):
team = self.TeamList.GetValue()
if team == "Tigers":
self.EmpList.append(some_list)
如果你想保持自己的工作方式,那么首先创建EmpList对象,然后在创建TeamList时将其作为另一个参数传递给myComboBox类。然后,您可以在TeamList实例中附加到它。以下内容应该有效:
import wx, pyodbc
class MyFrame(wx.Frame):
def __init__(self, Parent, Title):
super(MyFrame, self).__init__(None, title=Title, size=(400,400))
#creating the panel in which all widgets will be stored/created.
#self.panel1 = wx.Panel(self, pos=(1,1),size=(382,100),style=wx.RAISED_BORDER)
#now creating the first Label inside the panel
a = teamData()
#rows = a.runQueryEmpList("Mama Badi")
TmLst = a.runQueryTmList()
abc=[]
for r in TmLst:
abc.append(r.Team_Name)
#static box for the employee details
self.myvbfrm = wx.StaticBox(self,-1,label="Employee Detail:-",pos=(1,1),size=(380,98))
#items for the employee details
LblName = wx.StaticText(self.myvbfrm,-1, 'Team Name:-',(8,20))
LblName2 = wx.StaticText(self.myvbfrm,-1, 'Employee Name:-',(8,50))
# employee combobox
#EmpList = wx.ComboBox(self.myvbfrm,-1,"",(200,50),(100,50))
EmpList = EmpComboBox(self.myvbfrm,(200,50),(100,50))
# team combobox
TeamList = TeamComboBox(self.myvbfrm,(200,20),(100,50), EmpList)
TeamList.addItem(abc)
TeamList.Bind(wx.EVT_TEXT, TeamList.addTeamMember)
#creating the panel in which all widgets will be stored/created.
self.panel2 = wx.Panel(self, pos=(10,175),size=(365,150),style=wx.RAISED_BORDER)
#now creating the first Label inside the panel
myFont = wx.Font(8,wx.DECORATIVE, wx.NORMAL, wx.BOLD,True)
myLblFont = wx.Font(12,wx.DECORATIVE, wx.NORMAL, wx.BOLD,True)
#Lbl.SetFont(myFont)
#Lbl.SetForegroundColour((0,0,0))
#Lbl.SetBackgroundColour((204,204,204))
self.SetBackgroundColour((237,237,237))
LblName.SetFont(myLblFont)
LblName2.SetFont(myLblFont)
# now under this __init__ method i will also initiate the method which
# will create the MenuBar and the Menu Items.
self.AddMenu()
# now i will also have to create a method named as AddMenu so that
# can be run (which will add the Menu Items.
def AddMenu(self):
myMenuBar = wx.MenuBar()
myFileMenu = wx.Menu()
myEditMenu = wx.Menu()
exitBtn = myFileMenu.Append(wx.ID_EXIT, 'Exit App')
editBtn = myEditMenu.Append(wx.ID_MOVE_FRAME, 'Move App')
myMenuBar.Append(myFileMenu, '&File')
myMenuBar.Append(myEditMenu, '&Edit')
self.SetMenuBar(myMenuBar)
self.Centre()
self.Show()
class myComboBox(wx.ComboBox):
def __init__(self, parent, lstposition, lstsize):
super(myComboBox, self).__init__(parent, -1, value="", pos=lstposition, size=lstsize)
def addItem(self, Lst=[]):
self.Lst = Lst
for el in self.Lst:
self.Append(el)
def addTeamMember(self,extra):
raise NotImplementedError
########################################################################
class EmpComboBox(myComboBox):
""""""
pass
########################################################################
class TeamComboBox(myComboBox):
""""""
#----------------------------------------------------------------------
def __init__(self, parent, lstposition, lstsize, empComboBox=None):
"""Constructor"""
super(myComboBox, self).__init__(parent, -1, value="", pos=lstposition, size=lstsize)
self.empComboBox = empComboBox
def addTeamMember(self,extra):
self.extra = extra
a = teamData()
rows = a.runQueryEmpList(self.GetValue())
for r in rows:
self.empComboBox.Append(r.Emp_Name)
class myFrm(wx.StaticBox):
def __init__(self, parent, lblstring, position, BxSize):
super(myFrm, self).__init__(parent,-1,label=lblstring,pos=position,size=BxSize)
def borderColor(self):
self
app = wx.App()
frame = MyFrame(None, 'Clysdale Activity Tracker')
app.MainLoop()
基本上,您需要子类化您的ComboBox类并覆盖addTeamMember方法。由于我无法运行您的代码,我无法测试上面的示例,但我相信它会起作用(尽管可能需要在这里或那里进行调整)。