wxpython的简单应用,实现对话框,绑定事件,页面布局,改变面板背景

import wx
#参数配置界面
class ChangeDepthDialog(wx.Dialog):

    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id=wx.ID_ANY, title='参数配置', pos=wx.DefaultPosition,
                           size=wx.Size(600, 400), style=wx.DEFAULT_DIALOG_STYLE)

        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)

        # 最外层 sizer
        box_sizer = wx.BoxSizer(wx.VERTICAL)

        # 客户端及服务器配置 sizer
        sb_sizer_1 = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, u"客户端及服务器配置"), wx.VERTICAL)
        fg_sizer_1 = wx.FlexGridSizer(2, 2, 0, 0)
        fg_sizer_1.SetFlexibleDirection(wx.BOTH)
        fg_sizer_1.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_SPECIFIED)
        # 服务器 IP 地址
        self.label_ip = wx.StaticText(sb_sizer_1.GetStaticBox(), wx.ID_ANY, u"服务器 IP 地址 :",
                                      wx.DefaultPosition, wx.Size(100, -1), wx.ALIGN_RIGHT)
        self.label_ip.Wrap(-1)
        fg_sizer_1.Add(self.label_ip, 0, wx.ALIGN_CENTER | wx.ALL, 5)
        self.text_ip = wx.TextCtrl(sb_sizer_1.GetStaticBox(), wx.ID_ANY, wx.EmptyString,
                                   wx.DefaultPosition, wx.Size(300, -1), 0)
        fg_sizer_1.Add(self.text_ip, 0, wx.ALL, 5)
        # SAP 程序路径
        self.label_exe_path = wx.StaticText(sb_sizer_1.GetStaticBox(), wx.ID_ANY, u"SAP 程序路径 :",
                                            wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_RIGHT)
        self.label_exe_path.Wrap(-1)
        fg_sizer_1.Add(self.label_exe_path, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT, 5)
        self.text_exe_path = wx.FilePickerCtrl(sb_sizer_1.GetStaticBox(), wx.ID_ANY, wx.EmptyString,
                                               u"请选择 SAP 可执行文件", u"*.exe", wx.DefaultPosition,
                                               wx.Size(394, -1), wx.FLP_DEFAULT_STYLE)
        fg_sizer_1.Add(self.text_exe_path, 0, wx.ALL, 5)
        sb_sizer_1.Add(fg_sizer_1, 1, wx.EXPAND, 5)
        box_sizer.Add(sb_sizer_1, 2, wx.ALL | wx.EXPAND, 5)

        # 公司及业务代码 sizer
        sb_sizer_2 = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, u"公司及业务代码配置"), wx.VERTICAL)
        fg_sizer_2 = wx.FlexGridSizer(1, 4, 0, 0)
        fg_sizer_2.SetFlexibleDirection(wx.BOTH)
        fg_sizer_2.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_SPECIFIED)
        # 公司代码
        self.label_company_code = wx.StaticText(sb_sizer_2.GetStaticBox(), wx.ID_ANY, u"公司代码 :",
                                                wx.DefaultPosition, wx.Size(100, -1), wx.ALIGN_RIGHT)
        self.label_company_code.Wrap(-1)
        fg_sizer_2.Add(self.label_company_code, 0, wx.ALIGN_CENTER | wx.ALL, 5)
        self.text_company_code = wx.TextCtrl(sb_sizer_2.GetStaticBox(), wx.ID_ANY, wx.EmptyString,
                                             wx.DefaultPosition, wx.Size(90, -1), 0)
        fg_sizer_2.Add(self.text_company_code, 0, wx.ALL, 5)
        # 业务代码
        self.label_biz_code = wx.StaticText(sb_sizer_2.GetStaticBox(), wx.ID_ANY, u"业务代码 :",
                                            wx.DefaultPosition, wx.Size(100, -1), wx.ALIGN_RIGHT)
        self.label_biz_code.Wrap(-1)
        fg_sizer_2.Add(self.label_biz_code, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT, 5)

        self.text_biz_code = wx.TextCtrl(sb_sizer_2.GetStaticBox(), wx.ID_ANY, wx.EmptyString,
                                         wx.DefaultPosition, wx.Size(90, -1), 0)
        fg_sizer_2.Add(self.text_biz_code, 0, wx.ALL, 5)
        sb_sizer_2.Add(fg_sizer_2, 1, wx.EXPAND, 5)
        box_sizer.Add(sb_sizer_2, 1, wx.ALL | wx.EXPAND, 5)

        # 用户账户及密码 sizer
        sb_sizer_3 = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, u"用户账户及密码配置"), wx.VERTICAL)
        fg_sizer_3 = wx.FlexGridSizer(2, 4, 0, 0)
        fg_sizer_3.SetFlexibleDirection(wx.BOTH)
        fg_sizer_3.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_SPECIFIED)
        # 登录环境
        self.label_login_env = wx.StaticText(sb_sizer_3.GetStaticBox(), wx.ID_ANY, u"登录环境 :",
                                             wx.DefaultPosition, wx.Size(100, -1), wx.ALIGN_RIGHT)
        self.label_login_env.Wrap(-1)
        fg_sizer_3.Add(self.label_login_env, 0, wx.ALIGN_CENTER | wx.ALL, 5)
        self.text_login_env = wx.TextCtrl(sb_sizer_3.GetStaticBox(), wx.ID_ANY, wx.EmptyString,
                                          wx.DefaultPosition, wx.Size(90, -1), 0)
        fg_sizer_3.Add(self.text_login_env, 0, wx.ALL, 5)
        # 登录端口
        self.label_login_port = wx.StaticText(sb_sizer_3.GetStaticBox(), wx.ID_ANY, u"登录端口 :",
                                              wx.DefaultPosition, wx.Size(100, -1), wx.ALIGN_RIGHT)
        self.label_login_port.Wrap(-1)
        fg_sizer_3.Add(self.label_login_port, 0, wx.ALL, 5)
        self.text_login_port = wx.TextCtrl(sb_sizer_3.GetStaticBox(), wx.ID_ANY, wx.EmptyString,
                                           wx.DefaultPosition, wx.Size(90, -1), 0)
        fg_sizer_3.Add(self.text_login_port, 0, wx.ALL, 5)
        # 用户账号
        self.label_user_account = wx.StaticText(sb_sizer_3.GetStaticBox(), wx.ID_ANY, u"用户账号 :",
                                                wx.DefaultPosition, wx.Size(100, -1), wx.ALIGN_RIGHT)
        self.label_user_account.Wrap(-1)
        fg_sizer_3.Add(self.label_user_account, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT, 5)
        self.text_user_account = wx.TextCtrl(sb_sizer_3.GetStaticBox(), wx.ID_ANY, wx.EmptyString,
                                             wx.DefaultPosition, wx.Size(90, -1), 0)
        fg_sizer_3.Add(self.text_user_account, 0, wx.ALL, 5)
        # 用户密码
        self.label_user_pwd = wx.StaticText(sb_sizer_3.GetStaticBox(), wx.ID_ANY, u"用户密码 :",
                                            wx.DefaultPosition, wx.Size(100, -1), wx.ALIGN_RIGHT)
        self.label_user_pwd.Wrap(-1)
        fg_sizer_3.Add(self.label_user_pwd, 0, wx.ALL, 5)
        self.text_user_pwd = wx.TextCtrl(sb_sizer_3.GetStaticBox(), wx.ID_ANY, wx.EmptyString,
                                         wx.DefaultPosition, wx.Size(90, -1), wx.TE_PASSWORD)
        fg_sizer_3.Add(self.text_user_pwd, 0, wx.ALL, 5)
        sb_sizer_3.Add(fg_sizer_3, 0, wx.EXPAND, 5)
        box_sizer.Add(sb_sizer_3, 2, wx.ALL | wx.EXPAND, 5)

        sdb_sizer = wx.StdDialogButtonSizer()
        self.btn_ok = wx.Button(self, wx.ID_OK, label='确定')
        sdb_sizer.AddButton(self.btn_ok)
        self.btn_cancel = wx.Button(self, wx.ID_CANCEL, label='取消')
        sdb_sizer.AddButton(self.btn_cancel)
        sdb_sizer.Realize()

        box_sizer.Add(sdb_sizer, 1, wx.ALIGN_BOTTOM | wx.ALIGN_RIGHT, 5)
        self.SetSizer(box_sizer)
        self.Layout()
        self.Centre(wx.BOTH)



    def __del__(self):
        pass

    def settings_cancel(self, event):
        self.Destroy()




