[파이썬] wxPython `wx.StaticText`: 라벨 위젯

In wxPython, a wx.StaticText widget is used to display static text on a graphical user interface. It provides a simple way to show information or labels to the users. In this blog post, we will explore how to create a wx.StaticText widget and use it in our Python application.

Creating a wx.StaticText Widget

To create a wx.StaticText widget in wxPython, we need to first import the necessary modules and then create an instance of the wx.StaticText class. Here’s an example code that demonstrates how to create a wx.StaticText widget:

import wx

# Create the main application
app = wx.App()

# Create a frame (window) to hold the widget
frame = wx.Frame(None, title='StaticText Widget Example')

# Create a panel within the frame
panel = wx.Panel(frame)

# Create a StaticText widget
static_text = wx.StaticText(panel, label='Hello, World!')

# Center the text horizontally within the widget
static_text.SetLabelTextAlignment(wx.ALIGN_CENTER)

# Show the frame and start the application event loop
frame.Show()
app.MainLoop()

In the above code, we first import the wx module and create the main application object using wx.App(). Then, we create a frame and a panel to hold the widget. Next, we create an instance of wx.StaticText with the desired label text. We can also use methods like SetLabelText() to dynamically change the label text. Finally, we show the frame and start the application event loop using app.MainLoop().

Customizing the wx.StaticText Widget

The wx.StaticText widget provides various methods to customize its appearance. Here are a few commonly used methods:

# Set the label text to 'Hello, World!'
static_text.SetLabel('Hello, World!')

# Set the foreground color to red
static_text.SetForegroundColour(wx.RED)

# Set the background color to light gray
static_text.SetBackgroundColour(wx.Colour(240, 240, 240))

# Set the font to bold and italic
static_text.SetFont(wx.Font(wx.FontInfo(12).Bold().Italic()))

# Set the text alignment to center
static_text.SetLabelTextAlignment(wx.ALIGN_CENTER)

These methods can be used to enhance the appearance and usability of the wx.StaticText widget according to your application’s needs.

Conclusion

The wx.StaticText widget in wxPython is a simple and effective way to display static text or labels on a GUI. In this blog post, we covered how to create a wx.StaticText widget, customize its appearance, and use it in a Python application. With its various customization options, you can create informative and visually appealing labels for your wxPython applications.