作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
[问题]
如果用户单击 wx.TreeCtrl 中的某些项目,我想更新 wxStaticBitamp 图像。
[代码]
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()
[当前]
当我单击 root 时,会显示图像对象 (chooseMessageImage)。
[所需]
当我单击 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()
最佳答案
更改图像后添加 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
来自 wxpython 文档中有关 Refresh() 的内容:
"""
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.
"""
关于python - 如何更新 wxStaticBitmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33893287/
我是一名优秀的程序员,十分优秀!