#主界面
class MainFrame(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(*args, **kwargs)
        self.init_ui()
    def init_ui(self):
        menu_bar = wx.MenuBar()
        menu_file = wx.Menu()
        menu_edit=wx.Menu()
       ####菜单栏
        self.CJ1=menu_file.Append(1,'事件1')
        self.CJ2=menu_file.Append(2,'事件2')
        menu_bar.Append(menu_file,'&开始')

        self.end=menu_edit.Append(3,'结束')
        self.Bind(wx.EVT_MENU,self.on_end,self.end)
        menu_bar.Append(menu_edit,'&Help')
        self.SetMenuBar(menu_bar)

        self.SetWindowStyle(style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
        self.SetSize((470, 720))
        self.SetTitle('小小界面')
        self.Centre()

        # 背景图片
        # panel=wx.Panel(self)
        background_img = 'image.jpg'
        to_bmp_image = wx.Image(background_img, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.bitmap = wx.StaticBitmap(self, -1, to_bmp_image, (0, 0))
        #布局处理
        sizer = wx.GridBagSizer(4, 4)
        #场景1
        self.button1 = wx.Button(self.bitmap,-1, label="场景1",size=(240, 120))
        sizer.Add(self.button1, pos=(0, 0),
                  flag=wx.ALL, border=20)
        self.button2 = wx.Button(self.bitmap,-1, label="配置参数",size=(100, 50))
        sizer.Add(self.button2, pos=(0, 3),span=(1,1),
                  flag=wx.ALL, border=20)
        #对按钮2进行事件绑定
        self.Bind(wx.EVT_BUTTON,self.OnChangeDepth,self.button2)
        #场景2
        self.button3 = wx.Button(self.bitmap,-1, label="场景2",size=(240, 120))
        sizer.Add(self.button3, pos=(1, 0),
                  flag=wx.ALL, border=20)
        self.button4 = wx.Button(self.bitmap,-1, label="配置参数",size=(100, 50))
        sizer.Add(self.button4, pos=(1, 3),span=(1,1),
                  flag=wx.ALL, border=20)
        #场景3
        self.button5 = wx.Button(self.bitmap,-1, label="场景2",size=(240, 120))
        sizer.Add(self.button5, pos=(2, 0),
                  flag=wx.ALL, border=20)
        self.button6 = wx.Button(self.bitmap,-1, label="配置参数",size=(100, 50))
        sizer.Add(self.button6, pos=(2, 3),span=(1,1),
                  flag=wx.ALL, border=20)
        #场景4
        self.button7 = wx.Button(self.bitmap,-1, label="场景2",size=(240, 120))
        sizer.Add(self.button7, pos=(3, 0),
                  flag=wx.ALL, border=20)
        self.button8 = wx.Button(self.bitmap,-1, label="配置参数",size=(100, 50))
        sizer.Add(self.button8, pos=(3, 3),span=(1,1),
                  flag=wx.ALL, border=20)

        self.bitmap.SetSizer(sizer)

    def on_end(self, event):
        self.Close()
    # 参数配置
    def OnChangeDepth(self, e):
        chgdep = ChangeDepthDialog(None)
        chgdep.ShowModal()
        chgdep.Destroy()


def main():
    app = wx.App()
    main_frame=MainFrame(None)
    main_frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

里面的image.jpg自己随便找张图然后改一下命名
在这里推荐几个wxpython的学习网址

http://thisis.yorven.site/blog/index.php/category/tutorial/wxpython-jiaocheng/
学习教程
https://wxpython.org/Phoenix/docs/html/index.html
https://www.yiibai.com/wxpython
可惜查询wxpython中的各个类

你可能感兴趣的:(wxPython,wxpython)