当前位置:网站首页>Pyqt5 controls qpixmap, qlineedit qsplitter, qcombobox
Pyqt5 controls qpixmap, qlineedit qsplitter, qcombobox
2022-06-13 05:13:00 【strive_ one】
QPixmap
- QPixmap Is a control for processing images . Is the optimized display image on the screen . In our code example , We will use QPixmap The window displays an image .
# -*- coding: utf-8 -*-
""" PyQt5 tutorial In this example, we dispay an image on the window. author: py40.com last edited: 2017 year 3 month """
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("icon.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_())
- Show a picture on the window
pixmap = QPixmap("icon.png")
- Create a QPixmap object , It takes the passed in file name as a parameter .
lbl = QLabel(self)
lbl.setPixmap(pixmap)
- This will make us pixmap Put it in QLabel Control .
The text box QLineEdit
- QLineEdit Is a control for entering or editing single line text . It also has undo redo 、 Cut, copy and drag .
# -*- coding: utf-8 -*-
""" PyQt5 tutorial This example shows text which is entered in a QLineEdit in a QLabel widget. author: py40.com last edited: 2017 year 3 month """
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_())
- The example shows a QLineEdit With a QLabel. We are QLineEdit The text entered in will be displayed in real time QLabel Control .
qle = QLineEdit(self)
- establish QLineEdit
qle.textChanged[str].connect(self.onChanged)
- When the content of the text box changes , Would call onChanged Method
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
- stay onChanged() In the method, we will QLabel Control to the input . By calling adjustSize() Methods will QLabel Control is resized to the length of the text .
QSplitter
- adopt QSplitter, The user can drag the border of the child control to adjust the size of the child control . In the following example , We show three by two QSplitter The organization's QFrame Control .
# -*- coding: utf-8 -*-
""" PyQt5 tutorial This example shows how to use QSplitter widget. author: py40.com last edited: 2017 year 3 month """
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 the example, we created three QFrame With two QSplitter. Note these in some topics QSplitter May not be visible .
topleft = QFrame(self)
topleft.setFrameShape(QFrame.StyledPanel)
- We use a style framework in order to see QFrame Boundaries between widgets .
splitter1 = QSplitter(Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)
- We create a QSplitter Widget and add two frames .
splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)
- We can also put QSplitter Add to another QSplitter Control .
The drop-down list QComboBox
- QComboBox Is a control that allows the user to select from a drop-down list .
# -*- coding: utf-8 -*-
""" PyQt5 tutorial This example shows how to use a QComboBox widget. author: py40.com last edited: 2017 year 3 month """
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 With a QLabel,QComboBox Control has 5 An option (Linux Several release names of the system ).QLabel Control displays QComboBox An option selected in .
combo = QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Arch")
combo.addItem("Gentoo")
- Created a with five options QComboBox
combo.activated[str].connect(self.onActivated)
- When an entry is selected, it calls onActivated() Method .
def onActivated(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
- In the method we will QLabel The content of the control is set to the selected item , Then adjust its size .
边栏推荐
- Building Nacos 2 based on docker compose (using MySQL for persistence)
- 详解OpenCV的函数cv::add(),并附各种情况的示例代码和运行结果
- Case - the ArrayList collection stores student objects and traverses them in three ways
- COAP protocol libcoap API
- OpenCV中的saturate操作(饱和操作)究竟是怎么回事
- Case - simulated landlords (upgraded version)
- Advanced C language - Section 1 - data storage
- Section 6 - pointers
- C language learning log 12.14
- Small project - household income and expenditure software (2)
猜你喜欢

Metartc4.0 integrated ffmpeg compilation

Case -- the HashSet set stores the student object and traverses

C language learning log 1.24

KVM hot migration for KVM virtual management

Simple greedy strategy

Spice story

Article 29: assuming that the mobile operation does not exist, is expensive, and is not used

Shell built-in string substitution

Leetcode game 297 (20220612)

Chapter 18 pagination: Introduction
随机推荐
Luogu p1012 guess
Jeffery0207 blog navigation
lookup
Luogu p3654 fisrt step
Section 4 - arrays
Clause 47: please use traits classes to represent type information
Clause 27: alternatives to overloading with familiar universal reference types
Section 8 - Practical commissioning techniques
Regular expressions in QT
External sort
RuoYi-Cloud启动教程(手把手图文)
[multi thread programming] the future interface obtains thread execution result data
QT brushes and brushes
What is the saturate operation in opencv
Understanding of speech signal framing
Violence enumeration~
Std:: Map initialization
Opencv image storage and reading
priority inversion problem
Time display of the 12th Blue Bridge Cup