我已经在这个项目上工作了一段时间了-它最初应该是一个测试,看看使用wxPython,我是否可以“从头开始”构建一个按钮。从头开始意味着:我将完全控制按钮的所有方面(即控制显示的BMP。。。事件处理程序所做的。。。等等)
我遇到了几个问题(因为这是我的第一个python项目)。在
dc = wx.BufferedPaintDC(self)
dc.SetFont(self.GetFont())
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
dc.Clear()
dc.DrawBitmap(wx.Bitmap("/home/wallter/Desktop/Mouseover.bmp"), 100, 100)
self.Refresh()
self.Update()
import wx
from wxPython.wx import *
class Custom_Button(wx.PyControl):
def __init__(self, parent, id, Pos, Over_BMP, Norm_BMP, Push_BMP, **kwargs):
wx.PyControl.__init__(self,parent, id, **kwargs)
self.Bind(wx.EVT_LEFT_DOWN, self._onMouseDown)
self.Bind(wx.EVT_LEFT_UP, self._onMouseUp)
self.Bind(wx.EVT_LEAVE_WINDOW, self._onMouseLeave)
self.Bind(wx.EVT_ENTER_WINDOW, self._onMouseEnter)
self.Bind(wx.EVT_ERASE_BACKGROUND,self._onEraseBackground)
self.Bind(wx.EVT_PAINT,self._onPaint)
self.pos = Pos
self.Over_bmp = Over_BMP
self.Norm_bmp = Norm_BMP
self.Push_bmp = Push_BMP
self._mouseIn = False
self._mouseDown = False
def _onMouseEnter(self, event):
self._mouseIn = True
def _onMouseLeave(self, event):
self._mouseIn = False
def _onMouseDown(self, event):
self._mouseDown = True
def _onMouseUp(self, event):
self._mouseDown = False
self.sendButtonEvent()
def sendButtonEvent(self):
event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, self.GetId())
event.SetInt(0)
event.SetEventObject(self)
self.GetEventHandler().ProcessEvent(event)
def _onEraseBackground(self,event):
# reduce flicker
pass
def Iz(self):
dc = wx.BufferedPaintDC(self)
dc.DrawBitmap(self.Norm_bmp, 100, 100)
def _onPaint(self, event):
# The printing functions, they should work... but don't.
dc = wx.BufferedPaintDC(self)
dc.SetFont(self.GetFont())
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
dc.Clear()
dc.DrawBitmap(self.Norm_bmp)
# This never printed... I don't know if that means if the EVT
# is triggering or what.
print '_onPaint'
# draw whatever you want to draw
# draw glossy bitmaps e.g. dc.DrawBitmap
if self._mouseIn: # If the Mouse is over the button
dc.DrawBitmap(self.Over_bmp, self.pos)
else: # Since the mouse isn't over it Print the normal one
# This is adding on the above code to draw the bmp
# in an attempt to get the bmp to display; to no avail.
dc.DrawBitmap(self.Norm_bmp, self.pos)
if self._mouseDown: # If the Mouse clicks the button
dc.DrawBitmap(self.Push_bmp, self.pos)
这个密码不管用?我没有显示BMP为什么?我怎么买?我已经得到了staticBitmap(...)
来显示一个,但它不会移动、调整大小或任何其他操作。。。-只在画面的左上角?在
注:框架为400pxl X 400pxl-和“/home/wallter/Desktop”/Mouseover.bmp““
你确定你的代码没有异常的工作,因为当我运行它时,我会遇到很多错误,阅读下面的要点,你应该有一个按钮,至少画正确
当O运行它时,它会出错,因为Custom_Button被传递的是空父代,而不是传递帧,例如
Custom_Button(self, ...)
您的drawBitmap调用也错误,它引发异常,而不是
dc.DrawBitmap(self.Norm_bmp)
,它应该是dc.DrawBitmap(self.Norm_bmp, 0, 0)
dc.DrawBitmap(self.Over_bmp, self.pos)
也抛出错误,因为pos应该是x,y不是元组,所以改为dc.DrawBitmap(self.Over_bmp, *self.pos)
最后你不需要做“从wxPython.wx版import*“相反,只需执行“from wx import*”而不是}使用
wxXXX
类名使用wx.XXX
,而不是{True
等这是我的工作代码
相关问题 更多 >
编程相关推荐