【问题标题】:Button with an image not showing up in wxPython带有图像的按钮未在 wxPython 中显示
【发布时间】:2014-03-19 09:25:24
【问题描述】:

我在 Windows 7 操作系统上使用 python v2.7 和 wxPython v3.0。在我的应用程序中,我有一个名为myPanel 的面板。我在myPanel 上有一个图像作为背景,图像名称是green.bmp myPanel 包含一个名为myButton 的按钮。这个myButton 还包含一个名为blue.bmp 的图像作为背景

问题:当我执行我的代码时,我在myPanel 上看不到myButton。我无法弄清楚为什么它不起作用。任何帮助将不胜感激。 如果我不使用图像作为myPanel 的背景,那么myButton 是可见的!

代码:图片可以从这里Green.bmp和Blue.bmp下载。下面提供代码sn-p:

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()

感谢您的宝贵时间!

【问题讨论】:

    标签: python python-2.7 user-interface wxpython


    【解决方案1】:

    我认为这是育儿问题。您需要将按钮的父级设置为 self.bitmap2,而不是 myPanel。否则,这两个小部件将“堆叠”在彼此之上。

    请参阅以下教程:

    您也可以使用Widget Inspection Tool 来诊断这类事情

    【讨论】:

    • 哇,谢谢。你是蟒蛇专家!我也访问了你的博客,它非常适合像我这样的初学者。我需要大量练习。
    • Mike,如果你不介意的话,你也可以看看我的这个问题:stackoverflow.com/questions/21703514/…
    猜你喜欢
    • 2014-05-07
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 2019-09-11
    相关资源
    最近更新 更多