开发者

Python - Bitmap won't draw/display on button

I have been working on this project for some time now - it was originally supposed to be a test to see if, using wxPython, I could build a button 'from scratch.' From scratch means: that i would have full control over all the aspects of the button (i.e. controlling the BMP's that are displayed... what the event handlers did... etc.)

I have run into several problems (as this is my first major python project.) Now, when the all the code is working for the life开发者_Python百科 of me I can't get an image to display.

Basic code - not working

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

Full Main.py

import wx
from Custom_Button import Custom_Button
from wxPython.wx import *

ID_ABOUT = 101
ID_EXIT  = 102

class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wxFrame.__init__(self, parent, ID, title,
                         wxDefaultPosition, wxSize(400, 400))

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

        # The call for the 'Experiential button' 
        self.Button1 = Custom_Button(parent, -1, 
                                     wx.Point(100, 100),
                                     wx.Bitmap("/home/wallter/Desktop/Mouseover.bmp"),
                                     wx.Bitmap("/home/wallter/Desktop/Normal.bmp"),
                                     wx.Bitmap("/home/wallter/Desktop/Click.bmp"))

        # The following three lines of code are in place to try to get the 
        # Button1 to display (trying to trigger the Paint event (the _onPaint.)
        # Because that is where the 'draw' functions are. 
        self.Button1.Show(true)  
        self.Refresh()
        self.Update()

        # Because the Above three lines of code did not work, I added the 
        # following four lines to trigger the 'draw' functions to test if the 
        # '_onPaint' method actually worked.
        # These lines do not work.
        dc = wx.BufferedPaintDC(self)
        dc.SetFont(self.GetFont())
        dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
        dc.DrawBitmap(wx.Bitmap("/home/wallter/Desktop/Mouseover.bmp"), 100, 100)

        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(NULL, -1, "wxPython | Buttons")
        frame.Show(true)
        self.SetTopWindow(frame)
        return true

app = MyApp(0)
app.MainLoop()

Full CustomButton.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)

This code won't work? I get no BMP displayed why? How do i get one? I've gotten the staticBitmap(...) to display one, but it won't move, resize, or anything for that matter... - it's only in the top left corner of the frame?

Note: the frame is 400pxl X 400pxl - and the "/home/wallter/Desktop/Mouseover.bmp"


Are your sure you code is working without exceptions because when I run it i get many errors, read the points below and you should have a button which at least draws correctly

  1. When O run it it gives error because Custom_Button is passed NULL parent instead pass frame e.g. Custom_Button(self, ...)

  2. Your drawBitmap call is also wrong, it throws exception, instead of dc.DrawBitmap(self.Norm_bmp) it should be dc.DrawBitmap(self.Norm_bmp, 0, 0)

  3. dc.DrawBitmap(self.Over_bmp, self.pos) also throws error as pos should be x,y not a tuple so instead do dc.DrawBitmap(self.Over_bmp, *self.pos)

  4. and lastly you do not need to do "from wxPython.wx import *" instead just do "from wx import *" and instead of wxXXX class names use wx.XXX, instead of true use True etc

here is my working code

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