返回介绍

PyQt5 widgets II

发布于 2025-02-22 22:19:26 字数 8198 浏览 0 评论 0 收藏 0

Here we will continue introducing PyQt5 widgets. We will cover a QPixmap , a QLineEdit , a QSplitter , and a QComboBox .

QPixmap

A QPixmap is one of the widgets used to work with images. It is optimized for showing images on screen. In our code example, we will use the QPixmap to display an image on the window.

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

In our example, we display an image on the window.

pixmap = QPixmap("redrock.png")

We create a QPixmap object. It takes the name of the file as a parameter.

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

We put the pixmap into the QLabel widget.

QLineEdit

A QLineEdit is a widget that allows to enter and edit a single line of plain text. There are undo and redo, cut and paste, and drag & drop functions available for the widget.

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

This example shows a line edit widget and a label. The text that we key in the line edit is displayed immediately in the label widget.

qle = QLineEdit(self)

The QLineEdit widget is created.

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

If the text in the line edit widget changes, we call the onChanged() method.

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

Inside the onChanged() method, we set the typed text to the label widget. We call the adjustSize() method to adjust the size of the label to the length of the text.

QLineEdit
Figure: QLineEdit

QSplitter

A QSplitter lets the user control the size of child widgets by dragging the boundary between its children. In our example, we show three QFrame widgets organized with two splitters.

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

In our example, we have three frame widgets and two splitters. Note that under some themes, the splitters may not be visible very well.

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

We use a styled frame in order to see the boundaries between the QFrame widgets.

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

We create a QSplitter widget and add two frames into it.

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

We can also add a splitter to another splitter widget.

QSplitter widget
Figure: QSplitter widget

QComboBox

The QComboBox is a widget that allows a user to choose from a list of options.

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

The example shows a QComboBox and a QLabel . The combo box has a list of five options. These are the names of Linux distros. The label widget displays the selected option from the combo box.

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

We create a QComboBox widget with five options.

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

Upon an item selection, we call the onActivated() method.

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

Inside the method, we set the text of the chosen item to the label widget. We adjust the size of the label.

QComboBox
Figure: QComboBox

In this part of the PyQt5 tutorial, we covered other four PyQt5 widgets.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文