wx.Frame.Refresh won't send wx.EVT_PAINT - python

Linux Mint 17, Python 2.7, wx '2.8.12.1'
I am new to wx so, probably, I can't make right searh queue.
I've read through
Dialog's ShowModal() won't send EVT_PAINT
and tried to replace self.pnl with self, but it obviously won't work.
The code is from "OpenCV with Python Blueprints" book, see pages 19-21.
I've made ugly patch to make it work (marked in code), but curious about this situation.
import cv2
import wx
class BaseLayout(wx.Frame):
def __init__(self, parent, id, title, capture, fps=24):
self.fps=fps
self.capture = capture
_, frame = self.capture.read()
self.imgHeight,self.imgWidth = frame.shape[:2]
self.bmp = wx.BitmapFromBuffer(self.imgWidth,
self.imgHeight, frame)
wx.Frame.__init__(self, parent, id, title,size=(self.imgWidth, self.imgHeight+20))
self._init_base_layout()
self._create_base_layout()
def _init_base_layout(self):
self.timer = wx.Timer(self)
self.timer.Start(1000./self.fps)
self.Bind(wx.EVT_TIMER, self._on_next_frame)
self.Bind(wx.EVT_PAINT, self._on_paint)
def _on_next_frame(self, event):
ret, frame = self.capture.read()
if ret:
frame = self._process_frame(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
self.bmp.CopyFromBuffer(frame)
self.Refresh(eraseBackground=False)#this intended to work
deviceContext = wx.BufferedPaintDC(self.pnl)#this works (ugly patch from on_paint)
deviceContext.DrawBitmap(self.bmp, 0, 0)#this works(ugly patch from on_paint)
def _on_paint(self, event):
print('I can never see this line')
deviceContext = wx.BufferedPaintDC(self.pnl)
deviceContext.DrawBitmap(self.bmp, 0, 0)
def _create_base_layout(self):
self.pnl = wx.Panel(self, -1,size=(self.imgWidth, self.imgHeight))
self.pnl.SetBackgroundColour(wx.BLACK)
self.panels_vertical = wx.BoxSizer(wx.VERTICAL)
self.panels_vertical.Add(self.pnl, 1, flag=wx.EXPAND)
self.SetMinSize((self.imgWidth, self.imgHeight))
self.SetMaxSize((self.imgWidth, self.imgHeight))
self.SetSizer(self.panels_vertical)
self.Centre()
def _process_frame(self, frame):
return frame#stub
def main():
capture = cv2.VideoCapture("ScreenFlow9.mov")
if not(capture.isOpened()):
capture.open()
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
app = wx.App()
layout = BaseLayout(None, -1, 'Test', capture)
layout.Show(True)
app.MainLoop()
main()

The frame doesn't get an EVT_PAINT event because none of the frame's client area is visible. It is completely covered by the panel.

Related

How can I display data stream in a simple gui using pymavlink and wxpython? (first time using python)

I am trying to figure out how to create a basic GUI to display altitude telemetry of an RC plane in real time. So far I have successfully written code that prints out the live data in the terminal. My question now is how do i take that live data from the terminal and instead display it in a GUI in real time. I took the listen.py code and turned it into a def planeoutput(): and used a generator to display values in the GUI window but it wasn't displaying.
Here is a video that I found that shows what I want to do: https://www.youtube.com/watch?v=P4GBUcvMrDA
This is the code I have that listens for the altitude data and displays it in the terminal when it is compiled:
# Purpose: to get live telemetry from vehicle and display it as the output
#def planeoutput():
# https://mavlink.io/en/mavgen_python/
from pymavlink import mavutil
#x = 0
# Start a connection listening on a UDP port
the_connection = mavutil.mavlink_connection('udpin:localhost:14551') #make a user prompt to ask for COM port later on
# Wait for the first heartbeat
# This sets the system and component ID of remote system for the link
the_connection.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" % (the_connection.target_system, the_connection.target_component)) # tells us if we are connected to vehicle
# Once connected, use 'the_connection' to get and send messages
while 1:
altitude = the_connection.recv_match(type='GLOBAL_POSITION_INT',blocking=True) # locates GPS data and streams it to altitude variable
print("Planes Altitude:",round(altitude.relative_alt/304.8),"ft") #This is alt object not a dict
This is the output when I run the above code:
output of listen.py
This is the basic GUI code that I have so far:
import wx
class Window(wx.Frame):
def __init__(self, title):
super().__init__(parent = None, title = title, size = (420, 300))
self.panel = wx.Panel(self)
wx.StaticLine(self.panel, pos=(20, 240), size=(360,1), style = wx.HORIZONTAL)
content1 = "Altitude:" #live data would display here
text1 = wx.StaticText(self.panel, label = content1, pos = (60, 100))
wx.StaticLine(self.panel, pos=(20, 20), size=(360,1), style = wx.LI_HORIZONTAL)
self.Centre()
self.Show()
app = wx.App()
window = Window("Ground Control Station")
app.MainLoop()
The easiest way, is to use a multi-line wx.TextCtrl and simply write the results in.
I suspect that you'll want to investigate running your routine as a separate thread and have a method of stopping it, mid-simulation but those are different questions.
import wx
import time
def LongSimulation(self,_input):
# Simulate the behaviour of the simulation
for i in range(0, 1000, 100):
self.log.write("Planes Altitude: "+str(i)+" ft\n")
time.sleep(1)
wx.GetApp().Yield()
for i in range(i, 0, -100):
self.log.write("Planes Altitude: "+str(i)+" ft\n")
time.sleep(1)
wx.GetApp().Yield()
return 'Landed\n'
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL
sizer = wx.BoxSizer(wx.VERTICAL)
self.log = wx.TextCtrl(panel, wx.ID_ANY, size=(300,100), style=style)
sizer.Add(self.log, 1, wx.ALL|wx.EXPAND, 5)
btn = wx.Button(panel, wx.ID_ANY, 'Start')
self.Bind(wx.EVT_BUTTON, self.onButton, btn)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
self.Bind(wx.EVT_CLOSE, self.onQuit)
def onButton(self, event):
_input = 0
self.log.write("Takeoff!\n")
res = LongSimulation(self,_input)
self.log.write(res)
def onQuit(self, event):
self.Destroy()
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()

wxPython - cannot hide wxFrame for a desktop screenshot

I'm very new to wxPython and working on a desktop screenshot utility. In short, user selects a rectangular region on their screen from and produce an image from it as most screenshot tools already do. Its all working except for the fact that I cannot get the main frame, and all it contains, to hide or close, so the screenshot image produced contains the overlay mask and dashed selection box.
FYI, screenshots are triggered by pressing the ENTER key. I have tried a vast number of ways to achieve this without luck. Hiding the frame, or destroying the panel first etc. makes no difference. However, if I break things up to two steps with button to proceed it all works fine so I am betting it's something small. I tried Refresh on the frame and panel as well. For what it's worth, my desktop environment if Linux KDE (latest ArchLinux rolling release).
Here is my code in full.
#!/usr/bin/env python2
import wx, time
#--------------------------------------------------------------------------------------------------------#
class SelectableFrame(wx.Frame):
c1 = None
c2 = None
def __init__(self, parent=None, id=-1, title=""):
wx.Frame.__init__(self, parent, id, title, pos=(0, 0), size=wx.DisplaySize(), style=wx.FRAME_NO_TASKBAR | wx.NO_BORDER | wx.STAY_ON_TOP)
self.panel = wx.Panel(self, size=self.GetSize())
self.panel.Bind(wx.EVT_MOTION, self.OnMouseMove)
self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnMouseSelect)
self.panel.Bind(wx.EVT_RIGHT_DOWN, self.OnReset)
self.panel.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
self.panel.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
self.panel.SetBackgroundColour(wx.Colour(0, 0, 0))
self.panel.SetBackgroundStyle(wx.BG_STYLE_COLOUR)
self.SetTransparent(150)
def OnClose(self, event):
self.Destroy()
def OnMouseMove(self, event):
if event.Dragging() and event.LeftIsDown():
self.c2 = event.GetPosition()
self.Refresh()
def OnMouseSelect(self, event):
self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
self.c1 = event.GetPosition()
def OnMouseUp(self, event):
self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
def OnKeyDown(self, event):
key = event.GetKeyCode()
if key == wx.WXK_ESCAPE:
self.Close()
elif (key == wx.WXK_RETURN or key == wx.WXK_NUMPAD_ENTER) and self.RegionSelected():
self.TakeScreenshot()
event.Skip()
def OnReset(self, event=None):
wx.PaintDC(self.panel).Clear()
self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
def OnPaint(self, event):
if not self.RegionSelected():
return
dc = wx.PaintDC(self.panel)
dc.SetPen(wx.Pen("white", 1, wx.LONG_DASH))
dc.SetBrush(wx.Brush(wx.Colour(100, 100, 100), wx.SOLID))
dc.DrawRectangle(self.c1.x, self.c1.y, self.c2.x - self.c1.x, self.c2.y - self.c1.y)
def RegionSelected(self):
if self.c1 is None or self.c2 is None:
return False
else:
return True
def TakeScreenshot(self):
if not self.RegionSelected():
return
wx.PaintDC(self.panel).Clear()
self.Hide()
self.Refresh()
dcScreen = wx.ScreenDC()
bmp = wx.EmptyBitmap(self.c2.x - self.c1.x, self.c2.y - self.c1.y)
memDC = wx.MemoryDC()
memDC.SelectObject(bmp)
memDC.Blit(0, 0, self.c2.x - self.c1.x, self.c2.y - self.c1.y, dcScreen, self.c1.x, self.c1.y)
memDC.SelectObject(wx.NullBitmap)
img = bmp.ConvertToImage()
filename = "/tmp/ocr-screenshot-" + time.strftime('%Y%m%d-%H%M%S') + ".png"
img.SaveFile(filename, wx.BITMAP_TYPE_PNG)
self.Close()
#--------------------------------------------------------------------------------------------------------
class SelectableApp(wx.App):
def OnInit(self):
self.frame = SelectableFrame()
self.frame.Show(True)
self.SetTopWindow(self.frame)
return True
#--------------------------------------------------------------------------------------------------------#
app = SelectableApp(False)
app.MainLoop()

