PyQt5教程(七)——控件(II)

原文:http://zetcode.com/gui/pyqt5/widgets2/python

下面咱們繼續介紹PyQt5控件。咱們將學習QPixmap, QLineEdit, QSplitter與QComboBox。web

QPixmap

QPixmap是一種用於處理圖像的控件。它爲圖片的顯示作過優化。在下面的示例中,咱們將使用QPixmap展現圖片。app

#!/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)

咱們將這個pixmap放到QLabel控件中。ui

QLineEdit

QLineEdit是用於輸入或編輯單行文本的控件。它還有撤銷重作、剪切複製和拖拽功能。this

#!/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_())

示例中展現了一個QLineEdit與一個QLabel。咱們在QLineEdit中輸入的文字會實時顯示在QLabel控件中。spa

qle = QLineEdit(self)

建立QLineEdit控件。3d

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

若是QLineEdit控件中的文本發生變化會調用onChanged()方法。code

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

onChanged()方法中咱們將QLabel控件的文本設置爲輸入的內容。經過調用adjustSize()方法將QLabel控件的尺寸調整爲文本的長度。

QLineEdit

QSplitter

經過QSplitter,用戶能夠拖動子控件邊界來調整子控件的尺寸。在下面的示例中,咱們展現了三個由兩個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_())

示例中咱們建立了三個QFrame與兩個QSplitter。注意在某些主題中這些QSplitter可能會不可見。

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

爲了觀察QFrame控件間的邊界,咱們使用風格化的QFrame。

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

咱們建立了一個QSplitter控件,併爲它添加了兩個QFrame。

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

咱們也能夠將QSplitter添加到另外一個QSplitter控件中。

QSplitter

QComboBox

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

示例中展現了一個QComboBox與一個QLabel,QComboBox控件中有5個選項(Linux系統的幾個發行版名稱)。QLabel控件會顯示QComboBox中選中的某個選項。

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

咱們建立了一個帶有5個選項的QComboBox控件。

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

當選中某個條目時會調用onActivated()方法。

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

在方法中咱們將QLabel控件的內容設置爲選中的條目,而後調整它的尺寸。

QComboBox

在這部分教程中,咱們學習了另外四個控件。

相關文章
相關標籤/搜索