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

This commit is contained in:
M11212051 2024-05-05 22:29:17 +08:00
parent 1aee843c22
commit ce5d9029f6
5 changed files with 282 additions and 0 deletions

View File

@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中 QLineEdit.EchoMode效果範例
'''
from PyQt5.QtWidgets import QApplication, QLineEdit , QWidget , QFormLayout
import sys
class lineEditDemo(QWidget):
def __init__(self, parent=None):
super(lineEditDemo, self).__init__(parent)
self.setWindowTitle("QLineEdit範例")
flo = QFormLayout()
pNormalLineEdit = QLineEdit( )
pNoEchoLineEdit = QLineEdit()
pPasswordLineEdit = QLineEdit( )
pPasswordEchoOnEditLineEdit = QLineEdit( )
flo.addRow("Normal", pNormalLineEdit)
flo.addRow("NoEcho", pNoEchoLineEdit)
flo.addRow("Password", pPasswordLineEdit)
flo.addRow("PasswordEchoOnEdit", pPasswordEchoOnEditLineEdit)
pNormalLineEdit.setPlaceholderText("Normal")
pNoEchoLineEdit.setPlaceholderText("NoEcho")
pPasswordLineEdit.setPlaceholderText("Password")
pPasswordEchoOnEditLineEdit.setPlaceholderText("PasswordEchoOnEdit")
# 設定顯示效果
pNormalLineEdit.setEchoMode(QLineEdit.Normal)
pNoEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
pPasswordLineEdit.setEchoMode(QLineEdit.Password)
pPasswordEchoOnEditLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)
self.setLayout(flo)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = lineEditDemo()
win.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中 QLineEdit的驗證器範例
'''
from PyQt5.QtWidgets import QApplication, QLineEdit , QWidget , QFormLayout
from PyQt5.QtGui import QIntValidator ,QDoubleValidator , QRegExpValidator
from PyQt5.QtCore import QRegExp
import sys
class lineEditDemo(QWidget):
def __init__(self, parent=None):
super(lineEditDemo, self).__init__(parent)
self.setWindowTitle("QLineEdit範例")
flo = QFormLayout()
pIntLineEdit = QLineEdit( )
pDoubleLineEdit = QLineEdit()
pValidatorLineEdit = QLineEdit( )
flo.addRow("整數", pIntLineEdit)
flo.addRow("浮點數", pDoubleLineEdit)
flo.addRow("字母和數字", pValidatorLineEdit)
pIntLineEdit.setPlaceholderText("整數");
pDoubleLineEdit.setPlaceholderText("浮點數");
pValidatorLineEdit.setPlaceholderText("字母和數字");
# 整數,範圍:[1, 99]
pIntValidator = QIntValidator(self)
pIntValidator.setRange(1, 99)
# 浮點數,範圍:[-360, 360],精度:小數點後兩位
pDoubleValidator = QDoubleValidator(self)
pDoubleValidator.setRange(-360, 360)
pDoubleValidator.setNotation(QDoubleValidator.StandardNotation)
pDoubleValidator.setDecimals(2)
# 字母和數字
reg = QRegExp("[a-zA-Z0-9]+$")
pValidator = QRegExpValidator(self)
pValidator.setRegExp(reg)
# 設定驗證器
pIntLineEdit.setValidator(pIntValidator)
pDoubleLineEdit.setValidator(pDoubleValidator)
pValidatorLineEdit.setValidator(pValidator)
self.setLayout(flo)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = lineEditDemo()
win.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中 QLineEdit的輸入遮罩範例
'''
from PyQt5.QtWidgets import QApplication, QLineEdit , QWidget , QFormLayout
import sys
class lineEditDemo(QWidget):
def __init__(self, parent=None):
super(lineEditDemo, self).__init__(parent)
self.setWindowTitle("QLineEdit的輸入遮罩範例")
flo = QFormLayout()
pIPLineEdit = QLineEdit()
pMACLineEdit = QLineEdit()
pDateLineEdit = QLineEdit()
pLicenseLineEdit = QLineEdit()
pIPLineEdit.setInputMask("000.000.000.000;_")
pMACLineEdit.setInputMask("HH:HH:HH:HH:HH:HH;_")
pDateLineEdit.setInputMask("0000-00-00")
pLicenseLineEdit.setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#")
flo.addRow("數字遮罩", pIPLineEdit)
flo.addRow("Mac遮罩", pMACLineEdit)
flo.addRow("日期遮罩", pDateLineEdit)
flo.addRow("許可證遮罩", pLicenseLineEdit)
#pIPLineEdit.setPlaceholderText("111")
#pMACLineEdit.setPlaceholderText("222")
#pLicenseLineEdit.setPlaceholderText("333")
#pLicenseLineEdit.setPlaceholderText("444")
self.setLayout(flo)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = lineEditDemo()
win.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中 QLineEdit範例
'''
from PyQt5.QtWidgets import QApplication, QLineEdit , QWidget , QFormLayout
from PyQt5.QtGui import QIntValidator , QDoubleValidator , QFont
from PyQt5.QtCore import Qt
import sys
class lineEditDemo(QWidget):
def __init__(self, parent=None):
super(lineEditDemo, self).__init__(parent)
e1 = QLineEdit()
e1.setValidator( QIntValidator() )
e1.setMaxLength(4)
e1.setAlignment( Qt.AlignRight )
e1.setFont( QFont("Arial",20))
e2 = QLineEdit()
e2.setValidator( QDoubleValidator(0.99,99.99,2))
flo = QFormLayout()
flo.addRow("integer validator", e1)
flo.addRow("Double validator",e2)
e3 = QLineEdit()
e3.setInputMask('+99_9999_999999')
flo.addRow("Input Mask",e3)
e4 = QLineEdit()
e4.textChanged.connect( self.textchanged )
flo.addRow("Text changed",e4)
e5 = QLineEdit()
e5.setEchoMode( QLineEdit.Password )
flo.addRow("Password",e5)
e6 = QLineEdit("Hello PyQt5")
e6.setReadOnly(True)
flo.addRow("Read Only",e6 )
e5.editingFinished.connect( self.enterPress )
self.setLayout(flo)
self.setWindowTitle("QLineEdit範例")
def textchanged(self, text):
print( "輸入的內容為:"+text )
def enterPress( self ):
print( "已輸入值" )
if __name__ == "__main__":
app = QApplication(sys.argv)
win = lineEditDemo()
win.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-
"""
簡介
列印圖片範例
"""
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QImage , QIcon, QPixmap
from PyQt5.QtWidgets import QApplication , QMainWindow, QLabel, QSizePolicy , QAction
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
import sys
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
self.setWindowTitle(self.tr("列印圖片"))
# 建立一個置放圖形的QLabel物件imageLabel並將該QLabel物件設定為中心視窗。
self.imageLabel=QLabel()
self.imageLabel.setSizePolicy(QSizePolicy.Ignored,QSizePolicy.Ignored)
self.setCentralWidget(self.imageLabel)
self.image=QImage()
# 建立功能表、工具列等元件
self.createActions()
self.createMenus()
self.createToolBars()
# 在imageLabel物件中置放圖形
if self.image.load("./images/screen.png"):
self.imageLabel.setPixmap(QPixmap.fromImage(self.image))
self.resize(self.image.width(),self.image.height())
def createActions(self):
self.PrintAction=QAction(QIcon("./images/printer.png"),self.tr("列印"),self)
self.PrintAction.setShortcut("Ctrl+P")
self.PrintAction.setStatusTip(self.tr("列印"))
self.PrintAction.triggered.connect(self.slotPrint)
def createMenus(self):
PrintMenu=self.menuBar().addMenu(self.tr("列印"))
PrintMenu.addAction(self.PrintAction)
def createToolBars(self):
fileToolBar=self.addToolBar("Print")
fileToolBar.addAction(self.PrintAction)
def slotPrint(self):
# 新建一個QPrinter物件
printer=QPrinter()
# 建立一個QPrintDialog物件參數為QPrinter物件
printDialog=QPrintDialog(printer,self)
'''
判斷列印對話方塊顯示後是否點擊列印如果是
則相關列印屬性可以透過建立QPrintDialog物件時以QPrinter物件取得
若點擊取消則不執行後續的列印操作
'''
if printDialog.exec_():
# 建立一個QPainter物件並指定繪圖設備為一個QPrinter物件。
painter=QPainter(printer)
# 取得QPainter物件的視窗矩形
rect=painter.viewport()
# 取得圖形的大小
size=self.image.size()
# 依照圖形的比例大小重新設定視窗矩形
size.scale(rect.size(),Qt.KeepAspectRatio)
painter.setViewport(rect.x(),rect.y(),size.width(),size.height())
# 設定QPainter視窗大小為圖形的大小
painter.setWindow(self.image.rect())
# 列印
painter.drawImage(0,0,self.image)
if __name__ == "__main__":
app=QApplication(sys.argv)
main=MainWindow()
main.show()
sys.exit(app.exec_())