当前位置:网站首页>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 .
边栏推荐
- Modification and analysis of libcoap source code by Hongmeng device discovery module
- Understanding inode
- lookup
- Building Nacos 2 based on docker compose (using MySQL for persistence)
- Deleted the jupyter notebook in the jupyter interface by mistake
- Draw a hammer
- C language learning log 10.19
- Nonstandard compiler attribute extension
- Std:: map insert details
- QT client development -- driver loading problem of connecting to MySQL database
猜你喜欢
Chapter 15 mechanism: Address Translation
Use of natural sorting comparable
Install harbor (online offline)
Chapter 13 abstraction: address space
Brick story
C language learning log 12.14
Sort (internal sort) + external sort
C language learning log 1.22
安装harbor(在线|离线)
Case - count the number of occurrences of each string in the string
随机推荐
Metaltc4.0 stable release
C language learning log 1.17
Clause 32: moving objects into closures using initialization capture objects
Stepping on a horse (one stroke)
Std:: map insert details
Modification and analysis of libcoap source code by Hongmeng device discovery module
Chapter 17 free space management
Understanding inode
Pychart error resolution: process finished with exit code -1073741819 (0xc0000005)
MySQL8.0.13安装教程(有图)
Reductive elimination
Bm1z002fj-evk-001 startup evaluation
Regular expressions in QT
priority inversion problem
Install harbor (online offline)
Create a harbor image library from the command line
Clause 48: understand template metaprogramming
详解OpenCV的函数cv::add(),并附各种情况的示例代码和运行结果
Clause 30: be familiar with the failure of perfect forwarding
Section 4 - arrays