gpt4 book ai didi

python - 如何更新 wxStaticBitmap?

转载 作者:行者123 更新时间:2023-12-01 04:19:21 25 4
gpt4 key购买 nike

[问题]
如果用户单击 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com