wxPython之wx.DC繪制形狀
本文實(shí)例為大家分享了wxPython繪制形狀的具體代碼,供大家參考,具體內(nèi)容如下
繪制形狀
除了繪制文本和位圖,DC也可以繪制任意的形狀和線。這允許我們完全自定義窗口部件和控件的外觀。
示例說(shuō)明
利用PaintDC創(chuàng)建一個(gè)簡(jiǎn)單笑臉控件。
#-*-coding: UTF-8 -*-
#------------------------------------------------------
#Purpose: nothing....
#Author: 阿Bin先生
#Created: 2017年5月21日
#------------------------------------------------------
import wx
class Smiley(wx.PyControl):
def __init__(self, parent, size=(100, 100)):
super(Smiley, self).__init__(parent,
size=size,
style=wx.NO_BORDER)
# Event Handlers
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, event):
"""Draw the image on to the panel"""
dc = wx.PaintDC(self) # Must create a PaintDC
# Get the working rectangle we can draw in
rect = self.GetClientRect()
# Setup the DC
dc.SetPen(wx.BLACK_PEN) # for drawing lines / borders
yellowbrush = wx.Brush(wx.Colour(255, 255, 0))
dc.SetBrush(yellowbrush) # Yellow fill
cx = (rect.width / 2) + rect.x
cy = (rect.width / 2) + rect.y
radius = min(rect.width, rect.height) / 2
dc.DrawCircle(cx, cy, radius)
eyesz = (rect.width / 8, rect.height / 8)
eyepos = (cx / 2, cy / 2)
dc.SetBrush(wx.BLUE_BRUSH)
dc.DrawRectangle(eyepos[0], eyepos[1],
eyesz[0], eyesz[1])
eyepos = (eyepos[0] + (cx - eyesz[0]), eyepos[1])
dc.DrawRectangle(eyepos[0], eyepos[1],
eyesz[0], eyesz[1])
dc.SetBrush(yellowbrush)
startpos = (cx / 2, (cy / 2) + cy)
endpos = (cx + startpos[0], startpos[1])
dc.DrawArc(startpos[0], startpos[1],
endpos[0], endpos[1], cx, cy)
dc.SetPen(wx.TRANSPARENT_PEN)
dc.DrawRectangle(startpos[0], cy,
endpos[0] - startpos[0],
startpos[1] - cy)
class MyFrame(wx.Frame):
def __init__(self, parent, *args, **kwargs):
super(MyFrame, self).__init__(parent, *args, **kwargs)
# Attributes
self.Panel = wx.Panel(self)
Smiley(self.Panel)
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, title="DrawShapes",size = [500, 500])
self.SetTopWindow(self.frame)
self.frame.Show()
return True
if __name__ == "__main__":
app = MyApp(False)
app.MainLoop()
運(yùn)行結(jié)果:

示例分析
DC的SetPen用來(lái)繪制線條和形狀的邊框。DC的SetBrush用來(lái)填充顏色。首先使用DCdeDrawCircle繪制一個(gè)黑色邊框的黃色圓,表示頭。然后使用DrawRectangle方法繪制藍(lán)色矩形,表示眼睛。最后使用DC的DrawArch方法繪制扇形,因?yàn)橹幌胗脠A弧來(lái)表示微笑,所以用矩形覆蓋圓弧兩端的兩條半徑線。
常用的基本繪制函數(shù)


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django中使用celery完成異步任務(wù)的示例代碼
本篇文章主要介紹了Django中使用celery完成異步任務(wù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Python基礎(chǔ)之?dāng)?shù)據(jù)類型知識(shí)匯總
今天帶大家復(fù)習(xí)一下Python基礎(chǔ)知識(shí),文中對(duì)數(shù)據(jù)類型相關(guān)知識(shí)做了詳細(xì)的匯總,對(duì)剛?cè)腴Tpython的小伙伴很有幫助喲,需要的朋友可以參考下2021-05-05
Python 給某個(gè)文件名添加時(shí)間戳的方法
今天小編就為大家分享一篇Python 給某個(gè)文件名添加時(shí)間戳的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Pytorch可視化(顯示圖片)及格式轉(zhuǎn)換問(wèn)題
這篇文章主要介紹了Pytorch可視化(顯示圖片)及格式轉(zhuǎn)換問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
python實(shí)現(xiàn)簡(jiǎn)單的俄羅斯方塊
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單的俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Python中實(shí)現(xiàn)高效的列表過(guò)濾多種方法示例
這篇文章主要給大家介紹了關(guān)于Python中實(shí)現(xiàn)高效的列表過(guò)濾的多種方法,包括基礎(chǔ)的for循環(huán)、列表推導(dǎo)式、filter函數(shù)、itertools模塊,以及高級(jí)的pandas和numpy庫(kù),我們還討論了生成器的使用,以及在實(shí)際場(chǎng)景中的應(yīng)用,需要的朋友可以參考下2024-12-12

