【问题标题】:wxPython: populate grid from another class using flatenotebookwxPython:使用 flatenotebook 从另一个类填充网格
【发布时间】:2016-11-08 15:36:48
【问题描述】:

我正在尝试从 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()

【问题讨论】:

    标签: sqlite wxpython wxwidgets


    【解决方案1】:

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

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

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

    page2.OnAddCue()
    

    【讨论】:

    • 要么是我的语法,要么是我对函数 onAddCue() 的放置。在不更改调用 onAddCue() 的位置的情况下,更改语法时会出现以下错误: GridPanel().grid.onAddCue() 给出 TypeError:__init__() 恰好需要 3 个参数(给定 1 个)。 GridPanel(parent, db).grid.onAddCue() 给出 NameError:未定义全局名称“父级”。 onAddCue() 给出 NameError:未定义全局名称“onAddCue”。 GridPanel.grid.onAddCue() 给出 AttributeError:类型对象 'GridPanel' 没有属性 'grid'。提示 [] 是正确的:它写入数据库。其他建议@ravenspoint?
    • 在您的原始帖子中,您说运行程序时调用不起作用。现在你是说你遇到了语法错误?
    • 对不起。也许我不清楚(菜鸟)。当在与网格相同的面板中使用时,函数 onAddCue 起作用。我无法让它从不同的面板工作。这就是为什么我认为这是我的语法。感谢@ravensport 的帮助,我还在学习中。
    • 请澄清您的问题。
    • page2.onAddCue() 导致:NameError:未定义全局名称“page2”。还有其他想法吗? @ravensport?
    猜你喜欢
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2018-07-30
    • 2014-12-10
    相关资源
    最近更新 更多