首页 > 编程语言 > 详细

弥补wxpython无背景图片缺陷

时间:2014-08-17 16:57:52      阅读:500      评论:0      收藏:0      [点我收藏+]

思路:

通过设置Panel的背景样式为wx.BG_STYLE_CUSTOM:

self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)


绑定Panel的背景事情:

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



在绑定方法中

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

dc.DrawBitmap(bmp, 0, 0)



画出背景图片,从而实现给Panel容器添加背景图片
 

完整代码如下:

import wx

########################################################################
class MainPanel(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)

#----------------------------------------------------------------------
def OnEraseBackground(self, evt):
"""
Add a picture to the background
"""
# yanked from ColourDB.py
dc = evt.GetDC()

if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap("butterfly.jpg")
dc.DrawBitmap(bmp, 0, 0)

########################################################################
class MainFrame(wx.Frame):
""""""

#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, size=(600,450))
panel = MainPanel(self)
self.Center()

########################################################################
class Main(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()

可以尝试运行一下 :)

弥补wxpython无背景图片缺陷,布布扣,bubuko.com

弥补wxpython无背景图片缺陷

原文:http://www.cnblogs.com/yjken/p/3917928.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!