用户导向的位图更改



我正在使用Python 2.7进行编码,并且我正在尝试根据用户从不同窗口的下拉菜单中选择的单选按钮来更改背景位图图像。在附带的代码中,Bitmap0图像是默认的。通过选择"照片"菜单,然后选择"更改照片"菜单项,会出现一个单选框。我希望能够选择Bitmap1,并将位图图像更改为Bitmap1图像。

我尝试过pubsub,但没有成功,而且我对pubsub模块如何适合Python从来没有信心。这难道不可能通过正确的方式访问正确的变量吗?

我的简化代码如下:

import wx
IPaa0 = 0
bgphoto = "bitmap" + str(IPaa0) + ".bmp"
print bgphoto
_ID_Item0 = wx.NewId()
_ID_Item1 = wx.NewId()
_ID_Item2 = wx.NewId()
_ID_Item3 = wx.NewId()
_ID_Item4 = wx.NewId()
_ID_Item5 = wx.NewId()

class cPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
    def TogFot(self, fotnum):
        print "TogFot Entered"

class aFotoDir(wx.App):
    def OnInit(self):
        iMainFrame = cMainFrame("Top Frame", (0, 0), (1200, 900))
        iMainFrame.Show()
        self.SetTopWindow(iMainFrame)
        return True

class cMainFrame(wx.Frame):
    def __init__(self, title, pos, size):
        wx.Frame.__init__(self, None, -1, title, pos, size)
        iPanel = cPanel(self)

        MenuBar = wx.MenuBar()
        mFile = wx.Menu()
        MenuBar.Append(mFile, "File")
        mFoto = wx.Menu()
        MenuBar.Append(mFoto, "Photo")
        self.SetMenuBar(MenuBar)

        MenuItem0 = wx.Menu()
        mFile.Append(_ID_Item0, "Open")
        MenuItem1 = wx.Menu()
        mFile.Append(_ID_Item1, "Save")
        MenuItem2 = wx.Menu()
        mFile.Append(_ID_Item2, "Save As")
        MenuItem3 = wx.Menu()
        mFile.Append(_ID_Item3, "Close")
        MenuItem4 = wx.Menu()
        mFile.Append(_ID_Item4, "Exit")

        MenuItem5 = wx.Menu()
        mFoto.Append(_ID_Item5, "Change Photo")
        self.Bind(wx.EVT_MENU, self.ClickChFoto, id = _ID_Item5)

        img0 = wx.Image(bgphoto, wx.BITMAP_TYPE_ANY)
        img1 = wx.StaticBitmap(iPanel, -1, wx.BitmapFromImage(img0))

    def ClickChFoto(self, event):
        iSecondFrame = cSecondFrame("Input", (50, 0), (400, 300))
        iSecondFrame.Show()
        return True

class cSecondFrame(cMainFrame):
        def __init__(self, title, pos, size):
            wx.Frame.__init__(self, None, -1, title, pos, size)
            iSecondPanel = wx.Panel(self, -1)
            iBut0 = wx.Button(iSecondPanel, -1,     "OK", pos = ( 75, 200))
            iBut1 = wx.Button(iSecondPanel, -1, "CANCEL", pos = (225, 200))
            Plist = ["Bitmap0", "Bitmap1"]
            self.iWid0 = wx.RadioBox(iSecondPanel, -1, "Photo Selection", (50, 50),
                                     (200, 100), Plist, 1, wx.RA_SPECIFY_COLS)
            self.Bind(wx.EVT_BUTTON, self.ClickOK,     iBut0)
            self.Bind(wx.EVT_BUTTON, self.ClickCANCEL, iBut1)

        def ClickOK(self, event):
            print "OK Clicked"
            global IPaa0
            IPaa0 = self.iWid0.GetSelection()
            print IPaa0
            global bgphoto
            bgphoto = "bitmap" + str(IPaa0) + ".bmp"
            print bgphoto
            self.Close(True)

        def ClickCANCEL(self, event):
            print "CANCEL Clicked"
            self.Close(True)
#-------------------------------------------------------------------------------
if __name__ == "__main__":
    app = aFotoDir(False)
    app.MainLoop()

图像文件只是两个文件,标题为"Bitmap0"one_answers"Bitmap1"。我不知道上传图片的协议,而且每张图片的大小都超过6mb,所以我没有上传。

需要将cFramecSecondFrame的引用作为参数传递给cSecondFrame__init__。然后从它的def ClickOK中像以前一样加载新的位图,并在img1中设置它,并调用cFrameLayout()函数。

最新更新