0

我有一个 3 列的网格。第一列填充了我正在导入的多个文件,并根据文件的数量写入适当的行。只要未启用自动换行,此方法就可以正常工作。我在尝试填充第二列时遇到问题,该列还允许用户选择多个文件,但我希望每行的文件用逗号分隔。例如,如果我将 3 个文件导入到第一列,因此我在 A 列中有 3 行数据。接下来我尝试为 B 列选择 5 个文件,然后对于每一行,我需要有 5 个文件的列表。

第二个问题是自动换行功能。我希望将 B 列和 C 列换行到特定的列大小,但我无法做到这一点。如果我们使用自动换行功能,那么列和行都需要进行相应的调整,就像在 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()

此外,当我调整框架大小时,我的网格不会随着框架调整大小。有没有办法我们可以完成它。

4

1 回答 1

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

我仍然没有弄清楚自动换行功能的问题。

于 2013-11-23T15:33:34.567 回答