⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 artprovider.py

📁 用wxPython编写GUI程序的样例代码
💻 PY
📖 第 1 页 / 共 4 页
字号:

import  cStringIO
import  wx

#----------------------------------------------------------------------

ArtClients = [ "wx.ART_TOOLBAR",
               "wx.ART_MENU",
               "wx.ART_FRAME_ICON",
               "wx.ART_CMN_DIALOG",
               "wx.ART_HELP_BROWSER",
               "wx.ART_MESSAGE_BOX",
               "wx.ART_OTHER",
               ]

ArtIDs = [ "wx.ART_ADD_BOOKMARK",
           "wx.ART_DEL_BOOKMARK",
           "wx.ART_HELP_SIDE_PANEL",
           "wx.ART_HELP_SETTINGS",
           "wx.ART_HELP_BOOK",
           "wx.ART_HELP_FOLDER",
           "wx.ART_HELP_PAGE",
           "wx.ART_GO_BACK",
           "wx.ART_GO_FORWARD",
           "wx.ART_GO_UP",
           "wx.ART_GO_DOWN",
           "wx.ART_GO_TO_PARENT",
           "wx.ART_GO_HOME",
           "wx.ART_FILE_OPEN",
           "wx.ART_FILE_SAVE",
           "wx.ART_FILE_SAVE_AS",
           "wx.ART_PRINT",
           "wx.ART_HELP",
           "wx.ART_TIP",
           "wx.ART_REPORT_VIEW",
           "wx.ART_LIST_VIEW",
           "wx.ART_NEW_DIR",
           "wx.ART_HARDDISK",
           "wx.ART_FLOPPY",
           "wx.ART_CDROM",
           "wx.ART_REMOVABLE",
           "wx.ART_FOLDER",
           "wx.ART_FOLDER_OPEN",
           "wx.ART_GO_DIR_UP",
           "wx.ART_EXECUTABLE_FILE",
           "wx.ART_NORMAL_FILE",
           "wx.ART_TICK_MARK",
           "wx.ART_CROSS_MARK",
           "wx.ART_ERROR",
           "wx.ART_QUESTION",
           "wx.ART_WARNING",
           "wx.ART_INFORMATION",
           "wx.ART_MISSING_IMAGE",
           "wx.ART_COPY",
           "wx.ART_CUT",
           "wx.ART_PASTE",
           "wx.ART_DELETE",
           "wx.ART_NEW",
           "wx.ART_UNDO",
           "wx.ART_REDO",
           "wx.ART_QUIT",
           "wx.ART_FIND",
           "wx.ART_FIND_AND_REPLACE",
           ]


#----------------------------------------------------------------------

class MyArtProvider(wx.ArtProvider):
    def __init__(self, log):
        wx.ArtProvider.__init__(self)
        self.log = log

    def CreateBitmap(self, artid, client, size):
        # You can do anything here you want, such as using the same
        # image for any size, any client, etc., or using specific
        # images for specific sizes, whatever...

        # See end of file for the image data

        bmp = wx.NullBitmap
        # use this one for all 48x48 images
        if size.width == 48:
            bmp = makeBitmap(smile48_png)

        # but be more specific for these
        elif size.width == 16 and artid == wx.ART_ADD_BOOKMARK:
            bmp = makeBitmap(smile16_png)
        elif size.width == 32 and artid == wx.ART_ADD_BOOKMARK:
            bmp = makeBitmap(smile32_png)

        # and just ignore the size for these
        elif artid == wx.ART_GO_BACK:
            bmp = makeBitmap(left_png)
        elif artid == wx.ART_GO_FORWARD:
            bmp = makeBitmap(right_png)
        elif artid == wx.ART_GO_UP:
            bmp = makeBitmap(up_png)
        elif artid == wx.ART_GO_DOWN:
            bmp = makeBitmap(down_png)
        elif artid == wx.ART_GO_TO_PARENT:
            bmp = makeBitmap(back_png)

        elif artid == wx.ART_CROSS_MARK:
            bmp = makeBitmap(cross_png)
        elif artid == wx.ART_TICK_MARK:
            bmp = makeBitmap(tick_png)

        if bmp.Ok():
            self.log.write("MyArtProvider: providing %s:%s at %s\n" %(artid, client, size))
        return bmp



