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

在这部分教程中,咱们学习了另外四个控件。

相关文章
相关标签/搜索