上傳檔案到「pyqt5/CODE/元件使用範例」

This commit is contained in:
M11212051 2024-05-05 22:28:41 +08:00
parent c2252b861f
commit 1aee843c22
5 changed files with 271 additions and 0 deletions

View File

@ -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_())

View File

@ -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_())

View File

@ -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_())

View File

@ -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_())

View File

@ -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_())