class TestPanel(wx.Panel):
    def __init__(self, parent, log):
        wx.Panel.__init__(self, parent, -1)
        self.log = log

        sizer = wx.BoxSizer(wx.VERTICAL)

        title = wx.StaticText(self, -1, "ArtProvider")
        title.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
        sizer.Add(title, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
        sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        fgs = wx.FlexGridSizer(0, 3, 10, 10)

        combo = wx.ComboBox(self, -1, "", choices = ArtClients,
                           style = wx.CB_DROPDOWN|wx.CB_READONLY)
        fgs.Add(combo, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        self.Bind(wx.EVT_COMBOBOX, self.OnSelectClient, combo)
        combo.Select(0)

        combo = wx.ComboBox(self, -1, "", choices = ArtIDs,
                           style = wx.CB_DROPDOWN|wx.CB_READONLY)
        fgs.Add(combo, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        self.Bind(wx.EVT_COMBOBOX, self.OnSelectID, combo)
        combo.Select(0)

        cb = wx.CheckBox(self, -1, "Use custom provider")
        fgs.Add(cb, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        self.Bind(wx.EVT_CHECKBOX, self.OnUseCustom, cb)

        fgs.Add((10, 10), 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        fgs.Add((10, 10), 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        fgs.Add((10, 10), 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        box = wx.BoxSizer(wx.VERTICAL)
        bmp = wx.EmptyBitmap(16,16)
        self.bmp16 = wx.StaticBitmap(self, -1, bmp)
        box.Add(self.bmp16, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        text = wx.StaticText(self, -1, "16x16")
        box.Add(text, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        fgs.Add(box, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        box = wx.BoxSizer(wx.VERTICAL)
        bmp = wx.EmptyBitmap(32,32)
        self.bmp32 = wx.StaticBitmap(self, -1, bmp)
        box.Add(self.bmp32, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        text = wx.StaticText(self, -1, "32x32")
        box.Add(text, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        fgs.Add(box, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        box = wx.BoxSizer(wx.VERTICAL)
        bmp = wx.EmptyBitmap(48,48)
        self.bmp48 = wx.StaticBitmap(self, -1, bmp)
        box.Add(self.bmp48, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        text = wx.StaticText(self, -1, "48x48")
        box.Add(text, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        fgs.Add(box, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(fgs, 0, wx.ALL, 5)
        self.SetSizer(sizer)

        self.client = eval(ArtClients[0])
        self.artid = eval(ArtIDs[0])
        self.getArt()


    def OnSelectClient(self, evt):
        self.log.write("OnSelectClient\n")
        self.client = eval(evt.GetString())
        self.getArt()


    def OnSelectID(self, evt):
        self.log.write("OnSelectID\n")
        self.artid = eval(evt.GetString())
        self.getArt()


    def OnUseCustom(self, evt):
        if evt.IsChecked():
            self.log.write("Images will now be provided by MyArtProvider\n")
            wx.ArtProvider.Push( MyArtProvider(self.log) )
        else:
            self.log.write("MyArtProvider deactivated\n")
            wx.ArtProvider.Pop()
        self.getArt()


    def getArt(self):
        self.log.write("Getting art for %s:%s\n" % (self.client, self.artid))

        bmp = wx.ArtProvider.GetBitmap(self.artid, self.client, (16,16))

        if not bmp.Ok():
            bmp = wx.EmptyBitmap(16,16)
            self.clearBmp(bmp)

        self.bmp16.SetBitmap(bmp)

        bmp = wx.ArtProvider.GetBitmap(self.artid, self.client, (32,32))

        if not bmp.Ok():
            bmp = wx.EmptyBitmap(32,32)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -