def resize(self, event=None):
"""Handle wx resize events.
Notes:
This method may be called outside the wx event buffer
in order to initialize the panel or reset the drawing
buffer.
"""
size = self.winWidth, self.winHeight = self.GetSize()
if size != self.lastSize: # hack to mitigate multiple consecutive resize events
self.winRadius = min((self.winWidth/2.0, self.winHeight/2.0))
#self.drawingBuffer = wx.Bitmap(self.winWidth, self.winHeight) # wxpython3
self.drawingBuffer = wx.EmptyBitmap(self.winWidth, self.winHeight) # wxpython3
self.lastSize = size
self.refresh()
if event is not None:
event.Skip()
python类EmptyBitmap()的实例源码
def __init__(self, parent=None, id=-1, title=None):
wx.Frame.__init__(self, parent, id, title)
self.MainPanel = wx.Panel(self, size=(640, 480))
self.MainPanel.SetBackgroundColour('WHITE')
self.panel = wx.Panel(self.MainPanel, size = (640,480))
self.panel.SetBackgroundColour('WHITE')
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(self.panel)
self.SetSizer(mainSizer)
self.Fit()
self.Bind(wx.EVT_CLOSE, self.CloseWindow)
self.World = None
self.cdc = wx.ClientDC(self.panel)
w, h = self.panel.GetSize()
self.bmp = wx.EmptyBitmap(w,h)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.timer.Start(20)
def updatelastpic(self, lframe):
capsdir = self.capsfolder_box.GetValue()
last_pic = str(capsdir + cap_files[lframe])
if os.path.exists(last_pic):
last_pic = wx.Image(last_pic, wx.BITMAP_TYPE_ANY)
last_pic = self.scale_pic(last_pic, 500)
self.last_pic.SetBitmap(wx.BitmapFromImage(last_pic))
lpicdate = self.date_from_fn(cap_files[lframe])
self.lpic_text.SetLabel('Frame ' + str(lframe) + ' - ' + str(lpicdate))
else:
self.last_pic.SetBitmap(wx.EmptyBitmap(10,10))
self.fpic_text.SetLabel('end')
def updatefirstpic(self, fframe):
capsdir = self.capsfolder_box.GetValue()
first_pic = str(capsdir + cap_files[fframe])
if os.path.exists(first_pic):
first_pic = wx.Image(first_pic, wx.BITMAP_TYPE_ANY)
first_pic = self.scale_pic(first_pic, 500)
fpicdate = self.date_from_fn(cap_files[fframe])
self.fpic_text.SetLabel('Frame ' + str(fframe) + ' - ' + str(fpicdate))
self.first_pic.SetBitmap(wx.BitmapFromImage(first_pic))
else:
self.first_pic.SetBitmap(wx.EmptyBitmap(10,10))
self.fpic_text.SetLabel('start')
def saveImage(canvas, filename):
s = wx.ScreenDC()
w, h = canvas.size.Get()
b = wx.EmptyBitmap(w, h)
m = wx.MemoryDCFromDC(s)
m.SelectObject(b)
m.Blit(0, 0, w, h, s, 70, 0)
m.SelectObject(wx.NullBitmap)
b.SaveFile(filename, wx.BITMAP_TYPE_PNG)
def initBuffer(self):
w,h = self.GetClientSize() #????
self.buffer = wx.EmptyBitmap(w,h) #????buffer,????BufferedDC???
def RefreshView(self):
width, height = self.MessagePanel.GetClientSize()
bitmap = wx.EmptyBitmap(width, height)
dc = wx.BufferedDC(wx.ClientDC(self.MessagePanel), bitmap)
dc.Clear()
dc.BeginDrawing()
if self.CurrentMessage is not None:
dc.SetFont(self.Font)
for button in self.LeftButtons + self.RightButtons:
button.Draw(dc)
message_idx = self.CurrentMessage
message = self.LogMessages[message_idx]
draw_date = True
offset = 5
while offset < height and message is not None:
message.Draw(dc, offset, width, draw_date)
offset += message.GetHeight(draw_date)
previous_message, message_idx = self.GetPreviousMessage(message_idx)
if previous_message is not None:
draw_date = message.Date != previous_message.Date
message = previous_message
dc.EndDrawing()
self.MessageScrollBar.RefreshThumbPosition()
def __init__(self, parent, ID, bitmapname,
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=0,
name="genstatbmp"):
bitmap = GetBitmap(bitmapname)
if bitmap is None:
bitmap = wx.EmptyBitmap(0, 0)
wx.StaticBitmap.__init__(self, parent, ID,
bitmap,
pos, size,
style,
name)
def GetLogicalDC(self, buffered=False):
if buffered:
bitmap = wx.EmptyBitmap(*self.Editor.GetClientSize())
dc = wx.MemoryDC(bitmap)
else:
dc = wx.ClientDC(self.Editor)
dc.SetFont(self.GetFont())
if wx.VERSION >= (2, 6, 0):
self.Editor.DoPrepareDC(dc)
else:
self.Editor.PrepareDC(dc)
dc.SetUserScale(self.ViewScale[0], self.ViewScale[1])
return dc
def GetBitmap(bmp_name1, bmp_name2=None, size=None):
bmp = BitmapLibrary.get((bmp_name1, bmp_name2, size))
if bmp is not None:
return bmp
if bmp_name2 is None:
bmp = SearchBitmap(bmp_name1)
else:
# Bitmap with two icon
bmp1 = SearchBitmap(bmp_name1)
bmp2 = SearchBitmap(bmp_name2)
if bmp1 is not None and bmp2 is not None:
# Calculate bitmap size
width = bmp1.GetWidth() + bmp2.GetWidth() - 1
height = max(bmp1.GetHeight(), bmp2.GetHeight())
# Create bitmap with both icons
bmp = wx.EmptyBitmap(width, height)
dc = wx.MemoryDC()
dc.SelectObject(bmp)
dc.Clear()
dc.DrawBitmap(bmp1, 0, 0)
dc.DrawBitmap(bmp2, bmp1.GetWidth() - 1, 0)
dc.Destroy()
elif bmp1 is not None:
bmp = bmp1
elif bmp2 is not None:
bmp = bmp2
if bmp is not None:
BitmapLibrary[(bmp_name1, bmp_name2, size)] = bmp
return bmp
def saveImage(canvas, filename):
s = wx.ScreenDC()
w, h = canvas.size.Get()
b = wx.EmptyBitmap(w, h)
m = wx.MemoryDCFromDC(s)
m.SelectObject(b)
m.Blit(0, 0, w, h, s, 70, 0)
m.SelectObject(wx.NullBitmap)
b.SaveFile(filename, wx.BITMAP_TYPE_PNG)
def OnSize(self, event):
# The buffer is initialized in here, so that the buffer is always
# the same size as the Window.
width, height = self.GetClientSizeTuple()
# Make new off-screen bitmap: this bitmap will always have the
# current drawing in it, so it can be used to save the image to
# a file, or whatever.
if width and height:
# Macs can generate events with 0-size values
self._buffer = wx.EmptyBitmap(width, height)
self.UpdateDrawing()
def __init__(self, parent=None, id=-1, title=None):
wx.Frame.__init__(self, parent, id, title)
self.panel = wx.Panel(self, size=(640, 480))
self.panel.SetBackgroundColour('WHITE')
self.Fit()
self.A = Agent(self.panel, 150, 100 )
self.greenB = [Ball(rnd.randint(40, 600),rnd.randint(40, 440),
wx.Colour(112,173,71), property = 1) for i in range(0, 15)]
self.redB = [Ball(rnd.randint(40, 600),rnd.randint(40, 440),
wx.Colour(237,125,49), property = 2) for i in range(0, 10)]
# OutrBox
self.Box = Walls(640, 480, 0, 480)
self.Box.addPoint(0,0)
self.Box.addPoint(640,0)
self.Box.addPoint(640,480)
# Wall in the world
self.WallA = Walls(96, 90, 256, 90)
self.WallA.addPoint(256, 390)
self.WallA.addPoint(96,390)
self.Bind(wx.EVT_CLOSE, self.CloseWindow)
self.cdc = wx.ClientDC(self.panel)
w, h = self.panel.GetSize()
self.bmp = wx.EmptyBitmap(w,h)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.timer.Start(20)
def create_background(self):
w,h = self.GetSize()
self.background_bitmap = wx.EmptyBitmap(w, h)
dc = wx.MemoryDC(self.background_bitmap)
gc = wx.GraphicsContext_Create(dc)
dc.SetBrush(wx.Brush("#FFFFFF"))
dc.Clear()
dc.DrawRectangle(0,0,w,h)
off = h // self.chnls // 2
gc.SetPen(wx.Pen('#000000', width=1, style=wx.SOLID))
gc.SetBrush(wx.Brush("#FFFFFF", style=wx.TRANSPARENT))
dc.SetTextForeground("#444444")
if sys.platform in "darwin":
font, ptsize = dc.GetFont(), dc.GetFont().GetPointSize()
font.SetPointSize(ptsize - 3)
dc.SetFont(font)
else:
font = dc.GetFont()
font.SetPointSize(8)
dc.SetFont(font)
tickstep = w // 10
if tickstep < 40:
timelabel = "%.1f"
elif tickstep < 80:
timelabel = "%.2f"
elif tickstep < 120:
timelabel = "%.3f"
else:
timelabel = "%.4f"
timestep = (self.end - self.begin) * 0.1
for i, samples in enumerate(self.img):
y = h // self.chnls * i
if len(samples):
gc.DrawLines(samples)
dc.SetPen(wx.Pen('#888888', width=1, style=wx.DOT))
dc.DrawLine(0, y+off, w, y+off)
# for j in range(10):
# dc.SetPen(wx.Pen('#888888', width=1, style=wx.DOT))
# dc.DrawLine(j*tickstep, 0, j*tickstep, h)
# dc.DrawText(timelabel % (self.begin+j*timestep), j*tickstep+2, h-y-12)
dc.SetPen(wx.Pen('#000000', width=1))
dc.DrawLine(0, h-y, w, h-y)
dc.SelectObject(wx.NullBitmap)
def blank_settings(self):
print("clearing settings")
# clear system pannel text
system_info_pnl.sys_hdd_total.SetLabel("")
system_info_pnl.sys_hdd_remain.SetLabel("")
system_info_pnl.sys_hdd_used.SetLabel("")
system_info_pnl.sys_pigrow_folder.SetLabel("")
system_info_pnl.sys_os_name.SetLabel("")
#system_info_pnl.sys_pigrow_version.SetLabel("")
system_info_pnl.sys_pigrow_update.SetLabel("")
system_info_pnl.sys_network_name.SetLabel("")
system_info_pnl.wifi_list.SetLabel("")
system_info_pnl.sys_power_status.SetLabel("")
system_info_pnl.sys_camera_info.SetLabel("")
system_info_pnl.sys_pi_revision.SetLabel("")
system_info_pnl.sys_pi_date.SetLabel("")
system_info_pnl.sys_pc_date.SetLabel("")
#system_info_pnl.sys_time_diff.SetLabel("")
# clear config ctrl text and tables
try:
MainApp.config_ctrl_pannel.dirlocs_dict.clear()
MainApp.config_ctrl_pannel.config_dict.clear()
MainApp.config_ctrl_pannel.gpio_dict.clear()
MainApp.config_ctrl_pannel.gpio_on_dict.clear()
except:
pass
MainApp.config_info_pannel.gpio_table.DeleteAllItems()
config_info_pnl.boxname_text.SetValue("")
config_info_pnl.location_text.SetLabel("")
config_info_pnl.config_text.SetLabel("")
config_info_pnl.lamp_text.SetLabel("")
config_info_pnl.dht_text.SetLabel("")
# clear cron tables
cron_list_pnl.startup_cron.DeleteAllItems()
cron_list_pnl.repeat_cron.DeleteAllItems()
cron_list_pnl.timed_cron.DeleteAllItems()
# clear local files text and images
localfiles_info_pnl.cron_info.SetLabel("")
localfiles_info_pnl.local_path_txt.SetLabel("")
localfiles_info_pnl.folder_text.SetLabel("") ## check this updates on reconnect
localfiles_info_pnl.photo_text.SetLabel("")
localfiles_info_pnl.first_photo_title.SetLabel("")
localfiles_info_pnl.last_photo_title.SetLabel("")
blank = wx.EmptyBitmap(220, 220)
try:
localfiles_info_pnl.photo_folder_first_pic.SetBitmap(blank)
localfiles_info_pnl.photo_folder_last_pic.SetBitmap(blank)
except:
pass
# clear local file info
localfiles_info_pnl.local_path = ""
localfiles_info_pnl.config_files.DeleteAllItems()
localfiles_info_pnl.logs_files.DeleteAllItems()
def RefreshView(self):
self.RefreshCanvasPosition()
width, height = self.GraphicsWindow.GetVirtualSize()
bitmap = wx.EmptyBitmap(width, height)
dc = wx.BufferedDC(wx.ClientDC(self.GraphicsWindow), bitmap)
dc.Clear()
dc.BeginDrawing()
if self.DraggingAxesPanel is not None:
destBBox = self.DraggingAxesBoundingBox
srcBBox = self.DraggingAxesPanel.GetAxesBoundingBox()
srcBmp = _convert_agg_to_wx_bitmap(self.DraggingAxesPanel.get_renderer(), None)
srcDC = wx.MemoryDC()
srcDC.SelectObject(srcBmp)
dc.Blit(destBBox.x, destBBox.y,
int(destBBox.width), int(destBBox.height),
srcDC, srcBBox.x, srcBBox.y)
dc.EndDrawing()
if not self.Fixed or self.Force:
self.Force = False
refresh_graphics = True
else:
refresh_graphics = False
if self.DraggingAxesPanel is not None and self.DraggingAxesPanel not in self.GraphicPanels:
self.DraggingAxesPanel.RefreshViewer(refresh_graphics)
for panel in self.GraphicPanels:
if isinstance(panel, DebugVariableGraphicViewer):
panel.RefreshViewer(refresh_graphics)
else:
panel.RefreshViewer()
if self.CursorTick is not None:
tick = self.CursorTick
elif len(self.Ticks) > 0:
tick = self.Ticks[-1]
else:
tick = None
if tick is not None:
self.TickLabel.SetLabel(label=_("Tick: %d") % tick)
tick_duration = int(tick * self.Ticktime)
not_null = False
duration = ""
for value, format in [(tick_duration / DAY, _("%dd")),
((tick_duration % DAY) / HOUR, _("%dh")),
((tick_duration % HOUR) / MINUTE, _("%dm")),
((tick_duration % MINUTE) / SECOND, _("%ds"))]:
if value > 0 or not_null:
duration += format % value
not_null = True
duration += _("%03gms") % (float(tick_duration % SECOND) / MILLISECOND)
self.TickTimeLabel.SetLabel("t: %s" % duration)
else:
self.TickLabel.SetLabel("")
self.TickTimeLabel.SetLabel("")
self.TickSizer.Layout()
def RefreshViewer(self):
"""
Method that refresh the content displayed by Viewer
"""
# Create buffered DC for drawing in panel
width, height = self.GetSize()
bitmap = wx.EmptyBitmap(width, height)
dc = wx.BufferedDC(wx.ClientDC(self), bitmap)
dc.Clear()
# Get Graphics Context for DC, for anti-aliased and transparent
# rendering
gc = wx.GCDC(dc)
gc.BeginDrawing()
# Get first item
item = self.ItemsDict.values()[0]
# Get item variable path masked according Debug Variable Panel mask
item_path = item.GetVariable(
self.ParentWindow.GetVariableNameMask())
# Draw item variable path at Viewer left side
w, h = gc.GetTextExtent(item_path)
gc.DrawText(item_path, 20, (height - h) / 2)
# Update 'Release' button state and text color according to item forced
# flag value
item_forced = item.IsForced()
self.Buttons[1].Enable(item_forced)
self.RefreshButtonsPosition()
if item_forced:
gc.SetTextForeground(wx.BLUE)
# Draw item current value at right side of Viewer
item_value = item.GetValue()
w, h = gc.GetTextExtent(item_value)
gc.DrawText(item_value, width - 40 - w, (height - h) / 2)
# Draw other Viewer common elements
self.DrawCommonElements(gc)
gc.EndDrawing()
def onTakeScreenShot(self, event):
"""
Takes a screenshot of the screen at give pos & size (rect).
Method based on a script by Andrea Gavana
"""
print('Taking screenshot...')
rect = self.GetRect()
# adjust widths for Linux (figured out by John Torres
# http://article.gmane.org/gmane.comp.python.wxpython/67327)
if sys.platform == 'linux2':
client_x, client_y = self.ClientToScreen((0, 0))
border_width = client_x - rect.x
title_bar_height = client_y - rect.y
rect.width += (border_width * 2)
rect.height += title_bar_height + border_width
# Create a DC for the whole screen area
dcScreen = wx.ScreenDC()
# Create a Bitmap that will hold the screenshot image later on
# Note that the Bitmap must have a size big enough to hold the screenshot
# -1 means using the current default colour depth
bmp = wx.EmptyBitmap(rect.width, rect.height)
#Create a memory DC that will be used for actually taking the screenshot
memDC = wx.MemoryDC()
# Tell the memory DC to use our Bitmap
# all drawing action on the memory DC will go to the Bitmap now
memDC.SelectObject(bmp)
# Blit (in this case copy) the actual screen on the memory DC
# and thus the Bitmap
memDC.Blit( 0, # Copy to this X coordinate
0, # Copy to this Y coordinate
rect.width, # Copy this width
rect.height, # Copy this height
dcScreen, # Where to copy from
rect.x, # What's the X offset in the original DC?
rect.y # What's the Y offset in the original DC?
)
# Select the Bitmap out of the memory DC by selecting a new
# uninitialized Bitmap
memDC.SelectObject(wx.NullBitmap)
img = bmp.ConvertToImage()
fileName = "myImage.png"
img.SaveFile(fileName, wx.BITMAP_TYPE_PNG)
print('...saving as png!')
def __init__(self, parent=None, id=-1, title=None):
wx.Frame.__init__(self, parent, id, title)
self.MainPanel = wx.Panel(self, size=(640, 640))
self.MainPanel.SetBackgroundColour('WHITE')
self.panel = wx.Panel(self.MainPanel, size = (640,480))
self.panel.SetBackgroundColour('WHITE')
self.plotter = plot.PlotCanvas(self.MainPanel, size =(640, 640-480))
self.plotter.SetEnableZoom(False)
self.plotter.SetEnableLegend(True)
self.plotter.SetFontSizeLegend(10.5)
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(self.panel)
mainSizer.Add(self.plotter)
self.SetSizer(mainSizer)
self.Fit()
self.A = Agent(self.panel, 240, 49 )
self.B = Agent(self.panel, 240, 49)
self.B.B_color = wx.Colour(112,173,71)
# OutrBox
self.Box = Walls(640, 479, 0, 479)
self.Box.addPoint(0,0)
self.Box.addPoint(640,0)
self.Box.addPoint(640,479)
# Oval Course
Rad = 190.0
Poly = 16
self.Course = Walls(240, 50, 640-(50+Rad),50)
for i in range(1, Poly):
self.Course.addPoint(Rad*math.cos(-np.pi/2.0 + np.pi*i/Poly)+640-(50+Rad),
Rad*math.sin(-np.pi/2.0 + np.pi*i/Poly)+50+Rad)
self.Course.addPoint(240, 50+Rad*2)
for i in range(1, Poly):
self.Course.addPoint(Rad*math.cos(np.pi/2.0 + np.pi*i/Poly)+(50+Rad),
Rad*math.sin(np.pi/2.0 + np.pi*i/Poly)+50+Rad)
self.Course.addPoint(240,50)
self.Bind(wx.EVT_CLOSE, self.CloseWindow)
self.cdc = wx.ClientDC(self.panel)
w, h = self.panel.GetSize()
self.bmp = wx.EmptyBitmap(w,h)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.timer.Start(20)
self.i = 0
self.tmp_sum = 0.0
self.data = []
def __init__(self, parent=None, id=-1, title=None):
wx.Frame.__init__(self, parent, id, title)
self.MainPanel = wx.Panel(self, size=(640, 640))
self.MainPanel.SetBackgroundColour('WHITE')
self.panel = wx.Panel(self.MainPanel, size = (640,480))
self.panel.SetBackgroundColour('WHITE')
self.plotter = plot.PlotCanvas(self.MainPanel, size =(640, 640-480))
self.plotter.SetEnableZoom(False)
self.plotter.SetEnableLegend(True)
self.plotter.SetFontSizeLegend(10.5)
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(self.panel)
mainSizer.Add(self.plotter)
self.SetSizer(mainSizer)
self.Fit()
self.A = Agent(self.panel, 150, 100 )
self.B = Agent(self.panel, 400, 300, model = self.A.model)
self.C = Agent(self.panel, 400, 150, model = self.A.model)
self.D = Agent(self.panel, 320, 240, model = self.A.model)
self.greenB = [Ball(rnd.randint(40, 600),rnd.randint(40, 440),
wx.Colour(112,173,71), property = 1) for i in range(0, 15)]
self.redB = [Ball(rnd.randint(40, 600),rnd.randint(40, 440),
wx.Colour(237,125,49), property = 2) for i in range(0, 10)]
# OutrBox
self.Box = Walls(640, 480, 0, 480)
self.Box.addPoint(0,0)
self.Box.addPoint(640,0)
self.Box.addPoint(640,480)
# Wall in the world
self.WallA = Walls(96, 90, 256, 90)
self.WallA.addPoint(256, 390)
self.WallA.addPoint(96,390)
self.Bind(wx.EVT_CLOSE, self.CloseWindow)
self.cdc = wx.ClientDC(self.panel)
w, h = self.panel.GetSize()
self.bmp = wx.EmptyBitmap(w,h)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.timer.Start(20)
self.i = 0
self.tmp_sum = 0.0
self.data = []