2016-11-08 69 views
-1

我想从平板笔记本的第一页保存文本文件,将它们写入EXTERNALLY定义的sqLite数据库,并将值写入到flatNotebook的第二页上的网格。这些值保存到文本文件并成功写入数据库,但无法同时将值填入网格。在关闭程序并重新启动后,它们的值将显示在网格中。我很难理解如何调用onAddCue()函数。我已经尝试了很多东西,所以我现在只是在困惑自己。请帮我理解我做错了什么。这里是我的整个代码:wxPython:从另一个类使用flatenotebook填充网格

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() 

回答

0

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

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

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

page2.OnAddCue() 
+0

它要么是我的语法还是我的功能onAddCue的位置()。在不改变我调用onAddCue()的位置时,当我更改语法时,出现以下错误:GridPanel()。grid.onAddCue()给出TypeError:__init __()只需要3个参数(给定1)。 GridPanel(parent,db).grid.onAddCue()给出NameError:没有定义全局名称'parent'。 onAddCue()给出NameError:未定义全局名称'onAddCue'。 GridPanel.grid.onAddCue()给出AttributeError:类型对象'GridPanel'没有属性'grid'。提示[]是正确的:它写入数据库。其他建议@ravenspoint? –

+0

在您原来的文章中,您说过运行该程序时通话不起作用。现在你说你得到语法错误? – ravenspoint

+0

对不起。也许我不清楚(小白)。 onAddCue函数在与网格相同的面板中使用时可以工作。我无法从不同的面板上工作。这就是为什么我认为这是我的语法。感谢您的帮助@ravensport,我还在学习。 –