如何更新wxStaticBitamp?

时间:2015-11-24 12:05:33

标签: python wxpython

[问题]
如果用户点击wx.TreeCtrl中的某个项目,我想更新wxStaticBitamp图像。

[CODE]

import wx
app = wx.App(redirect=False)
top = wx.Frame(None)
sizer = wx.GridBagSizer()

def createHelpWindow(event):
    import wx  
    locale = wx.Locale(wx.LANGUAGE_ENGLISH)
    frame = wx.Frame(None, wx.ID_ANY, " Help contents", style=wx.MINIMIZE_BOX | wx.CAPTION | wx.CLOSE_BOX) # A Frame is a top-level window.
    frame.Center()
    frame.SetBackgroundColour('LIGHTGREY')
    frame.SetSize(800,600)
    sizer = wx.GridBagSizer()

    # Add control tree
    treeHelp = wx.TreeCtrl(frame, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TR_DEFAULT_STYLE )
    sizer.Add(treeHelp,(1,1),(19,23),wx.EXPAND)

    # Add description box
    helpDisplay = wx.StaticText( frame, wx.ID_ANY, "", wx.DefaultPosition, wx.DefaultSize, 0, )
    sizer.Add(helpDisplay,(1,26),(1,1),wx.EXPAND)

    # Create image object
    chooseMessageImage = wx.StaticBitmap( frame, wx.ID_ANY, wx.Bitmap(wx.NullBitmap), wx.Point( 300,110 ),wx.DefaultSize, 0)
    def OnSelChanged(event):
        locale = wx.Locale(wx.LANGUAGE_ENGLISH)
        chooseMessageImage = wx.StaticBitmap( frame, wx.ID_ANY, wx.Bitmap(wx.NullBitmap), wx.Point( 300,110 ),wx.DefaultSize, 0)
        item =  event.GetItem()

        # Display the selected item text in the text widget
        print "selected ", treeHelp.GetItemText(item)
        if treeHelp.GetItemText(item) == 'TFT':
            helpSecondDisplay.SetLabel("""test page for landing page""")
            chooseMessageImage.SetBitmap(wx.NullBitmap) # Set image object to null
        #################### End main #########################
        elif treeHelp.GetItemText(item) == 'Main page':
            helpSecondDisplay.SetLabel("""test txt for main page""")
            chooseMessageImage.SetBitmap(wx.Bitmap( u"create_images/add_message.png", wx.BITMAP_TYPE_ANY)) # Set image to some src  
    treeHelp.Bind(wx.EVT_TREE_SEL_CHANGED, OnSelChanged)
    helpSecondDisplay = wx.StaticText( frame, wx.ID_ANY, "Lorem ipsum", wx.DefaultPosition, wx.DefaultSize, 0, )
    sizer.Add(helpSecondDisplay,(3,26),(1,1),wx.EXPAND)   
    root = treeHelp.AddRoot('TFT')
    rootMainPage = treeHelp.AppendItem(root, 'Main page') 
    treeHelp.Expand(root)

    # Wrap it up
    frame.SetSizer(sizer)
    frame.Center()    
    frame.Show(True)     # Show the frame.


# Bind the method
helpButton = wx.Button(top, wx.ID_ANY, u"help",  style=wx.BU_EXACTFIT )
sizer.Add(helpButton,(0,0),(1,1))
top.Bind(wx.EVT_BUTTON, createHelpWindow, helpButton)  
top.Sizer = sizer
top.Sizer.Fit(top)
top.Show()
app.MainLoop()

[CURRENT]
单击root时会显示图像对象(chooseMessageImage)。

[DESIRED]
单击root时,不应显示图像对象(chooseMessageImage)。

注意:加载窗口时,焦点将设置为root,但图像不会显示。

编辑1:

import wx
app = wx.App(redirect=False)
top = wx.Frame(None)
sizer = wx.GridBagSizer()

def createHelpWindow(event):
    import wx  
    locale = wx.Locale(wx.LANGUAGE_ENGLISH)
    frame = wx.Frame(None, wx.ID_ANY, " Help contents", style=wx.MINIMIZE_BOX | wx.CAPTION | wx.CLOSE_BOX) # A Frame is a top-level window.
    frame.Center()
    frame.SetBackgroundColour('LIGHTGREY')
    frame.SetSize(800,600)
    sizer = wx.GridBagSizer()

    # Add control tree
    treeHelp = wx.TreeCtrl(frame, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TR_DEFAULT_STYLE )
    sizer.Add(treeHelp,(1,1),(19,23),wx.EXPAND)

    # Add description box
    helpDisplay = wx.StaticText( frame, wx.ID_ANY, "", wx.DefaultPosition, wx.DefaultSize, 0, )
    sizer.Add(helpDisplay,(1,26),(1,1),wx.EXPAND)

    # Create image object
    chooseMessageImage = wx.StaticBitmap( frame, wx.ID_ANY, wx.NullBitmap, wx.Point( 300,110 ),wx.DefaultSize, 0)
    def OnSelChanged(event):
        locale = wx.Locale(wx.LANGUAGE_ENGLISH)
        chooseMessageImage = wx.StaticBitmap( frame, wx.ID_ANY, wx.NullBitmap, wx.Point( 300,110 ),wx.DefaultSize, 0)
        item =  event.GetItem()

        # Display the selected item text in the text widget
        print "selected ", treeHelp.GetItemText(item)
        if treeHelp.GetItemText(item) == 'TFT':
            helpSecondDisplay.SetLabel("""test page for landing page""")
            chooseMessageImage.SetBitmap(wx.NullBitmap) # Set image object to null
        #################### End main #########################
        elif treeHelp.GetItemText(item) == 'Main page':
            helpSecondDisplay.SetLabel("""test txt for main page""")
            chooseMessageImage.SetBitmap(wx.Bitmap( "create_images/add_message.png", wx.BITMAP_TYPE_ANY)) # Set image to some src
        chooseMessageImage.Refresh()
    treeHelp.Bind(wx.EVT_TREE_SEL_CHANGED, OnSelChanged)
    helpSecondDisplay = wx.StaticText( frame, wx.ID_ANY, "Lorem ipsum", wx.DefaultPosition, wx.DefaultSize, 0, )
    sizer.Add(helpSecondDisplay,(3,26),(1,1),wx.EXPAND)   
    root = treeHelp.AddRoot('TFT')
    rootMainPage = treeHelp.AppendItem(root, 'Main page') 
    treeHelp.Expand(root)

    # Wrap it up
    frame.SetSizer(sizer)
    frame.Center()    
    frame.Show(True)     # Show the frame.


