wxPython:图标、菜单、加速键、消息框

代码下载地址:wxPython:图标、菜单、加速键、消息框

运行结果展示

wxPython:图标、菜单、加速键、消息框_第1张图片

# _*_coding:utf-8_*_

import wx

import os

os.chdir('C:\Users\FXJ\Desktop' + u'\课程\多平台应用开发\作业' + '\wxPython\lab04')

class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, -1, u"第4次上机作业(WX)", size=(800, 500))
        self.panel = wx.Panel(self)

        self.icon1 = wx.Icon(name="icon1.ico",  type=wx.BITMAP_TYPE_ICO)
        self.icon2 = wx.Icon(name="icon2.ico", type=wx.BITMAP_TYPE_ICO)
        self.icon3 = wx.Icon(name="icon3.ico", type=wx.BITMAP_TYPE_ICO)
        self.SetIcon(self.icon1)
        self.word = wx.StaticText(self.panel, -1, u"", pos=(200, 100))
        self.word.SetLabel(u"当前使用的图标是:图标1")
        self.word.SetFont(wx.Font(15, wx.SWISS, wx.NORMAL, wx.BOLD, False))

        # Create the MenuBar
        self.menuBar = wx.MenuBar()

        # and a menu
        menu = wx.Menu()
        menu.Append(wx.ID_EXIT, u"E&xit\tCtrl+Shift+Delete", u"Exit this simple sample")
        # bind the menu event to an event handler
        self.Bind(wx.EVT_MENU, self.OnClose, id=wx.ID_EXIT)
        # and put the menu on the MenuBar
        self.menuBar.Append(menu, u"&File")

        # add a icon menu
        self.icon = wx.Menu()
        self.icon.Append(201, u"图标1\tCtrl+1", u"", wx.ITEM_RADIO)
        self.icon.Append(202, u"图标2\tCtrl+2", u"", wx.ITEM_RADIO)
        self.icon.Append(203, u"图标3\tCtrl+3", u"", wx.ITEM_RADIO)

        self.iSelectIcon = 201
        self.Bind(wx.EVT_MENU_RANGE, self.OnIcon, id=201, id2=203)
        self.menuBar.Append(self.icon,u"图标(&I)")
        self.menuBar.Check(self.iSelectIcon,True)
        self.SetBackgroundColour(u"Gray")

        self.show = wx.Menu()
        self.show.Append(301, u"显示1\tCtrl+Shift+1", u"", wx.ITEM_CHECK)
        self.show.Append(302, u"显示2\tCtrl+Shift+2", u"", wx.ITEM_CHECK)
        self.show.Append(303, u"显示3\tCtrl+Shift+3", u"", wx.ITEM_CHECK)
        self.show.Append(304, u"显示4\tCtrl+Shift+4", u"", wx.ITEM_CHECK)

        # 建立空列表s,在s里面添加4个wx.StaticText用来显示文字
        self.s= []
        self.s.append(wx.StaticText(self.panel, -1, u"", pos=(300, 200)))
        self.s.append(wx.StaticText(self.panel, -1, u"", pos=(400, 200)))
        self.s.append(wx.StaticText(self.panel, -1, u"", pos=(300, 250)))
        self.s.append(wx.StaticText(self.panel, -1, u"", pos=(400, 250)))

        self.menuBar.Append(self.show,u"显示(&D)")
        self.Bind(wx.EVT_MENU, self.OnShow, id=301, id2=304)
        self.iSelectShow = [False, False, False, False]


        # add another menu
        menu = wx.Menu()
        IdAbout = menu.Append(-1, u"程序信息(&I)\tF1", u"Help tip")
        self.Bind(wx.EVT_MENU, self.OnHelp, IdAbout)

        # and put the menu on the menubar
        self.menuBar.Append(menu, u"关于(&A)")
        self.SetMenuBar(self.menuBar)

        self.CreateStatusBar()
        self.Bind(wx.EVT_PAINT, self.OnPaint)


    def OnIcon(self, evt):
        item = evt.GetId()
        # word =wx.StaticText(self.panel, -1, u"", pos=(200, 100))
        if(wx.MessageBox(u"确定要修改么?", u"Confirmation", wx.YES_NO | wx.ICON_INFORMATION, self) == wx.YES):
            if(item == 201):
                self.SetIcon(self.icon1)
                self.iSelectIcon=201
                self.word.SetLabel(u"当前使用的图标是:图标1")
                self.GetMenuBar().EnableTop(2, True)
            elif(item==202):
                self.SetIcon(self.icon2)
                self.iSelectIcon=202
                self.word.SetLabel(u"当前使用的图标是:图标2")
                self.GetMenuBar().EnableTop(2, True)
            else:
                self.SetIcon(self.icon3)
                self.iSelectIcon=203
                self.word.SetLabel(u"当前使用的图标是:图标3")
                # 灰化第2个菜单项(显示)
                self.GetMenuBar().EnableTop(2,False)
        # 如果确认修改图标点否,恢复先前选择
        else:
            self.menuBar.Check(self.iSelectIcon, True)
        self.Refresh()

    def OnShow(self, evt):
        item = evt.GetId() - 301
        self.iSelectShow[item] = not self.iSelectShow[item]

        if(self.iSelectShow[item]):
            self.s[item].SetLabel(u"显示"+str(item+1))
        else:
            self.s[item].SetLabel(u"")
        self.Refresh()

    def OnHelp(self, evt):
        wx.MessageBox(u"第4次上机练习(WX)\n图标、菜单、加速键、消息框\n\n学号:***********\n姓名:***",
                      u"Lab4(WX)", wx.OK | wx.ICON_INFORMATION, self)

    def OnClose(self, evt):
        self.Close()


if __name__ == u'__main__':
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show(True)
    app.MainLoop()

你可能感兴趣的:(多平台应用开发)