簡體   English   中英

wxpython:如何用在其他位置創建的網格填充“筆記本”選項卡?

[英]wxpython: How can I fill a Notebook tab with a grid created elsewhere?

現在,我有一個用wxpython構建的GUI,它可以成功地從.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()

您不能在兩個不同的父對象中顯示完全相同的窗口小部件。 相反,創建獨立框架時需要創建empGrid實例,而創建筆記本時則需要創建其他實例。

實例化empGrid時,會將筆記本面板/頁面作為父面板傳遞給它。 創建框架時,您將框架(或其面板)作為父級傳遞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM