当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Search DFS and BFS
Leetcode game 297 (20220612)
System file interface open
Interpretation of QT keypressevent
Bomb disposal cat
QT realizes message sending and file transmission between client and server
Sort (internal sort) + external sort
Bm1z002fj-evk-001 startup evaluation
Dynamic and static libraries
Simple greedy strategy
随机推荐
Small project - household income and expenditure software (1)
Nonstandard compiler attribute extension
Violence enumeration~
C language learning log 12.14
C language learning log 12.5
Luogu p1088 Martians
C language learning log 11.7
C language learning log 10.6
C language learning log 12.25
Logical point
Draw a hammer
Article 49: understanding the behavior of new handler
Metaltc4.0 stable release
【线程/多线程】线程的执行顺序
Clause 28: understanding reference folding
Flex布局自适应失效的问题
Case - recursive factorial (recursive)
Pycharm错误解决:Process finished with exit code -1073741819 (0xC0000005)
Section 8 - Practical commissioning techniques
Spread your wings and soar