在wx中自动调整表格大小

问题描述 投票:0回答:2

当使用wx.grid通过wxpython创建表格时,一旦用户调整框架大小,如何自动扩展表格中的行数和列数?

如果我创建 5*5 表格(网格)以适合我的框架,并且用户调整框架大小(例如,使其变大),我如何实现自动增加行数和/或列数以响应框架的增加尺寸?

python user-interface event-handling wxpython wxwidgets
2个回答
1
投票

遗憾的是,据我所知,没有内置的方法可以做到这一点。您需要捕获框架的 EVT_SIZE,然后根据需要对网格使用 AppendRows 和 AppendCols。您需要考虑帧大小发生了多少变化,并且仅在它变大而不是变小时才追加。


0
投票

试试这个,增加了点击标题排序的功能:

import wx 
import wx.grid

class Table123(wx.grid.Grid):
    def __init__(self, parent, rows=[], columns=[], pagination=None):
        super(Table123, self).__init__(parent)
        if pagination is None:
            pagination = {
                'sortBy': '',
                'descending': False,
                'page': 1,
                'rowsPerPage': 10,
                'rowsNumber': 10
            }
        self.columns = columns
        self.rows = rows
        self.pagination = pagination
        self.SetDefaultCellAlignment(wx.ALIGN_CENTER, wx.ALIGN_CENTER)
        self.CreateGrid(len(self.rows), len(self.columns))
        self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.on_label_click)
        self.populate_grid()
        self.HideRowLabels()
        self.Bind(wx.EVT_SIZE, self.on_size)
        self.on_size()

    def populate_grid(self):
        for index, column in enumerate(self.columns):
            self.SetColLabelValue(index, column.get("label",""))
        for col in range(self.GetNumberCols()):
            label = self.GetColLabelValue(col)
            self.SetColLabelValue(col, f"{label} {self.get_sort_indicator(col)}")
        for row_index, row in enumerate(self.rows):
            for col_index, column in enumerate(self.columns):
                self.SetCellValue(row_index, col_index, str(row.get(column.get("name",""),"")))

    def on_label_click(self, event):
        col = event.GetCol()
        column_dict = self.columns[col]
        col_name = column_dict.get("name","")
        if self.pagination.get("sortby","") == col_name:
            self.pagination["descending"] = not self.pagination.get("descending","")
        else:
            self.pagination["sortby"] = col_name
            self.pagination["descending"] = False

        self.sort_data(col_name)
        self.ClearGrid()
        self.populate_grid()

    def on_size(self, event=None):
        width, height = self.GetClientSize()
        self.AutoSizeColumns()
        current_col_widths = [self.GetColSize(col) for col in range(self.GetNumberCols())]
        total_width = sum(current_col_widths)
        for col, column_info in enumerate(self.columns):
            relative_width = current_col_widths[col] / total_width
            col_width = int(width * relative_width)
            self.SetColSize(col, col_width)

    def sort_data(self, key):
        self.rows.sort(key=lambda x: x[key], reverse=(self.pagination.get("descending",False)))

    def get_sort_indicator(self, col):
        if self.pagination.get("sortby") == self.columns[col].get("name"):
            return "↑" if self.pagination.get("descending") else "↓"
        return ""

class MyFrame(wx.Frame):
    def __init__(self, *args, **kw):
        super(MyFrame, self).__init__(*args, **kw)
        self.Maximize()
        panel = wx.Panel(self)
        data = [
            {'name': 'Alice', 'age': 25, 'city': 'New York'},
            {'name': 'Bob', 'age': 30, 'city': 'San Franciscoooooooooooooooooooooooooo'},
            {'name': 'Charlie', 'age': 22, 'city': 'Seattle'},
        ]
        columns = [
            {'name': 'name', 'label': 'FirstName'},
            {'name': 'age', 'label': 'Age'},
            {'name': 'city', 'label': 'City'},
        ]
        grid = Table123(panel, rows=data, columns=columns)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(grid, 1, wx.EXPAND)
        panel.SetSizer(sizer)

        self.SetSize((600, 400))
        self.Center()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame(None, title="Grid Example")
    frame.Show()
    app.MainLoop()
© www.soinside.com 2019 - 2024. All rights reserved.