# Bind the method
helpButton = wx.Button(top, wx.ID_ANY, u"help",  style=wx.BU_EXACTFIT )
sizer.Add(helpButton,(0,0),(1,1))
top.Bind(wx.EVT_BUTTON, createHelpWindow, helpButton)  
top.Sizer = sizer
top.Sizer.Fit(top)
top.Show()
app.MainLoop()

2 个答案:

答案 0 :(得分:2)

更改图像后添加Refresh()调用

#################### End main #########################
elif treeHelp.GetItemText(item) == 'Main page':
    helpSecondDisplay.SetLabel("""test txt for main page""")
    chooseMessageImage.SetBitmap(wx.Bitmap( "create_images/add_message.png", wx.BITMAP_TYPE_ANY)) # Set image to some src
chooseMessageImage.Refresh()

并将wx.Bitmap(wx.NullBitmap)更改为wx.NullBitmap

来自关于Refresh()的wxpython文档:

    """
    Refresh(self, bool eraseBackground=True, Rect rect=None)

    Mark the specified rectangle (or the whole window) as "dirty" so it
    will be repainted.  Causes an EVT_PAINT event to be generated and sent
    to the window.
    """

答案 1 :(得分:0)

问题解决了。最终的工作代码是:

import wx


app = wx.App(redirect=False)
top = wx.Frame(None)
sizer = wx.GridBagSizer()

def createHelpWindow(event):
    import wx
    locale = wx.Locale(wx.LANGUAGE_ENGLISH)
    frame = wx.Frame(None, wx.ID_ANY, " Help contents", style=wx.MINIMIZE_BOX | wx.CAPTION | wx.CLOSE_BOX) # A Frame is a top-level window.
    frame.Center()
    frame.SetBackgroundColour('LIGHTGREY')
    frame.SetSize(800,600)
    sizer = wx.GridBagSizer()
    # Add control tree
    treeHelp = wx.TreeCtrl(frame, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TR_DEFAULT_STYLE )
    sizer.Add(treeHelp,(1,1),(19,23),wx.EXPAND)
    # Add description box
    helpDisplay = wx.StaticText( frame, wx.ID_ANY, "", wx.DefaultPosition, wx.DefaultSize, 0, )
    sizer.Add(helpDisplay,(1,26),(1,1),wx.EXPAND)
    # Create image object
    chooseMessageImage = wx.StaticBitmap( frame, wx.ID_ANY, wx.NullBitmap, wx.Point( 300,110 ),wx.DefaultSize, 0)
    def OnSelChanged(event):
        locale = wx.Locale(wx.LANGUAGE_ENGLISH)
        item =  event.GetItem()
        # Display the selected item text in the text widget
        print "selected ", treeHelp.GetItemText(item)
        if treeHelp.GetItemText(item) == 'TFT':
            helpSecondDisplay.SetLabel("""test page for landing page""")
            chooseMessageImage.SetBitmap(wx.NullBitmap) # Set image object to null
        #################### End main #########################
        elif treeHelp.GetItemText(item) == 'Main page':
            helpSecondDisplay.SetLabel("""test txt for main page""")
            chooseMessageImage.SetBitmap(wx.Bitmap( "create_images/add_message.png", wx.BITMAP_TYPE_ANY)) # Set image to some src
            frame.Refresh()
    treeHelp.Bind(wx.EVT_TREE_SEL_CHANGED, OnSelChanged)
    helpSecondDisplay = wx.StaticText( frame, wx.ID_ANY, "Lorem ipsum", wx.DefaultPosition, wx.DefaultSize, 0, )
    sizer.Add(helpSecondDisplay,(3,26),(1,1),wx.EXPAND)
    root = treeHelp.AddRoot('TFT')
    rootMainPage = treeHelp.AppendItem(root, 'Main page')
    treeHelp.Expand(root)
    # Wrap it up
    frame.SetSizer(sizer)
    frame.Center()
    frame.Show(True)     # Show the frame.


# Bind the method
helpButton = wx.Button(top, wx.ID_ANY, u"help",  style=wx.BU_EXACTFIT )
sizer.Add(helpButton,(0,0),(1,1))
top.Bind(wx.EVT_BUTTON, createHelpWindow, helpButton)
top.Sizer = sizer
top.Sizer.Fit(top)
top.Show()
app.MainLoop()

我的错误是我在OnSelChanged方法中创建了bitmapObj。