Python-GUI-wxPython-控件

发布于:2025-07-05 ⋅ 阅读:(17) ⋅ 点赞:(0)

1 需求


2 接口


 3.* 控件:wx.StaticText

import wx


class MainFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(*args, **kwargs)

        self.init_ui()
        self.Center()
        self.Maximize()

    def init_ui(self):
        static_text = wx.StaticText(parent=self,
                                    id=-1,
                                    label="Hello World",
                                    pos=wx.DefaultPosition,
                                    size=wx.DefaultSize,
                                    style=0,
                                    name="textCtrl")


if __name__ == "__main__":
    app = wx.App()
    frame = MainFrame(None, title="wxPython Demo")
    frame.Show()
    app.MainLoop()

3.* 控件:wx.TextCtrl

import wx


class MainFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(*args, **kwargs)

        self.init_ui()
        self.Center()
        self.Maximize()

    def init_ui(self):
        text_control = wx.TextCtrl(parent=self,
                                   id=-1,
                                   value="",
                                   pos=wx.DefaultPosition,
                                   size=wx.DefaultSize,
                                   style=0,
                                   validator=wx.DefaultValidator,
                                   name="textCtrl")


if __name__ == "__main__":
    app = wx.App()
    frame = MainFrame(None, title="wxPython Demo")
    frame.Show()
    app.MainLoop()

3.* 控件:wx.Button

import wx


class MyFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.text_ctrl = None

        self.init_ui()

        self.Center()

        self.Maximize()

    def init_ui(self):
        panel = wx.Panel(self)

        sizer = wx.BoxSizer(wx.HORIZONTAL)

        btn = wx.Button(panel, label="测试")
        self.text_ctrl = wx.TextCtrl(panel)

        sizer.Add(btn)
        sizer.Add(self.text_ctrl)

        btn.Bind(wx.EVT_BUTTON, self.on_btn)

        panel.SetSizer(sizer)

    def on_btn(self, event):
        self.text_ctrl.SetValue("hello world")


if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame(None, title="wxPython Demo")
    frame.Show()
    app.MainLoop()

3.* 控件:wx.RadioButton

import wx


class MyFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.init_ui()

        self.Center()

        self.Maximize()

    def init_ui(self):
        panel = wx.Panel(self)

        sizer = wx.BoxSizer(wx.HORIZONTAL)

        static_text = wx.StaticText(panel, label="Gender: ")

        male_radio_btn = wx.RadioButton(panel, label="Male", style=wx.RB_GROUP)
        female_radio_btn = wx.RadioButton(panel, label="Female")

        sizer.Add(static_text, flag=wx.ALL | wx.ALIGN_TOP, border=10)
        sizer.Add(male_radio_btn, flag=wx.ALL | wx.ALIGN_TOP, border=10)
        sizer.Add(female_radio_btn, flag=wx.ALL | wx.ALIGN_TOP, border=10)

        self.text_ctrl = wx.TextCtrl(panel)
        sizer.Add(self.text_ctrl, flag=wx.ALL | wx.ALIGN_TOP, border=10)

        male_radio_btn.Bind(wx.EVT_RADIOBUTTON, self.on_radio)
        female_radio_btn.Bind(wx.EVT_RADIOBUTTON, self.on_radio)

        panel.SetSizer(sizer)

    def on_radio(self, event):
        selected_label = event.GetEventObject().GetLabel()
        self.text_ctrl.SetValue(selected_label)


if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame(None, title="wxPython Demo")
    frame.Show()
    app.MainLoop()

3.* 示例:wx.CheckBox

……


3.* 示例:wx.ComboBox

……


3.* 示例:wx.ListBox

……


4 示例

……