0

我正在尝试学习在 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 属性。下一步是决定一个单元格编辑器,这就是我感到困惑的地方。如果我希望用户能够编辑任何单元格,我应该使用基于列、基于事件还是基于注册表的编辑器?“获取”和“设置”从何而来?我可以在不创建单元格编辑器的情况下使用 GetValue 和 SetValue 吗?然后必须更新模型对象;这可以通过离开单元格来完成,还是必须进行一些活动,例如将功能绑定到按钮?

从 Mike Driscoll 提供的示例中,我看到他如何更新列表,但未保存更改。GUI 关闭后,更改将丢失。如何保存更改?

4

1 回答 1

0

不使用任何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()
于 2016-10-31T08:57:23.317 回答