diff --git a/pyqt5/CODE/元件使用範例/qt04_drag.py b/pyqt5/CODE/元件使用範例/qt04_drag.py new file mode 100644 index 0000000..8b27c0f --- /dev/null +++ b/pyqt5/CODE/元件使用範例/qt04_drag.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- + +''' + 【簡介】 + PyQt5中Drag and Drop範例 + + +''' + +import sys +from PyQt5.QtCore import * +from PyQt5.QtGui import * +from PyQt5.QtWidgets import * + +class Combo(QComboBox): + + def __init__(self, title, parent): + super(Combo, self).__init__( parent) + self.setAcceptDrops(True) + + def dragEnterEvent(self, e): + print( e) + if e.mimeData().hasText(): + e.accept() + else: + e.ignore() + + def dropEvent(self, e): + self.addItem(e.mimeData().text()) + +class Example(QWidget): + def __init__(self): + super(Example, self).__init__() + self.initUI() + + def initUI(self): + lo = QFormLayout() + lo.addRow(QLabel("請把左邊的文字拖曳到右邊的下拉式清單中")) + edit = QLineEdit() + edit.setDragEnabled(True) + com = Combo("Button", self) + lo.addRow(edit,com) + self.setLayout(lo) + self.setWindowTitle('簡單的拖曳範例') + +if __name__ == '__main__': + app = QApplication(sys.argv) + ex = Example() + ex.show() + sys.exit(app.exec_()) diff --git a/pyqt5/CODE/元件使用範例/qt04_drawBrush.py b/pyqt5/CODE/元件使用範例/qt04_drawBrush.py new file mode 100644 index 0000000..2fac27b --- /dev/null +++ b/pyqt5/CODE/元件使用範例/qt04_drawBrush.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- + +""" + 【簡介】 + 繪圖中QBrush的範例,繪圖九種不同樣式的矩形。 + + +""" + +import sys +from PyQt5.QtWidgets import * +from PyQt5.QtGui import * +from PyQt5.QtCore import Qt + +class Drawing(QWidget): + def __init__(self): + super().__init__() + self.initUI() + + def initUI(self): + self.setGeometry(300, 300, 365, 280) + self.setWindowTitle('筆刷範例') + self.show() + + def paintEvent(self, e): + qp = QPainter() + qp.begin(self) + self.drawLines(qp) + qp.end() + + def drawLines(self, qp): + brush = QBrush(Qt.SolidPattern) + qp.setBrush(brush) + qp.drawRect(10, 15, 90, 60) + + brush = QBrush(Qt.Dense1Pattern) + qp.setBrush(brush) + qp.drawRect(130, 15, 90, 60) + + brush = QBrush(Qt.Dense2Pattern) + qp.setBrush(brush) + qp.drawRect(250, 15, 90, 60) + + brush = QBrush(Qt.Dense3Pattern) + qp.setBrush(brush) + qp.drawRect(10, 105, 90, 60) + + brush = QBrush(Qt.DiagCrossPattern) + qp.setBrush(brush) + qp.drawRect(10, 105, 90, 60) + + brush = QBrush(Qt.Dense5Pattern) + qp.setBrush(brush) + qp.drawRect(130, 105, 90, 60) + + brush = QBrush(Qt.Dense6Pattern) + qp.setBrush(brush) + qp.drawRect(250, 105, 90, 60) + + brush = QBrush(Qt.HorPattern) + qp.setBrush(brush) + qp.drawRect(10, 195, 90, 60) + + brush = QBrush(Qt.VerPattern) + qp.setBrush(brush) + qp.drawRect(130, 195, 90, 60) + + brush = QBrush(Qt.BDiagPattern) + qp.setBrush(brush) + qp.drawRect(250, 195, 90, 60) + +if __name__ == '__main__': + app = QApplication(sys.argv) + demo = Drawing() + demo.show() + sys.exit(app.exec_()) diff --git a/pyqt5/CODE/元件使用範例/qt04_drawPen.py b/pyqt5/CODE/元件使用範例/qt04_drawPen.py new file mode 100644 index 0000000..2587ec1 --- /dev/null +++ b/pyqt5/CODE/元件使用範例/qt04_drawPen.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- + +""" + 【簡介】 + 繪圖中QPen的範例,繪製使用不同樣式的6條線 + + +""" + +import sys +from PyQt5.QtWidgets import * +from PyQt5.QtGui import * +from PyQt5.QtCore import Qt + +class Drawing(QWidget): + def __init__(self): + super().__init__() + self.initUI() + + def initUI(self): + self.setGeometry(300, 300, 280, 270) + self.setWindowTitle('鋼筆樣式範例') + + def paintEvent(self, e): + qp = QPainter() + qp.begin(self) + self.drawLines(qp) + qp.end() + + def drawLines(self, qp): + pen = QPen(Qt.black, 2, Qt.SolidLine) + + qp.setPen(pen) + qp.drawLine(20, 40, 250, 40) + + pen.setStyle(Qt.DashLine) + qp.setPen(pen) + qp.drawLine(20, 80, 250, 80) + + pen.setStyle(Qt.DashDotLine) + qp.setPen(pen) + qp.drawLine(20, 120, 250, 120) + + pen.setStyle(Qt.DotLine) + qp.setPen(pen) + qp.drawLine(20, 160, 250, 160) + + pen.setStyle(Qt.DashDotDotLine) + qp.setPen(pen) + qp.drawLine(20, 200, 250, 200) + + pen.setStyle(Qt.CustomDashLine) + pen.setDashPattern([1, 4, 5, 4]) + qp.setPen(pen) + qp.drawLine(20, 240, 250, 240) + +if __name__ == '__main__': + app = QApplication(sys.argv) + demo = Drawing() + demo.show() + sys.exit(app.exec_()) diff --git a/pyqt5/CODE/元件使用範例/qt04_drawPoint.py b/pyqt5/CODE/元件使用範例/qt04_drawPoint.py new file mode 100644 index 0000000..58414a9 --- /dev/null +++ b/pyqt5/CODE/元件使用範例/qt04_drawPoint.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- + +""" + 【簡介】 + 在視窗中畫點的範例 + + +""" + +import sys, math +from PyQt5.QtWidgets import * +from PyQt5.QtGui import * +from PyQt5.QtCore import Qt + +class Drawing(QWidget): + def __init__(self, parent=None): + super(Drawing, self).__init__(parent) + self.resize(300, 200) + self.setWindowTitle("在視窗中畫點") + + def paintEvent(self, event): + # 初始化繪圖工具 + qp = QPainter() + qp.begin(self) + # 自訂畫點方法 + self.drawPoints(qp) + qp.end() + + def drawPoints(self, qp): + qp.setPen( Qt.red) + size = self.size() + + for i in range(1000): + # 繪製正弦函數圖形,它的週期是[-100, 100] + x = 100 *(-1+2.0*i/1000)+ size.width()/2.0 + y = -50 * math.sin((x - size.width()/2.0)*math.pi/50) + size.height()/2.0 + qp.drawPoint(x, y) + +if __name__ == '__main__': + app = QApplication(sys.argv) + demo = Drawing() + demo.show() + sys.exit(app.exec_()) diff --git a/pyqt5/CODE/元件使用範例/qt04_drawText.py b/pyqt5/CODE/元件使用範例/qt04_drawText.py new file mode 100644 index 0000000..4321108 --- /dev/null +++ b/pyqt5/CODE/元件使用範例/qt04_drawText.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- + +""" + 【簡介】 + 在視窗中繪製文字的範例 + + +""" + +import sys +from PyQt5.QtWidgets import QApplication ,QWidget +from PyQt5.QtGui import QPainter ,QColor ,QFont +from PyQt5.QtCore import Qt + +class Drawing(QWidget): + def __init__(self,parent=None): + super(Drawing,self).__init__(parent) + self.setWindowTitle("在視窗中繪製文字") + self.resize(300, 200) + self.text = '歡迎學習 PyQt5' + + def paintEvent(self,event): + painter = QPainter(self) + painter.begin(self) + # 自訂繪製方法 + self.drawText(event, painter) + painter.end() + + def drawText(self, event, qp): + # 設定畫筆的顏色 + qp.setPen( QColor(168, 34, 3) ) + # 設定字體 + qp.setFont( QFont('SimSun', 20)) + # 繪製文字 + qp.drawText(event.rect(), Qt.AlignCenter, self.text) + +if __name__ == "__main__": + app = QApplication(sys.argv) + demo = Drawing() + demo.show() + sys.exit(app.exec_())