Dynamic text on top of wx.SplashScreen

I am using wxPython to create a splash screen. It actually works great, but now I want to put some loading feedback on the splashscreen. Does anyone know how I can put a dynamic label on top of the splashscreen? Here is the code I am using:
class myFrame(wxFrame):
wx.Frame.__init__(self, parent, -1, _("Radar"),
size=(800, 600), style=wx.DEFAULT_FRAME_STYLE)
class MySplash(wx.SplashScreen):
def __init__(self, parent=None):
aBitmap = wx.Image(name=VarFiles["Radar_Splash"]).ConvertToBitmap()
splashStyle = wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT
splashDuration = 12000 # ms
wx.SplashScreen.__init__(self, aBitmap, splashStyle, splashDuration, parent)
self.Bind(wx.EVT_CLOSE, self.CloseSplash)
wx.Yield()
def CloseSplash(self, evt):
self.Hide()
global frame
frame = myFrame(parent=None)
app.SetTopWindow(frame)
frame.Show(True)
evt.Skip()
class MyAwesomeApp(wx.App):
def OnInit(self):
MySplash = MySplash()
MySplash.Show()
return True
As the SplashScreen is essentially a window displaying a bitmap, you will need to modify the provided bitmap, doing the layouting yourself.
def _draw_bmp(bmp, txt_lst):
dc = wx.MemoryDC()
dc.SelectObject(bmp)
dc.Clear()
gc = wx.GraphicsContext.Create(dc)
font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
gc.SetFont(font)
for i, line in enumerate(txt_lst):
dc.DrawText(line, 10, i * 20 + 15)
dc.SelectObject(wx.NullBitmap)
# ...
aBitmap = #...
_draw_bmp(aBitmap, ['splash', 'screen', 'text'])
The wx.GraphicsContext will be helpful to have antialiased text looking the same as in the underlying OS.

