1. 程式人生 > >pyqt4 繪製點,橢圓,線,矩形

pyqt4 繪製點,橢圓,線,矩形

#-*- coding:utf-8 -*-
#######pyqt繪製點,線,矩形,圓
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from random import *
class MyWindow(QDialog,QWidget):
    def __init__(self,parent = None):
        super(MyWindow,self).__init__(parent)
        self.resize(800,800)
        self.mainlayout = QGridLayout(self
) def paintEvent(self, e):########畫圖事件,每次update都會進入,想畫啥根據註釋進行,雙擊重畫 qp = QPainter() qp.begin(self) #self.drawLines(qp)######畫線 #self.drawPoints(qp) ###畫點 #self.drawRect(qp) ##畫矩形 self.drawEllipse(qp) ##畫圓,橢圓 qp.end() def mouseDoubleClickEvent(self, *args, **kwargs): self
.update() def drawPoints(self, qp): qp.setPen(QPen(Qt.red,10)) ######可以試下畫刷 setBrush,10指定點的大小 for i in range(10): qp.drawPoint(randint(1, self.width()), randint(1, self.height()) ) def drawLines(self,qp):#######畫線 loc_list = [] for i in xrange(4): loc_list.append((randint(0
,self.width()),randint(0,self.height())))########獲取隨機點 color = choice([Qt.black,Qt.white,Qt.darkGray,Qt.red,Qt.green,Qt.blue,Qt.cyan,Qt.magenta,Qt.yellow,Qt.darkRed,Qt.darkGreen,Qt.darkBlue,Qt.darkCyan,Qt.darkMagenta,Qt.darkYellow]) qp.setPen(QPen(color, randint(0,10), randint(1,6)))####前一個random是線條粗線,後一個random是線條型別 for i in xrange(4): qp.drawLine(loc_list[i][0],loc_list[i][1],loc_list[(i+1)%4][0],loc_list[(i+1)%4][1]) def drawRect(self,qp): qp.setPen(QPen(Qt.red,2,Qt.SolidLine)) qp.drawRect(200,200,400,400) def drawEllipse(self,qp):########橢圓,圓 qp.setPen(QPen(Qt.blue,2,Qt.SolidLine)) qp.drawEllipse(100,200,300,300) app=QApplication(sys.argv) window=MyWindow() window.show() app.exec_()