[파이썬] wxPython `wx.ListCtrl`: 리스트 뷰

The wx.ListCtrl is a powerful widget in the wxPython toolkit that allows you to display and interact with a list of items in a user interface. It provides several view styles, including a list view which is often used to display data in columns and rows.

Getting Started with wxPython

Before diving into the wx.ListCtrl, you need to have wxPython installed on your system. If you haven’t done so already, you can install it using pip:

pip install wxPython

Once wxPython is installed, you can start using it in your Python programs.

Creating a wx.ListCtrl Widget

To create a wx.ListCtrl, you need to import the necessary modules and create an instance of the wx.ListCtrl class. Here’s an example that creates a basic wx.ListCtrl with two columns:

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent):
        super().__init__(parent, title="ListCtrl Example")
        
        # Create a panel and a list control
        panel = wx.Panel(self)
        listctrl = wx.ListCtrl(panel, style=wx.LC_REPORT)
        
        # Add two columns to the list control
        listctrl.InsertColumn(0, "Name")
        listctrl.InsertColumn(1, "Age")
        
        # Set the width of the columns
        listctrl.SetColumnWidth(0, 150)
        listctrl.SetColumnWidth(1, 50)
        
        # Add some data
        listctrl.InsertItem(0, "John Doe")
        listctrl.SetItem(0, 1, "30")
        listctrl.InsertItem(1, "Jane Smith")
        listctrl.SetItem(1, 1, "25")
        
        # Layout the panel
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(listctrl, 1, wx.EXPAND)
        panel.SetSizer(sizer)
    
if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame(None)
    frame.Show()
    app.MainLoop()

In the above example, we create a new wx.Frame and a wx.Panel as the parent widget. Next, we create a wx.ListCtrl with the wx.LC_REPORT style, which gives us a list view with multiple columns. We then insert two columns with headers (“Name” and “Age”) and set their widths. Finally, we insert some sample data into the list view.

Interacting with the wx.ListCtrl

The wx.ListCtrl provides various methods to interact with the data and selection. The following example demonstrates how to get the selected item from the list view:

# ...

# Get the selected item
selected_item = listctrl.GetNextItem(-1, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED)
if selected_item != -1:
    name = listctrl.GetItemText(selected_item, 0)
    age = listctrl.GetItemText(selected_item, 1)
    print(f"Selected: Name - {name}, Age - {age}")

# ...

In the above example, we use the GetNextItem method to iterate through all items in the list view and check for the wx.LIST_STATE_SELECTED state. If a selected item is found, we retrieve its data using the GetItemText method.

Conclusion

The wx.ListCtrl widget in wxPython provides an effective way to display and interact with a list of items in a user interface. By customizing its view style, columns, and data, you can create powerful and intuitive user interfaces in your Python applications.