wxPython中带图像的按钮不显示
我在Windows 7系统上使用Python 2.7和wxPython 3.0。在我的应用程序中,有一个面板叫做myPanel
。这个myPanel
的背景是一张名为green.bmp
的图片。myPanel
里面有一个按钮,叫做myButton
,这个按钮的背景也是一张图片,名为blue.bmp
。
问题:当我运行代码时,myButton
在myPanel
上看不见。我搞不清楚为什么它不显示。任何帮助都很感谢。如果我不在myPanel
上使用背景图片,那么myButton
就能显示出来!
代码:你可以从这里下载图片 Green.bmp 和 Blue.bmp。下面是代码片段:
import wx
class gui(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, None, id, title, size=(500,400))
myPanel = wx.Panel(self, -1, size=(300,200))
image_file1 = 'green.bmp'
image1 = wx.Image(image_file1, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#---- Comment out the line below then the button is visible
self.bitmap2 = wx.StaticBitmap(myPanel, -1, image1, (0, 0))
myButton = wx.Button(myPanel, -1, size =(30,30), pos=(20,20))
image_file2 = 'blue.bmp'
image2 = wx.Image(image_file2, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.bitmap1 = wx.StaticBitmap(myButton, -1, image2, (0, 0))
if __name__=='__main__':
app = wx.App()
frame = gui(parent=None, id=-1, title="Test")
frame.Show()
app.MainLoop()
谢谢你的时间!
1 个回答
1
我觉得这是个父子关系的问题。你需要把按钮的父级设置为 self.bitmap2,而不是 myPanel。否则,这两个控件会“堆叠”在一起。
可以参考以下教程:
你也可以使用 控件检查工具 来诊断这类问题。