2014-02-13 144 views
-1

我是新来的蟒蛇编程和这个论坛以及。我正在准备一个包含2个组合框的wxpython程序(GUI)。首先是团队,第二是团队成员。所有数据都存储在访问数据库中,我成功地将团队列表添加到团队组合框中。将项目添加到wxpython组合框...?

现在我希望团队成员组合框加载团队组合框的文本更改事件。为此,我使用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() 

我理解他们为什么被添加到该团队组合框(因为我Append'ing的项目,以个体经营)。我应该如何参考addTeamMember()方法中的其他组合框?

回答

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方法。由于我无法运行你的代码,我无法测试上面的例子,但我相信它会起作用(尽管它可能需要在这里或那里调整)。

+0

非常感谢你的帮助。你的解决方案为我工作。然而,作为一个新的学习者,我也想知道如何将EmpList对象作为另一个参数传递给myComboBox类。如果你能给我一个小小的代码示例......再次感谢你的知识分享......祝你有个美好的一天:)。 – Premanshu

+0

我加了一个例子,虽然我无法测试它 –