wxPython objectListView保存更改



我正在学习在wxPython中使用objectListView。当运行Mouse vs.Python中的示例时。我看到您可以编辑单元格,但是一旦程序关闭,编辑就不会保存。我已经盯着readthedocs的文档2天了,我一直无法使它工作。如何允许编辑和保存它?

是否有从CSV文件构建行并让编辑更新CSV文件?

我正在使用wxPython Phoenix 3.0.3和Python 2.7

下面是我的起始代码:

class Book(object):
def __init__(self, cue, sendTo, red, green, blue, time):
    self.cue = cue
    self.sendTo = sendTo
    self.red = red
    self.green = green
    self.blue = blue
    self.time = time
class MainFrame(wx.Frame):
def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, 
                      title="ObjectListView", size=(800,600))

    panel = wx.Panel(self, -1)
    #Need to get this information from *.txt file
    self.cues = [Book("cue 1", "NodeA",
                      "193", "123", "123","0"),
                 Book("cue 2", "Group 1",
                      "193", "123", "123","0")
                 ]
    self.cuesOlv = ObjectListView(panel, wx.ID_ANY, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
    self.setCues()
    self.cuesOlv.cellEditMode = ObjectListView.CELLEDIT_SINGLECLICK

    mainSizer = wx.BoxSizer(wx.VERTICAL)
    mainSizer.Add(self.cuesOlv, 1, wx.ALL|wx.EXPAND, 5)
    #mainSizer.Add(self.updateBtn, 0, wx.ALL|wx.CENTER, 5)
    panel.SetSizer(mainSizer)
def setCues(self, data=None):
    self.cuesOlv.SetColumns([
        ColumnDefn("Cue", "center", 100, "cue"),
        ColumnDefn("Send To:", "center", 100, "sendTo"),
        ColumnDefn("Red", "center", 100, "red"),            
        ColumnDefn("Green", "center", 100, "green"),
        ColumnDefn("Blue", "center", 100, "blue"),
        ColumnDefn("Time", "center", 100, "time")
    ])
    self.cuesOlv.SetObjects(self.cues)

我的目标是允许用户更改任何列中的值。在关于编辑单元格值的文档中,我看到第一步是设置cellEditMode属性。下一步是决定一个单元格编辑器,这就是我感到困惑的地方。如果我希望用户能够编辑任何单元格,我应该使用基于列的、基于事件的还是基于注册表的编辑器?"获取"one_answers"设置"从何而来?我可以使用GetValue和SetValue不创建一个单元格编辑器吗?然后必须更新模型对象;这是否可以通过离开单元格来完成,或者必须发生一些活动,例如将函数绑定到按钮?

从Mike Driscoll提供的示例中,我看到他如何更新列表,但没有保存更改。一旦GUI关闭,更改就会丢失。如何保存更改?

不使用任何csv模块,这可能使您的生活更轻松,并假设本地book.txt文件,如下所示:

"wxPython in Action","Robin Dunn","1932394621","Manning"
"Hello World","Warren and Carter Sande","1933988495","Manning"
"Core Python Programming","Wesley Chun","0132269937","Prentice Hall"
"Python Programming for the Absolute Beginner","Michael Dawson","1598631128","Course Technology"
"Learning Python","Mark Lutz","0596513984","O'Reilly"

下面将给你一个如何实现你的目标的简单的想法,使用Mike Driscoll的示例代码

import wx
from ObjectListView import ObjectListView, ColumnDefn
########################################################################
class Book(object):
    """
    Model of the Book object
    Contains the following attributes:
    'ISBN', 'Author', 'Manufacturer', 'Title'
    """
    #----------------------------------------------------------------------
    def __init__(self, title, author, isbn, mfg):
        self.isbn = isbn
        self.author = author
        self.mfg = mfg
        self.title = title
########################################################################
class MainPanel(wx.Panel):
    #----------------------------------------------------------------------
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
        #Read the book data from a file
        with open('books.txt','r') as f:
            books = f.readlines()
        self.products = []
        #Manually strip out inverted commas and line feeds, then split the data into its parts
        for b in books:
            b=b.replace('"','')
            b=b.strip()
            title,author,isbn,pub = b.split(",")
            self.products.append(Book(title,author,isbn,pub))
        self.dataOlv = ObjectListView(self, wx.ID_ANY, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
        self.setBooks()
        # Allow the cell values to be edited when double-clicked
        self.dataOlv.cellEditMode = ObjectListView.CELLEDIT_SINGLECLICK
        # create an Write output file button
        updateBtn = wx.Button(self, wx.ID_ANY, "Write Output")
        updateBtn.Bind(wx.EVT_BUTTON, self.updateControl)
        # Create some sizers
        mainSizer = wx.BoxSizer(wx.VERTICAL)        
        mainSizer.Add(self.dataOlv, 1, wx.ALL|wx.EXPAND, 5)
        mainSizer.Add(updateBtn, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(mainSizer)
    #----------------------------------------------------------------------
    def updateControl(self, event):
        """
        Write OLV data to a file 
        """
        #retrieve the data from the Olv
        data=self.dataOlv.GetObjects()
        #Write the data out to an empty file
        with open('books1.txt','w') as f:
            for b in data:
                outp='"%s","%s","%s","%s"n'%(b.title,b.author,b.isbn,b.mfg)
                f.write(outp)
    #----------------------------------------------------------------------
    def setBooks(self, data=None):
        self.dataOlv.SetColumns([
            ColumnDefn("Title", "left", 220, "title"),
            ColumnDefn("Author", "left", 200, "author"),
            ColumnDefn("ISBN", "right", 100, "isbn"),            
            ColumnDefn("Mfg", "left", 180, "mfg")
        ])
        self.dataOlv.SetObjects(self.products)
########################################################################
class MainFrame(wx.Frame):
    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, 
                          title="ObjectListView Demo", size=(800,600))
        panel = MainPanel(self)
########################################################################
class GenApp(wx.App):
    #----------------------------------------------------------------------
    def __init__(self, redirect=False, filename=None):
        wx.App.__init__(self, redirect, filename)
    #----------------------------------------------------------------------
    def OnInit(self):
        # create frame here
        frame = MainFrame()
        frame.Show()
        return True
#----------------------------------------------------------------------
def main():
    """
    Run the demo
    """
    app = GenApp()
    app.MainLoop()
if __name__ == "__main__":
    main()

相关内容

  • 没有找到相关文章

最新更新