wxPython Background image on frame

MMGP Answerd but wont let me credit him right ;-) So I will at least mention him here.
(And I did finally get to credit him... 8-)
His linked discussion on Double Buffering provided a base code that worked with the following modifications:
Insert this beginning at line 106 (overwritting existing code until you get to the last line shown here):
# Here's the actual drawing code.
cliWidth, cliHeight = self.GetClientSize()
bmp=wx.Bitmap("Logo16x9.png")
bmpWide = bmp.GetWidth()
bmpHeight = bmp.GetHeight()
img = bmp.ConvertToImage()
scaleFactor = cliWidth/bmpWide
bmp = wx.BitmapFromImage(img.Scale(int(bmpWide * scaleFactor), int(bmpHeight * scaleFactor)))
bmpWide = bmp.GetWidth()
bmpHeight = bmp.GetHeight()
xPos = (cliWidth - (bmpWide))/2
yPos = (cliHeight - (bmpHeight))/2
# altered by me
dc.DrawBitmap(bmp, xPos, yPos)
class TestFrame(wx.Frame):
I've been beating my head against this all day.
I'm new to drawing graphics with the wxPython modules, and when I needed to draw a background image on a frame I found this code which works well if the image is the full size of the window.
However, I need to place a company logo as the background, and have it remain centered through resizes. In it's current form the resize causes a graphic artifact the size of a small nation to appear on the screen with any resize event.
The logo image file (used on line 43 of the code) is a 400x300 (WxH) image.
I am looking for a way to either: resize my image on the fly to match wx.GetClientSize(),
or a way to avoid/remove the artifact. Preferably without involving PIL or ImageMagick. App has to function on a local level only, and be system agnostic (Win, Lin and Mac), none of this involves network activities or mapped drives.
Python 2.7 and wxPython 2.8
The code I am using (with my modification annotated) is as follows:
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()
# Added by me
cliWidth, cliHeight = self.GetClientSize()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
# use a 400x300 image
bmp = wx.Bitmap("Logo4x3.png")
# added by me
xPos = (cliWidth - 400)/2
yPos = (cliHeight - 300)/2
# altered by me
dc.DrawBitmap(bmp, xPos, yPos)
#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()
Update: Latest Failure - Modified lines 37 to 52
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
# use a 400x300 image
bmp = wx.Bitmap("Logo4x3.png")
img = bmp.ConvertToImage()
scaleFactor = cliWidth/400
bmp = wx.BitmapFromImage(img.Scale(int(400*scaleFactor),int(300*scaleFactor)))
# added by me
#xPos = (cliWidth - 400)/2
#yPos = (cliHeight - 300)/2
# altered by me
#dc.DrawBitmap(bmp, xPos, yPos)
dc.DrawBitmap(bmp, 0, 0)
Another attempt and another fail. No difference in the output to screen. Additionally, the referenced document on double buffering does not address this issue, but does suffer from the same result. This code modifies lines 36 through 57 of the original.
brsh = wx.Brush('#000000')
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.SetBackground(brsh)
dc.SetDeviceOrigin(0,0)
dc.DestroyClippingRegion()
dc.Clear()
# use a 400x300 image
bmp = wx.Bitmap("Logo4x3.png")
img = bmp.ConvertToImage()
scaleFactor = cliWidth/400
bmp = wx.BitmapFromImage(img.Scale(int(400*scaleFactor),int(300*scaleFactor)))
# added by me
#xPos = (cliWidth - 400)/2
#yPos = (cliHeight - 300)/2
# altered by me
#dc.DrawBitmap(bmp, xPos, yPos)
dc.DrawBitmap(bmp, 0, 0)
From the comments I suggested using double buffered drawing, but I didn't see that in the edited post. Also, I had several drawing issues when self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) was used. But this line might be helpful in other systems beyond mine, so I wanted to keep it. So, in order to handle the situation, here is a updated code that uses double buffered drawing and works fine here:
import wx
class MainPanel(wx.Panel):
def __init__(self, parent, bg_img='Logo4x3.png'):
wx.Panel.__init__(self, parent=parent)
self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
self.bg = wx.Bitmap(bg_img)
self._width, self._height = self.bg.GetSize()
sizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
for num in range(4):
btn = wx.Button(self, label="Button %s" % num)
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_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def OnSize(self, size):
self.Layout()
self.Refresh()
def OnEraseBackground(self, evt):
pass
def OnPaint(self, evt):
dc = wx.BufferedPaintDC(self)
self.Draw(dc)
def Draw(self, dc):
cliWidth, cliHeight = self.GetClientSize()
if not cliWidth or not cliHeight:
return
dc.Clear()
xPos = (cliWidth - self._width)/2
yPos = (cliHeight - self._height)/2
dc.DrawBitmap(self.bg, xPos, yPos)
app = wx.App()
frame = wx.Frame(None, size=(400,300))
panel = MainPanel(frame)
frame.Show()
app.MainLoop()
The method OnEraseBackground is intentionally empty.

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 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
When O run it it gives error because Custom_Button is passed NULL parent instead pass frame e.g. Custom_Button(self, ...)
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)
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)
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()

Categories

Resources