[파이썬] wxPython `wx.Grid`: 그리드 뷰

The wxPython library provides a wide range of GUI widgets for building desktop applications. One of the most commonly used widgets is the wx.Grid class, which allows you to create a grid-based view for displaying and editing tabular data.

Setting up wxPython

Before we dive into using the wx.Grid widget, let’s make sure we have wxPython installed. You can install it using pip by running the following command in your terminal:

pip install wxPython

Creating a Simple Grid

To create a simple grid view using wxPython, follow these steps:

Step 1: Import the necessary modules

Start by importing the wx module and the wx.grid module:

import wx
import wx.grid as gridlib

Step 2: Create a wx.Frame window

Next, create a wx.Frame window to hold the grid view:

class GridFrame(wx.Frame):
    def __init__(self, parent):
        super(GridFrame, self).__init__(parent, title='Grid View Demo')

        self.panel = wx.Panel(self)

Step 3: Create a wx.Grid instance

Inside the GridFrame class, create an instance of the wx.Grid class and add it to the panel:

        self.grid = gridlib.Grid(self.panel)
        self.grid.CreateGrid(5, 5)  # Create a 5x5 grid

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

        self.panel.SetSizer(self.sizer)

Step 4: Run the application

Finally, create an instance of the GridFrame class and start the wxPython event loop:

if __name__ == '__main__':
    app = wx.App()
    frame = GridFrame(None)
    frame.Show()
    app.MainLoop()

By running the above code, you will see a simple grid view with 5 rows and 5 columns.

Customizing the Grid

The wx.Grid widget provides numerous customization options. Here are a few examples:

Setting column and row labels

To set labels for columns and rows, you can use the SetColLabelValue and SetRowLabelValue methods:

self.grid.SetColLabelValue(0, "Name")
self.grid.SetColLabelValue(1, "Age")

self.grid.SetRowLabelValue(0, "John")
self.grid.SetCellValue(0, 0, "John")
self.grid.SetCellValue(0, 1, "25")

Changing the cell background color

To change the background color of a specific cell, you can use the SetCellBackgroundColour method:

self.grid.SetCellBackgroundColour(0, 0, wx.YELLOW)

Adjusting column width and row height

To adjust the width of a column or the height of a row, you can use the SetColSize and SetRowSize methods:

self.grid.SetColSize(0, 150)  # Set the width of column 0 to 150 pixels
self.grid.SetRowSize(0, 30)   # Set the height of row 0 to 30 pixels

These are just a few examples of what you can do with the wx.Grid widget. You can explore the official wxPython documentation to discover more customization options.

Conclusion

In this blog post, we explored how to use the wx.Grid widget in wxPython to create a grid-based view for displaying and editing tabular data. We learned the basic steps to set up a grid view and customize various aspects of it. With wxPython, you can easily create powerful desktop applications with a user-friendly interface.