Python位图不会在按钮上绘制/显示

2024-06-26 00:10:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在这个项目上工作了一段时间了-它最初应该是一个测试,看看使用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()

满的主.py在

^{pr2}$

满的自定义按钮.py在

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““


Tags: thetoselfeventnormbinddefdc
1条回答
网友
1楼 · 发布于 2024-06-26 00:10:33

你确定你的代码没有异常的工作,因为当我运行它时,我会遇到很多错误,阅读下面的要点,你应该有一个按钮,至少画正确

  1. 当O运行它时,它会出错,因为Custom_Button被传递的是空父代,而不是传递帧,例如Custom_Button(self, ...)

  2. 您的drawBitmap调用也错误,它引发异常,而不是dc.DrawBitmap(self.Norm_bmp),它应该是dc.DrawBitmap(self.Norm_bmp, 0, 0)

  3. dc.DrawBitmap(self.Over_bmp, self.pos)也抛出错误,因为pos应该是x,y不是元组,所以改为dc.DrawBitmap(self.Over_bmp, *self.pos)

  4. 最后你不需要做“从wxPython.wx版import*“相反,只需执行“from wx import*”而不是wxXXX类名使用wx.XXX,而不是{}使用True

这是我的工作代码

from wx import *

ID_ABOUT = 101
ID_EXIT  = 102


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, 0, 0)

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

class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title,
                         wx.DefaultPosition, wx.Size(400, 400))

        self.CreateStatusBar()
        self.SetStatusText("Program testing custom button overlays")
        menu = wx.Menu()
        menu.Append(ID_ABOUT, "&About", "More information about this program")
        menu.AppendSeparator()
        menu.Append(ID_EXIT, "E&xit", "Terminate the program")
        menuBar = wx.MenuBar()
        menuBar.Append(menu, "&File");
        self.SetMenuBar(menuBar)

        # The call for the 'Experiential button' 
        s = r"D:\virtual_pc\mockup\mockupscreens\embed_images\toolbar\options.png"
        self.Button1 = Custom_Button(self, -1, 
                                     wx.Point(100, 100),
                                     wx.Bitmap(s),
                                     wx.Bitmap(s),
                                     wx.Bitmap(s))

        self.Button1.Show(True)  

        EVT_MENU(self, ID_ABOUT, self.OnAbout)
        EVT_MENU(self, ID_EXIT,  self.TimeToQuit)

    def OnAbout(self, event):
        dlg = wxMessageDialog(self, "Testing the functions of custom "
                              "buttons using pyDev and wxPython",
                              "About", wxOK | wxICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()


    def TimeToQuit(self, event):
        self.Close(true)



class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, "wxPython | Buttons")
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

app = MyApp(0)
app.MainLoop()

相关问题 更多 >