wxpython设置背景图片_弥补wxpython无背景图片缺陷

本文介绍如何在wxpython应用中设置背景图片,通过覆盖默认背景样式并绑定`wx.EVT_ERASE_BACKGROUND`事件来实现在窗口上显示图片。
摘要由CSDN通过智能技术生成

importwx########################################################################

classMainPanel(wx.Panel):""""""

#----------------------------------------------------------------------

def __init__(self, parent):"""Constructor"""wx.Panel.__init__(self, parent=parent)

self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)

self.frame=parent

sizer=wx.BoxSizer(wx.VERTICAL)

hSizer=wx.BoxSizer(wx.HORIZONTAL)for num in range(4):

label= "Button %s" %num

btn= wx.Button(self, label=label)

sizer.Add(btn, 0, wx.ALL,5)

hSizer.Add((1,1), 1, wx.EXPAND)

hSizer.Add(sizer, 0, wx.TOP,100)

hSizer.Add((1,1), 0, wx.ALL, 75)

self.SetSizer(hSizer)

self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)#----------------------------------------------------------------------

defOnEraseBackground(self, evt):"""Add a picture to the background"""

#yanked from ColourDB.py

dc =evt.GetDC()if notdc:

dc=wx.ClientDC(self)

rect=self.GetUpdateRegion().GetBox()

dc.SetClippingRect(rect)

dc.Clear()

bmp= wx.Bitmap("butterfly.jpg")

dc.DrawBitmap(bmp, 0, 0)########################################################################

classMainFrame(wx.Frame):""""""

#----------------------------------------------------------------------

def __init__(self):"""Constructor"""wx.Frame.__init__(self, None, size=(600,450))

panel=MainPanel(self)

self.Center()########################################################################

classMain(wx.App):""""""

#----------------------------------------------------------------------

def __init__(self, redirect=False, filename=None):"""Constructor"""wx.App.__init__(self, redirect, filename)

dlg=MainFrame()

dlg.Show()#----------------------------------------------------------------------

if __name__ == "__main__":

app=Main()

app.MainLoop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值