wxpython:如何将其他地方创建的网格填充到Notebook标签中?

1 投票
1 回答
1336 浏览
提问于 2025-04-18 12:32

现在我有一个用wxpython做的图形界面,它可以成功地从一个.csv文件中提取数据,填充到一个wx网格对象里,然后在一个新窗口中显示出来。我还成功地让我的主窗口以笔记本的形式显示一些信息。我的目标是让我的程序运行时,主页面的一个标签里包含和我之前制作的窗口一样的填充网格。现在让我困惑的问题是,网格的创建和填充(这两件事是分开的)是在不同的类中完成的,而且这些类是在一个不同的(但已导入的)本地文件里。此外,我在程序中运行的下面这段代码出现了AttributeError: 'TabPanel'对象没有属性'con',这让我觉得有道理,但我不太明白。

这是不是不可能,还是我漏掉了什么(我说得清楚吗)?下面是我猜测相关的代码。(为了方便,这里类和构造函数的缩进不太正确。)非常感谢!

标签/笔记本:

class TabPanel(wx.Panel):    
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        txtOne = wx.Panel(Employee.EmployeeViewAllFrame(self).show())

        self.sizer.Add(txtOne, 0, wx.ALL , 50)

        self.SetSizer(self.sizer)


class NotebookDemo(wx.Notebook):
    def __init__(self, parent):
        wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=
                         wx.BK_DEFAULT
                         #wx.BK_TOP 
                         #wx.BK_BOTTOM
                         #wx.BK_LEFT
                         #wx.BK_RIGHT
                         )

        # Create the first tab and add it to the notebook
        tabOne = TabPanel(self)
        tabOne.SetBackgroundColour("BLUE")
        self.AddPage(tabOne, "Main")

        # Create and add the second tab
        tabTwo = TabPanel(self)
        self.AddPage(tabTwo, "Employees")

        # Create and add the third tab
        self.AddPage(TabPanel(self), "Tasks")

网格/窗口:

class empGrid(wx.grid.Grid):
    def __init__(self, parent):
        wx.grid.Grid.__init__(self,parent,size = (1500,1000))
        self.SetDefaultCellOverflow(False)
        self.EnableEditing(False)
        self.EnableDragGridSize(False)
        self.EnableDragRowSize(False)
        self.EnableDragColSize(False)
        self.grid = gridlib.Grid(panel2)
        self.CreateGrid(TOTALEMPLOYEES, 12)
        self.SetColLabelValue(0, "Name")

        self.SetColSize(0, 200)
        self.SetColLabelValue(1, "Grade")
        self.SetColLabelValue(2, "NGID")
        self.SetColLabelValue(3, "MyID")
        self.SetColLabelValue(4, "Skillset1")
        self.SetColSize(4, 110)
        self.SetColLabelValue(5, "Skillset2")
        self.SetColSize(5, 110)
        self.SetColLabelValue(6, "SME")
        self.SetColLabelValue(7, "Org")
        self.SetColLabelValue(8, "Manager")
        self.SetColSize(8, 125)
        self.SetColLabelValue(9, "OfficePriority")
        self.SetColSize(9, 165)
        self.SetColLabelValue(10, "Comments")
        self.SetColSize(10, 200)
        self.SetColLabelValue(11, "Loan?")
        #self.AutoSizeColumns(setAsMin=True)


class EmployeeViewAllFrame(wx.Frame): 
  def __init__(self, parent):
    wx.Frame.__init__(self, parent,title = 'View All Employees',size=(wx.EXPAND,wx.EXPAND))
    self.currentid = ''
    self.currentrow = 0
    self.parent = parent
    #Declare all panels
    self.panelMain = wx.Panel(self,size = (1500, 1000)) 
    self.panelSide = wx.Panel(self,size = (wx.EXPAND, 1000)) 
    #self.panelTitle = wx.Panel(self,size = (1000,30))      
    #self.buttonPanel = wx.Panel(self)  

    self.buttonExit = wx.Button(self.panelSide, label="exit")
    self.buttonExit.Bind(wx.EVT_BUTTON, self.OnExitButton)

    cur = self.parent.con.cursor()
    cur.execute("SELECT * FROM " + EMPLOYEETABLE + " ORDER BY Name;")
    self.rows = cur.fetchall()#Load all the employees into self.rows and organize by name

    self.employeenumber = len(self.rows) #Going to be the fetched number from the database
    global TOTALEMPLOYEES 
    TOTALEMPLOYEES = self.employeenumber

    #Set up all the column panels and place into an array to be modified
    #self.empGrid = empGrid(self.panelMain)
    self.empGrid = empGrid(EMP.MainWindow.panel2)
    for i in xrange (0, TOTALEMPLOYEES):
        self.empGrid.SetRowLabelValue(i, str(i+1))
        for j in xrange (0,12):
            self.empGrid.SetCellValue(i, j, str(self.rows[i][j]))
            if i % 2 == 1:#if it is odd, change the color to make it easier on the eyes
                self.empGrid.SetCellBackgroundColour(i, j, 'LIGHT BLUE') #JTEST

    self.empGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnGridDoubleClick)
    self.empGrid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_DCLICK, self.OnGridDoubleClickRight)

    #Now do the same thing for the buttons
    text = wx.StaticText(self.panelSide, label = "Double left click an employee to modify fields\n\n\n  Double right click an employee to add a new employee task" , size = (wx.EXPAND,400))        
    sideSizer = wx.BoxSizer(wx.VERTICAL)        
    sideSizer.Add(text)
    sideSizer.Add(self.buttonExit)
    self.panelSide.SetSizer(sideSizer)
    self.panelSide.Layout()


    #Put them all together then display
    displayEmployeeSizer = wx.BoxSizer(wx.VERTICAL)
    displayEmployeeSizer.Add(self.empGrid) #JGRID
    self.panelMain.SetSizer(displayEmployeeSizer)
    self.panelMain.Layout() 

    viewEmployeeSizer = wx.BoxSizer(wx.HORIZONTAL)
    #viewEmployeeSizer.Add(self.panelTitle,proportion=0)
    viewEmployeeSizer.Add(self.panelMain,proportion=0)
    viewEmployeeSizer.Add(self.panelSide,proportion = 0)
    #viewEmployeeSizer.Add(self.buttonPanel, proportion=0, flag = wx.ALIGN_CENTER_HORIZONTAL)
    #viewEmployeeSizer.Add(self.buttonExit, proportion = 0, flag = wx.ALIGN_CENTER_HORIZONTAL)

    self.SetSizer(viewEmployeeSizer) #Set the panel size
    #self.buttonPanel.Layout()        
    self.Layout()
    self.Show()

1 个回答

1

你不能把同一个小部件放在两个不同的父级里。相反,你需要在创建独立窗口的时候,先创建一个empGrid的实例,然后在创建笔记本的时候,再创建一个不同的实例。

当你实例化empGrid时,你需要把笔记本的面板或页面作为它的父级。而当你创建窗口时,你要把窗口(或者它的面板)作为父级传给它。

撰写回答