wxgrid SetCellvalue和Wordwrap

时间:2013-11-21 00:40:14

标签: python wxpython wxgrid

我有一个有3列的网格。第一列正在填充我正在导入的多个文件,并根据文件数写入相应的行。只要未启用wordwrap,这样就可以正常工作。我在尝试填充第二列时遇到问题,此列还允许用户选择多个文件,但我希望每行都以逗号分隔文件。例如,如果我将3个文件导入第一列,那么我在A列中有3行数据。接下来我试图为B列选择5个文件,然后对于每一行,我需要有一个包含5个文件的列表。

第二个问题是wordwrap功能。我希望B列和C列是Word包装到某个列大小但我无法做到这一点。如果我们使用wordwrap功能,则需要相应地调整列和行,就像在excel中一样。

以下是代码。

import wx
import wx.grid
from wx.lib import wordwrap

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1GRID1,
 wxID_FRAME1PANEL1,
] = [wx.NewId() for _init_ctrls in range(5)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(394, 306), size=wx.Size(835, 372),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(819, 334))

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(819, 334),
              style=wx.TAB_TRAVERSAL)

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1,
              label='Import to Column A', name='button1', parent=self.panel1,
              pos=wx.Point(88, 16), size=wx.Size(152, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.On_Button1_Button,
              id=wxID_FRAME1BUTTON1)

        self.grid1 = wx.grid.Grid(id=wxID_FRAME1GRID1, name='grid1',
              parent=self.panel1, pos=wx.Point(16, 64), size=wx.Size(776, 208),
              style=0)
        self.grid1.SetAutoLayout(False)

        self.button2 = wx.Button(id=wxID_FRAME1BUTTON2,
              label='Import to Column B', name='button2', parent=self.panel1,
              pos=wx.Point(256, 16), size=wx.Size(136, 23), style=0)
        self.button2.Bind(wx.EVT_BUTTON, self.On_Button2_Button,
              id=wxID_FRAME1BUTTON2)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def On_Button1_Button(self, event):
        #event.Skip()
        filenames = []
        dlg = wx.FileDialog(self, "Choose  Files", ".", "","Text Files (*.txt)|*.txt", wx.MULTIPLE)
        try:
            if dlg.ShowModal() == wx.ID_OK:
                inputfilenames = dlg.GetPaths()
                gridheaderlist = ["File Name","Files to be Formatted","Other User Defined Options"]
                if self.grid1.GetNumberCols() == 0:
                    self.grid1.CreateGrid(len(inputfilenames),len(gridheaderlist))
                    for i in range(len(gridheaderlist)):
                        self.grid1.SetColLabelValue(i,gridheaderlist[i])
                        wordwrap.wordwrap(self.grid1.SetColLabelValue(i,gridheaderlist[i]),self.grid1.GetColSize(i),wx.ClientDC(self.panel1),breakLongWords=True)
                    for i in range(len(inputfilenames)):
                        self.grid1.SetCellValue(i,0,inputfilenames[i])
                else:
                    LastUsedRow = self.grid1.GetNumberRows()
                    self.grid1.InsertRows(LastUsedRow,len(inputfilenames))
                    for i in range(len(inputfilenames)):
                        self.grid1.SetCellValue(i+LastUsedRow,0,inputfilenames[i])
        finally:
            if self.grid1.GetNumberRows() >= 1:
                self.grid1.AutoSizeColumn(0, 1)
            dlg.Destroy()

    def On_Button2_Button(self, event):
        event.Skip()
        global forfiles
        forfiles = []
        dlg = wx.FileDialog(self, "Choose  Formatting Files", ".", "","Formatting Files (*.xls)|*.xls", wx.MULTIPLE)
        forfiles = dlg.GetPath()
        if len(forfiles) > 0:
            if dlg.ShowModal() == wx.ID_OK:
                    temp_files= dlg.GetPaths()
                    for d in range(len(temp_files)):
                            forfiles.append(temp_files[d])
            dlg.Destroy()
            for i in range(self.grid1.GetNumberRows()):
                self.grid1.SetCellValue(i,1,forfiles)
        if len(forfiles) == 0:
            try:
                if dlg.ShowModal() == wx.ID_OK:
                    forfiles = dlg.GetPath()
            finally:
                if self.grid1.GetNumberRows() >= 1:
                    self.grid1.AutoSizeColumn(1, 1)
                dlg.Destroy()


