PyQt5教程——組件 Ⅱ(八)


這部分的教程將會繼續介紹PyQt5的組件。我們這節教程的內容將包括像素圖(QPixmap),單行文本框(QLineEdit)和下拉列表框(QComboBox)

像素圖(QPixmap)

像素圖(QPixmap)是各種用於處理圖像的組件中的一個。它是在屏幕上顯示圖片的最佳選擇。在我們代碼例子中,我們將使用像素圖來在窗口上顯示一個圖片。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

In this example, we dispay an image
on the window. 

author: Jan Bodnar
website: zetcode.com 
last edited: January 2015
"""

import sys
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, 
    QLabel, QApplication)
from PyQt5.QtGui import QPixmap


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

        hbox = QHBoxLayout(self)
        pixmap = QPixmap("redrock.png")

        lbl = QLabel(self)
        lbl.setPixmap(pixmap)

        hbox.addWidget(lbl)
        self.setLayout(hbox)
        
        self.move(300, 200)
        self.setWindowTitle('Red Rock')
        self.show()        
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

運行這個例子,我們會把一個圖片顯示在窗口上。

pixmap = QPixmap("redrock.png")

創建QPixmap對象。該對象構造方法傳入一個文件的名字作為參數。

lbl = QLabel(self)
lbl.setPixmap(pixmap)

我們把像素圖對象設置給標簽,從而通過標簽來顯示像素圖。

單行文本編輯框(QLineEdit)

單行文本編輯框組件允許輸入單行的純文本數據 。這個組件支持撤銷、重做、剪切、粘貼、拖拽、拖動方法。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This example shows text which 
is entered in a QLineEdit
in a QLabel widget.
 
author: Jan Bodnar
website: zetcode.com 
last edited: January 2015
"""

import sys
from PyQt5.QtWidgets import (QWidget, QLabel, 
    QLineEdit, QApplication)


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

        self.lbl = QLabel(self)
        qle = QLineEdit(self)
        
        qle.move(60, 100)
        self.lbl.move(60, 40)

        qle.textChanged[str].connect(self.onChanged)
        
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('QLineEdit')
        self.show()
        
        
    def onChanged(self, text):
        
        self.lbl.setText(text)
        self.lbl.adjustSize()        
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

這個例子顯示了一個單行編輯文本框和一個標簽。我們在單行文本編輯框中輸入文本時會同步把文本顯示在標簽中。

qle = QLineEdit(self)

創建單行文本編輯框(QLineEdit)組件。

qle.textChanged[str].connect(self.onChanged)

如果單行文本編輯框框內文本被改變,調用onChanged()方法。

def onChanged(self, text):
    
    self.lbl.setText(text)
    self.lbl.adjustSize() 

上面是onChanged()方法的實現,我們設置了標簽的顯示文本。我們調用了adjustSize()方法來調整標簽相對於顯示的文本的長度。

QLineEditFigure: QLineEdit

分割框(QSplitter)

分割框組件讓我們通過拖拽分割線來控制子組件的大小。在我們的例子中,我們顯示由兩個分割框組件約束的三個QFrame組件。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This example shows
how to use QSplitter widget.
 
author: Jan Bodnar
website: zetcode.com 
last edited: January 2015
"""

import sys
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame, 
    QSplitter, QStyleFactory, QApplication)
from PyQt5.QtCore import Qt


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

        hbox = QHBoxLayout(self)

        topleft = QFrame(self)
        topleft.setFrameShape(QFrame.StyledPanel)
 
        topright = QFrame(self)
        topright.setFrameShape(QFrame.StyledPanel)

        bottom = QFrame(self)
        bottom.setFrameShape(QFrame.StyledPanel)

        splitter1 = QSplitter(Qt.Horizontal)
        splitter1.addWidget(topleft)
        splitter1.addWidget(topright)

        splitter2 = QSplitter(Qt.Vertical)
        splitter2.addWidget(splitter1)
        splitter2.addWidget(bottom)

        hbox.addWidget(splitter2)
        self.setLayout(hbox)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QSplitter')
        self.show()
        
        
    def onChanged(self, text):
        
        self.lbl.setText(text)
        self.lbl.adjustSize()        
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_()) 

在我們的例子中,我們有三個框架組件和兩個分割框組件。注意在某些主題下,分割框組件可能不會被顯示。

topleft = QFrame(self)
topleft.setFrameShape(QFrame.StyledPanel)

我們使用了一個樣式框架,為了讓框架組件之間的分割線看的明顯。

splitter1 = QSplitter(Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)

我們創建了一個分割框組件並且在這個分割框中添加進入兩個框架組件。

splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)

我們把第一個分割框添加進另一個分割框組件中。

QSplitter widgetFigure: QSplitter widget

下拉列表框(QComboBox)

下拉列表框組件允許用戶從列表中選擇一個列表項。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

This example shows how to use 
a QComboBox widget.
 
author: Jan Bodnar
website: zetcode.com 
last edited: January 2015
"""

import sys
from PyQt5.QtWidgets import (QWidget, QLabel, 
    QComboBox, QApplication)


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

        self.lbl = QLabel("Ubuntu", self)

        combo = QComboBox(self)
        combo.addItem("Ubuntu")
        combo.addItem("Mandriva")
        combo.addItem("Fedora")
        combo.addItem("Arch")
        combo.addItem("Gentoo")

        combo.move(50, 50)
        self.lbl.move(50, 150)

        combo.activated[str].connect(self.onActivated)        
         
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QComboBox')
        self.show()
        
        
    def onActivated(self, text):
      
        self.lbl.setText(text)
        self.lbl.adjustSize()  
        
                
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

例子中顯示了一個下拉列表框和一個標簽。下拉列表框有五個列表項。這五個列表項都是Linux發行版的名字。標簽組件顯示在下拉列表框中選中的列表項的文本。

combo = QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Arch")
combo.addItem("Gentoo")

我們創建一個下拉列表框並填充了五個列表項。

combo.activated[str].connect(self.onActivated) 

一旦列表項被選中,會調用onActivated()方法。

def onActivated(self, text):
  
    self.lbl.setText(text)
    self.lbl.adjustSize() 

上面的方法,我們把下拉列表框中選中的列表項的文本顯示在標簽組件上。並且根據標簽文本調整了標簽大小。

QComboBoxFigure: QComboBox

 

 

PyQt的入門教程就到這里結束了,下面該自己上官網了解更多的WIDGET的使用和QT更強大的功能了。

建議有C/C++基礎的同學,不妨直接下手QT,很簡單的,QT只要大概能寫個聊天室或者其他的小軟件,基本上PyQt就沒問題了,畢竟PyQt的核心還是Qt庫。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM