案例1、label應用
# -*- coding: UTF-8 -*-
from PyQt5.QtWidgets import QApplication,QLabel,QWidget,QVBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap,QPalette
import sys
class WindowDemo(QWidget):
def __init__(self):
super().__init__()
label1 = QLabel(self)
label2 = QLabel(self)
label3 = QLabel(self)
label4 = QLabel(self)
#初始化標籤控件
label1.setText("這是一個文本標籤")
label1.setAutoFillBackground(True)
palette = QPalette()
palette.setColor(QPalette.Window,Qt.blue)
label1.setPalette(palette)
label1.setAlignment(Qt.AlignCenter)
label2.setText("<a href='#'>歡迎使用Python GUI應用</a>")
label3.setAlignment(Qt.AlignCenter)
label3.setToolTip("這是一個圖片標籤")
label3.setPixmap(QPixmap("./images/2.jpg"))
label4.setText("<A href='http://www.baidu.com'>歡迎訪問百度</a>")
label4.setAlignment(Qt.AlignRight)
label4.setToolTip("這是一個超鏈接標籤")
#2在窗口布局中添加控件
vbox = QVBoxLayout()
vbox.addWidget(label1)
vbox.addStretch()
vbox.addWidget(label2)
vbox.addStretch()
vbox.addWidget(label3)
vbox.addStretch()
vbox.addWidget(label4)
#3允許label1訪問超鏈接
label1.setOpenExternalLinks(True)
label4.setOpenExternalLinks(True)
#點擊文本框綁定槽事件
label4.linkActivated.connect( self.link_clicked )
#劃過文本框綁定槽事件
label2.linkHovered.connect(self.link_hovered)
label1.setTextInteractionFlags(Qt.TextSelectableByMouse)
self.setLayout(vbox)
self.setWindowTitle("QLabel例子")
def link_hovered(self):
print("當鼠標劃過label2時,觸發事件")
def link_clicked(self):
print("當鼠標點擊label4時,觸發事件")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = WindowDemo()
win.show()
sys.exit(app.exec_())
案例2、label的夥伴設置
# -*- coding: UTF-8 -*-
from PyQt5.QtWidgets import *
import sys
class QlabelDemo(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("QLabel例子")
nameLb1 = QLabel("&Name",self)
nameEd1 = QLineEdit(self)
nameLb1.setBuddy(nameEd1)
nameLb2 = QLabel("&Password", self)
nameEd2 = QLineEdit(self)
nameLb2.setBuddy(nameEd2)
btnOk = QPushButton("&OK")
btnCancel = QPushButton("&Cancel")
mainLayout = QGridLayout(self)
mainLayout.addWidget(nameLb1,0,0)
mainLayout.addWidget(nameEd1,0,1,1,2)
mainLayout.addWidget(nameLb2,1,0)
mainLayout.addWidget(nameEd2,1,1,1,2)
mainLayout.addWidget(btnOk,2,1)
mainLayout.addWidget(btnCancel,2,2)
if __name__ == "__main__":
app = QApplication(sys.argv)
labelDemo = QlabelDemo()
labelDemo.show()
sys.exit(app.exec_())
案例3、lineEdit
from PyQt5.QtWidgets import *
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_())
案例4、格式化LineEdit
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class lineEditDemo(QWidget):
def __init__(self,parent=None):
super(lineEditDemo,self).__init__(parent)
self.setWindowTitle("QLineEdit例子")
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))
e3 = QLineEdit()
e3.setInputMask("+99_999_999999")
e4 = QLineEdit()
e4.textChanged.connect(self.textchanged)
e5 = QLineEdit()
e5.setEchoMode(QLineEdit.Password)
e5.editingFinished.connect(self.enterPress)
e6 = QLineEdit("Hello PyQt5")
e6.setReadOnly(True)
flo = QFormLayout()
flo.addRow("integer validator",e1)
flo.addRow("Double validator",e2)
flo.addRow("Input Mask", e3)
flo.addRow("Text changed", e4)
flo.addRow("Password", e5)
flo.addRow("Read Only", e6)
self.setLayout(flo)
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_())
案例5、多行文本框QTextEdit
from PyQt5.QtWidgets import *
import sys
class textEditDemo(QWidget):
def __init__(self,parent=None):
super(textEditDemo,self).__init__(parent)
self.setWindowTitle("QTextEdit 例子")
self.resize(300,270)
self.textEdit = QTextEdit()
self.btnPress1 = QPushButton("顯示文本")
self.btnPress2 = QPushButton("顯示HTML")
layout = QVBoxLayout()
layout.addWidget(self.textEdit)
layout.addWidget(self.btnPress1)
layout.addWidget(self.btnPress2)
self.setLayout(layout)
self.btnPress1.clicked.connect(self.btnPress1_clicked)
self.btnPress2.clicked.connect(self.btnPress2_clicked)
def btnPress1_clicked(self):
self.textEdit.setPlainText("Hello PyQt5!\n單擊按鈕")
def btnPress2_clicked(self):
self.textEdit.setHtml("<font color='red' size='6'><red>Hello PyQt5!\n單擊按鈕</font>")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = textEditDemo()
win.show()
sys.exit(app.exec_())