if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = create(None)
    frame.Show()

    app.MainLoop()

另外,当我调整框架大小时,我的网格不会随框架调整大小。有没有办法可以实现它。

1 个答案:

答案 0 :(得分:0)

我在单元格中使用wxgrid设置值解决了部分问题。这是更新的代码。

import wx
import wx.grid
from wx.lib import wordwrap

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1GRID1,
 wxID_FRAME1PANEL1,
] = [wx.NewId() for _init_ctrls in range(5)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(394, 306), size=wx.Size(835, 372),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(819, 334))

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(819, 334),
              style=wx.TAB_TRAVERSAL)

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1,
              label='Import to Column A', name='button1', parent=self.panel1,
              pos=wx.Point(88, 16), size=wx.Size(152, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.On_Button1_Button,
              id=wxID_FRAME1BUTTON1)

        self.grid1 = wx.grid.Grid(id=wxID_FRAME1GRID1, name='grid1',
              parent=self.panel1, pos=wx.Point(16, 64), size=wx.Size(776, 208),
              style=0)
        self.grid1.SetAutoLayout(False)

        self.button2 = wx.Button(id=wxID_FRAME1BUTTON2,
              label='Import to Column B', name='button2', parent=self.panel1,
              pos=wx.Point(256, 16), size=wx.Size(136, 23), style=0)
        self.button2.Bind(wx.EVT_BUTTON, self.On_Button2_Button,
              id=wxID_FRAME1BUTTON2)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def On_Button1_Button(self, event):
        #event.Skip()
        filenames = []
        dlg = wx.FileDialog(self, "Choose  Files", ".", "","Text Files (*.txt)|*.txt", wx.MULTIPLE)
        try:
            if dlg.ShowModal() == wx.ID_OK:
                inputfilenames = dlg.GetPaths()
                gridheaderlist = ["File Name","Files to be Formatted","Other User Defined Options"]
                if self.grid1.GetNumberCols() == 0:
                    self.grid1.CreateGrid(len(inputfilenames),len(gridheaderlist))
                    for i in range(len(gridheaderlist)):
                        self.grid1.SetColLabelValue(i,gridheaderlist[i])
                        #wordwrap.wordwrap(self.grid1.SetColLabelValue(i,gridheaderlist[i]),self.grid1.GetColSize(i),wx.ClientDC(self.panel1),breakLongWords=True)
                    for i in range(len(inputfilenames)):
                        self.grid1.SetCellValue(i,0,inputfilenames[i])
                else:
                    LastUsedRow = self.grid1.GetNumberRows()
                    self.grid1.InsertRows(LastUsedRow,len(inputfilenames))
                    for i in range(len(inputfilenames)):
                        self.grid1.SetCellValue(i+LastUsedRow,0,inputfilenames[i])
        finally:
            if self.grid1.GetNumberRows() >= 1:
                self.grid1.AutoSizeColumn(0, 1)
            dlg.Destroy()

    def On_Button2_Button(self, event):
        event.Skip()
        global forfiles, test
        forfiles = []
        dlg = wx.FileDialog(self, "Choose  Formatting Files", ".", "","Formatting Files (*.xls)|*.xls", wx.MULTIPLE)
        if dlg.ShowModal() == wx.ID_OK:
            forfiles = dlg.GetPaths()
        dlg.Destroy()
        if len(forfiles) > 0:
            for d in range(len(forfiles)):
                if d == 0:
                    test = forfiles[d]
                else:
                    test = test + '\n' + forfiles[d]              
            for i in range(self.grid1.GetNumberRows()):
                self.grid1.SetCellValue(i,1,test)
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = create(None)
    frame.Show()

    app.MainLoop()

我还没有弄清楚wordwrap功能的问题。

相关问题