PyQt5學習筆記二----組件---窗口、控件

  • QIcon 圖標控件,爲每一個窗口設置一個特有的圖標。
# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5.QtGui import QIcon
import sys
#創建一個名爲Icon的窗口,繼承自QWidget
class Icon(QWidget):
    def __init__(self,parent=None):
        super(Icon,self).__init__(parent)
        self.initUI()

    #初始化窗口
    def initUI(self):
        self.setGeometry(300,300,250,150) # x,y width, height
        self.setWindowTitle("程序圖標")
        self.setWindowIcon(QIcon("./../images/resize.png"))#爲窗口設置圖標

if __name__=="__main__":
    app=QApplication(sys.argv)
    icon=Icon()
    icon.show()
    sys.exit(app.exec_())
  • QToolTip   該控件是爲用戶顯示有好的氣泡信息
# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import QApplication,QWidget,QToolTip
from PyQt5.QtGui import QFont
import sys

class WinForm(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    #鼠標放在窗口上就可以顯示提示
    def initUI(self):
        QToolTip.setFont(QFont("SansSerif",10))
        self.setToolTip("這是一個<b>氣泡提示</b>")
        self.setGeometry(200,300,400,400)
        self.setWindowTitle("氣泡提示")

if __name__=="__main__":
    app=QApplication(sys.argv)
    win=WinForm()
    win.show()
    sys.exit(app.exec_())
  • QLable  標籤類佔位符,該控件可以顯示文本、圖片、GIF動畫,還可被用作提示標記爲其他控件。繼承自QFrame

常用方法

setAlignment()

按固定值方式對齊文本:Qt.AlignLeft(水平靠左對齊)、Qt.AlignRight(水平靠右對齊)、Qt.AlignCenter(水平居中)、Qt.AlignTop(垂直靠上對齊)、Qt.AlignButton(垂直靠下)、Qt.AlignVCenter(垂直居中對齊)

Qt.AlignJustify(水平方向調整間距兩端對齊)、

setIndent()

設置文本縮進值

setPixmap()

設置QLable爲一個Pixmap圖片

text()

獲得QLable的文本內容

setText()

設置QLable的文本內容

selectedText()

返回選擇的字符

setBuddy()

設置QLable的助記符及buddy(夥伴),即使用QLable設置快捷鍵,會在快捷鍵後將焦點設置到其Buddy上,這裏用到了QLable的交互控件功能。此外buddy可以是任何一個Widget控件,使用setBuddy(QWidget* )設置,其QLable必須是文本,並且使用 & 符號標記了助記符

setWordWrap()

設置是否允許換行

常用信號

linkActivated

當單擊標籤中嵌入的超鏈接,希望在新窗口打開超鏈接時,setOpenExternalLinks特性必須爲true

linkHovered

當鼠標滑過標籤中嵌入的超鏈接時,需要用槽函數與這個信號綁定

案例:操作QLable
#在窗口中顯示QLable
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QApplication,QWidget,QLabel,QVBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap,QPalette
import sys

class WindowDemo(QWidget):

    def __init__(self):
        super().__init__()
        self.initUi()
        self.setLable()
        self.addWidgets()
        self.doSomething()

    #定義控件變量並初始化控件,並不用全部都定義在構造函數中
    def initUi(self):
        self.lable1 = QLabel(self)
        self.lable2 = QLabel(self)
        self.lable3 = QLabel(self)
        self.lable4 = QLabel(self)
        self.palette = QPalette()
        self.palette.setColor(QPalette.Window, Qt.blue)
        self.vBox = QVBoxLayout()

    # 初始化標籤
    def setLable(self):
        self.lable1.setText("這是一個文本標籤")
        self.lable1.setAutoFillBackground(True)
        self.lable1.setPalette(self.palette)
        self.lable1.setAlignment(Qt.AlignCenter)
        self.lable2.setText("<a href='#'>歡迎使用Python</a>")
        self.lable3.setAlignment(Qt.AlignCenter)
        self.lable3.setToolTip("這是一個圖片標籤")
        self.lable3.setPixmap(QPixmap("./../images/resize.png"))
        self.lable4.setText("<a href='http://www.cnblogs.com/wangshuo1'>歡迎訪問小屋</a>")
        self.lable4.setAlignment(Qt.AlignRight)
        self.lable4.setToolTip("這是一個超鏈接")

    # 2在窗口布局中添加控件
    def addWidgets(self):
        self.vBox.addWidget(self.lable1)
        self.vBox.addStretch()
        self.vBox.addWidget(self.lable2)
        self.vBox.addStretch()
        self.vBox.addWidget(self.lable3)
        self.vBox.addStretch()
        self.vBox.addWidget(self.lable4)
        self.setLayout(self.vBox)
        self.setWindowTitle("QLable 例子")

    def doSomething(self):
        #允許Lable1控件訪問超鏈接
        self.lable1.setOpenExternalLinks(True)

        #打開超鏈接,默認是不允許,需要使用setOpenExternalLinks(True)允許瀏覽器訪問超鏈接
        self.lable4.setOpenExternalLinks(False)
        #點擊文本框綁定槽事件
        self.lable4.linkActivated.connect(self.link_clicked)

        #劃過文本框綁定槽事件
        self.lable2.linkHovered.connect(self.link_hovered)
        self.lable1.setTextInteractionFlags(Qt.TextSelectableByMouse)

    #槽函數
    def link_hovered(self):
        print("當鼠標滑過lable2標籤時,觸發")

    def link_clicked(self):
        print("當鼠標滑過lable4標籤時,觸發")

if __name__=="__main__":
    app=QApplication(sys.argv)
    win=WindowDemo()
    win.resize(400,400)
    win.show()
    sys.exit(app.exec_())
QLable:案例快捷鍵的使用
#在QLable中使用快捷鍵
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
import sys

class QlableDemo(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("快捷鍵")
        self.initUI()

    def initUI(self):
        self.nameLB1=QLabel("&Name",self)
        self.nameED1=QLineEdit(self)
        self.nameLB1.setBuddy(self.nameED1)

        self.nameLB2=QLabel("&Password",self)
        self.nameED2=QLineEdit(self)
        self.nameLB2.setBuddy(self.nameED2)

        self.btOK=QPushButton("&OK")
        self.btCancel=QPushButton("&Cancel")
        self.mainLayout=QGridLayout(self)
        self.mainLayout.addWidget(self.nameLB1,0,0)
        self.mainLayout.addWidget(self.nameED1,0,1,1,2)

        self.mainLayout.addWidget(self.nameLB2,1,0)
        self.mainLayout.addWidget(self.nameED2,1,1,1,2)

        self.mainLayout.addWidget(self.btOK,2,1)
        self.mainLayout.addWidget(self.btCancel, 2, 2)

        #點擊文本框綁定槽事件
        self.nameLB1.linkActivated.connect(self.link_clicked)

        #劃過文本框綁定槽事件
        self.nameLB2.linkHovered.connect(self.link_hovered)

    def link_hovered(self):
        print("鼠標滑過lable-2,觸發事件")
    def link_clicked(self):
        print("鼠標點擊lable-4,觸發事件")

if __name__=="__main__":
    app=QApplication(sys.argv)
    demo=QlableDemo()
    demo.show()
    sys.exit(app.exec_())

  • QLineEdit:QLineEdit十一個單行文本框控件,可以輸入單行字符串,若需輸入多行可以使用QTextEdit,該類中有許多常用的方法。

常用的方法

setAlignment()

按固定值方式對齊文本:Qt.AlignLeft(水平靠左對齊)、Qt.AlignRight(水平靠右對齊)、Qt.AlignCenter(水平居中)、Qt.AlignTop(垂直靠上對齊)、Qt.AlignButton(垂直靠下)、Qt.AlignVCenter(垂直居中對齊)

Qt.AlignJustify(水平方向調整間距兩端對齊)、

clear()

清空文本框內容

setEchoMode()

設置文本框顯示格式,允許輸入的格式的值:

QLineEdit.Normal:正常輸入,默認模式

QLineEdit.NoEcho:不顯示任何輸入字符。常用於密碼類型的輸入,且長度需要保密

QLineEdit.Password:不顯示密碼字符使用平臺內置的字符掩碼,星號或者圓點

QLineEdit.PasswordEchoOnEdit:在編輯時顯示字符和類型

Text()

獲得QLable的文本內容

setText()

設置QLable的文本內容

setPlaceholdText()

設置文本框浮顯文字

setMaxLength()

設置文本框最大輸入長度

setReadOnly()

設置文本框只讀

setDragEnable()

設置文本框是否接受拖動

selectAll()

全選

setFocus()

得到焦點

setInputMask()

設置掩碼

setValidator()

設置文本框驗證器,將限制任意可能輸入的文本

QInputValidator:限制輸入整數

QDoubleValidator:限制輸入浮點數

QRegexpValidator:輸入是否符合正則表達式

常用信號與槽

selectionChanged

選擇改變,信號就會被髮射

textChanged

修改文本內容時,信號觸發

editingFinished

編輯文本結束時,信號被髮射

案例一  #在QLineEdit
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
import sys

class LineEditDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QLineEdit 例子")
        self.initUI()
        self.setLayout(self.flo)

    def initUI(self):
        self.flo=QFormLayout()
        self.pNormalLineEdit=QLineEdit()
        self.pNoEchoLineEdit=QLineEdit()
        self.pPasswordLineEdit=QLineEdit()
        self.pPasswordEchoOnEditLineEdit=QLineEdit()

        self.flo.addRow("Normal",self.pNormalLineEdit)
        self.flo.addRow("NoEcho",self.pNoEchoLineEdit)
        self.flo.addRow("Password",self.pPasswordLineEdit)
        self.flo.addRow("PasswordEchoOnEdit",self.pPasswordEchoOnEditLineEdit)

        self.pNormalLineEdit.setPlaceholderText("Normal")
        self.pNoEchoLineEdit.setPlaceholderText("NoEcho")
        self.pPasswordLineEdit.setPlaceholderText("Password")
        self.pPasswordEchoOnEditLineEdit.setPlaceholderText("PasswordEchoOnEdit")
        self.Show()
        
    def Show(self):
         self.pNormalLineEdit.setEchoMode(QLineEdit.Normal)
         self.pNoEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
         self.pPasswordLineEdit.setEchoMode(QLineEdit.Password)
         self.pPasswordEchoOnEditLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)

if __name__=="__main__":
    app=QApplication(sys.argv)
    demo=LineEditDemo()
    demo.show()
    sys.exit(app.exec_())

案例二:設置QLineEdit模式,設置編輯框的輸入格式(輸入掩碼)、驗證器(輸入規則,搭配正則表達式
#QLineEdit驗證器
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
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 驗證器")
        self.resize(400,300)
        self.initUI()
        self.setInputMask()
        self.addRow()
        self.setValue()
        # 設置驗證器
        self.setLayout(self.flo)

    def initUI(self):
        #網格佈局
        self.flo=QFormLayout()
        #設置正則規則編輯框
        self.pIntLineEdit=QLineEdit()
        self.pDoubleLineEdit=QLineEdit()
        self.pValidatorLineEdit=QLineEdit()
        #設置輸入掩碼編輯框
        self.pIPLineEdit=QLineEdit()
        self.pMACLineEdit=QLineEdit()
        self.pDateLineEdit=QLineEdit()
        self.pLicenseLineEdit=QLineEdit()

    def setInputMask(self):
        self.pIPLineEdit.setInputMask("000.000.000.000;_")
        self.pMACLineEdit.setInputMask("HH:HH:HH:HH:HH:HH;_")
        self.pDateLineEdit.setInputMask("0000-00-00")
        self.pLicenseLineEdit.setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#")

    def addRow(self):
        self.flo.addRow("整形",self.pIntLineEdit)
        self.flo.addRow("浮點型",self.pDoubleLineEdit)
        self.flo.addRow("字母和數字",self.pValidatorLineEdit)

        #設置文本框默認顯示文字背景
        self.pIntLineEdit.setPlaceholderText("整形")
        self.pDoubleLineEdit.setPlaceholderText("浮點型")
        self.pValidatorLineEdit.setPlaceholderText("字母和數字")

        self.flo.addRow("數字掩碼",self.pIPLineEdit)
        self.flo.addRow("MAC 掩碼",self.pMACLineEdit)
        self.flo.addRow("日期掩碼",self.pDateLineEdit)
        self.flo.addRow("許可證掩碼",self.pLicenseLineEdit)

    def setValue(self):
        #整型 範圍  [1,99]
        self.pIntValidator=QIntValidator(self)
        self.pIntValidator.setRange(1,99)

        #浮點型,範圍[-360,360]   精度,小數點後兩位
        self.pDoubleValidator=QDoubleValidator(self)
        self.pDoubleValidator.setRange(-360,360)
        self.pDoubleValidator.setNotation(QDoubleValidator.StandardNotation)
        self.pDoubleValidator.setDecimals(2)

        #字母和數字
        self.reg=QRegExp("[a-zA-Z0-9]+$")
        self.pValidator=QRegExpValidator(self)
        self.pValidator.setRegExp(self.reg)
        #設置驗證器
        self.pIntLineEdit.setValidator(self.pIntValidator)
        self.pDoubleLineEdit.setValidator(self.pDoubleValidator)
        self.pValidatorLineEdit.setValidator(self.pValidator)


if __name__=="__main__":
    app=QApplication(sys.argv)
    win=lineEditDemo()
    win.show()
    sys.exit(app.exec_())


  • QTextEdit:該控件是一個   多行內容文本編輯框。
#QTextEdit格式
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QApplication,QWidget,QTextEdit,QVBoxLayout,QPushButton
import sys

class TextEditDemo(QWidget):
    def __init__(self,parent=None):
        super(TextEditDemo,self).__init__(parent)
        self.setWindowTitle("QTextEdit 例子")
        self.resize(300, 270)
        self.initUI()
        self.addWidgets()
        self.SignalSlot()


    def initUI(self):
        self.textEdit=QTextEdit()
        self.btnPress1=QPushButton("顯示文本")
        self.btnPress2=QPushButton("顯示HTML")
        self.layout=QVBoxLayout()
    def addWidgets(self):
        self.layout.addWidget(self.textEdit)
        self.layout.addWidget(self.btnPress1)
        self.layout.addWidget(self.btnPress2)
        self.setLayout(self.layout)

    def SignalSlot(self):
        self.btnPress1.clicked.connect(self.btnPress1_clicked)
        self.btnPress2.clicked.connect(self.btnPress2_clicked)

    def btnPress1_clicked(self):
        self.textEdit.setPlainText("Hello Qt5!\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_())


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章