【问题标题】:Adding items to wxpython combobox...?将项目添加到 wxpython 组合框...?
【发布时间】:2014-02-13 20:38:07
【问题描述】:

我是 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() 方法中的另一个组合框?

【问题讨论】:

    标签: python database combobox wxpython


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

    【讨论】:

    • 非常感谢迈克的帮助。你的解决方案对我有用。但是,作为一个新学习者,我也想知道如何将 EmpList 对象作为另一个参数传递给 myComboBox 类。如果你能给我一个小代码示例...再次感谢您的知识分享...祝您有美好的一天:)。
    • 我添加了一个例子,虽然我无法测试它
    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多