wxPython:使用flatenotebook

时间:2016-11-08 15:36:49

标签: sqlite wxpython wxwidgets

我正在尝试从flatNotebook的第一页保存文本文件,将它们写入外部定义的sqLite数据库,并将值写入flatNotebook第二页的网格中。这些值保存到文本文件并成功写入数据库,但我无法获取值以同时填充网格。在我关闭程序并重新启动它之后,它们的值会显示在网格中。我很难理解如何调用函数onAddCue()。我尝试了很多这样的事情,我现在只是在困惑自己。请帮我理解我做错了什么。这是我的整个代码:

cue =[4,'NodeA',11,22,33,44,55,66,77,88,99]

class InitialInputs(scrolled.ScrolledPanel):
    global cue  
    def __init__(self, parent, db):
        scrolled.ScrolledPanel.__init__(self, parent, -1)

        self.db = db
        self.cur = self.db.con.cursor()

        self.saveBtn = wx.Button(self, -1, "Save Current Values")
        self.Bind(wx.EVT_BUTTON, self.onSave, self.saveBtn)

        self.dirname = ""

    def onSave(self, event):
        global cue

        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.txt", wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
        if dlg.ShowModal() == wx.ID_OK:
            itcontains = cue
            self.filename=dlg.GetFilename()
            self.dirname=dlg.GetDirectory()
            filehandle=open(os.path.join(self.dirname, self.filename),'w')
            filehandle.write(str(itcontains))
            filehandle.close()
        dlg.Destroy()

        row = cue[0] - 1
        InsertCell ="UPDATE CUES SET 'Send'=?,'RED'=?,'GREEN'=?,'BLUE'=?,'RGB_Alpha'=?,'HUE'=?,'SAT'=?,'BRightness'=?,'HSB_Alpha'=?,'Fade'=? WHERE DTIndex=%i" %row
        self.cur.execute(InsertCell, cue[1:])
        self.db.con.commit()

        GridPanel().grid.onAddCue() #This is the part that's not working

class Grid(gridlib.Grid):
    global cue
    def __init__(self, parent, db):
        gridlib.Grid.__init__(self, parent, -1)
        self.CreateGrid(20,10)

        for row in range(20):
            rowNum = row + 1
            self.SetRowLabelValue(row, "cue %s" %rowNum)

        self.db = db
        self.cur = self.db.con.cursor()
        meta = self.cur.execute("SELECT * from CUES")
        labels = []
        for i in meta.description:
            labels.append(i[0])
        labels = labels[1:]
        for i in range(len(labels)):
            self.SetColLabelValue(i, labels[i])

        all = self.cur.execute("SELECT * from CUES ORDER by DTindex")
        for row in all:
            row_num = row[0]
            cells = row[1:]
            for i in range(len(cells)):
                if cells[i] != None and cells[i] != "null":
                    self.SetCellValue(row_num, i, str(cells[i]))

        self.Bind(gridlib.EVT_GRID_CELL_CHANGED, self.CellContentsChanged)

    def CellContentsChanged(self, event):
        x = event.GetCol()
        y = event.GetRow()
        val = self.GetCellValue(y,x)
        if val == "":
            val = "null"
        ColLabel = self.GetColLabelValue(x)
        InsertCell = "UPDATE CUES SET %s = ? WHERE DTindex = %d"%(ColLabel,y)
        self.cur.execute(InsertCell, [(val),]) 
        self.db.con.commit()
        self.SetCellValue(y, x, val)

class GridPanel(wx.Panel):
    def __init__(self, parent, db):
        wx.Panel.__init__(self, parent, -1)

        self.db = db
        self.cur = self.db.con.cursor()

        grid = Grid(self, db)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(grid)

        self.SetSizer(sizer)
        self.Fit()

    def onAddCue():
        global cue
        row = cue[0] - 1
        col=0
        while col < 10:
            for i in cue[1:]:
                grid.SetCellValue(row, col, str(i))
                col = col+1

class GetDatabase():
    def __init__(self, f):
        # check db file exists
        try:
            file = open(f)
            file.close()
        except IOError:
            # database doesn't exist - create file & populate it
            self.exists = 0
        else:
            # database already exists - need integrity check here
            self.exists = 1
        self.con = sqlite.connect(f)

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1,"Stage Lighting", size=(800,600))

        panel = wx.Panel(self)
        notebook = wx.Notebook(panel)

        page1 = InitialInputs(notebook, db)
        page2 = GridPanel(notebook, db)

        notebook.AddPage(page1, "Initial Inputs")
        notebook.AddPage(page2, "Grid")

        sizer = wx.BoxSizer()
        sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        self.Layout()

if __name__ == "__main__":

    db = GetDatabase("data.db")

    app = wx.App()
    logging.basicConfig(level=logging.DEBUG)          
    Frame().Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:0)

显然,问题是关于python语法错误!

onAddCue是一个属于GridPanel类的方法。该类在变量page2中实例化。

所以,你需要调用这样的方法:

page2.OnAddCue()