当前位置:网站首页>Various controls ==pyqt5
Various controls ==pyqt5
2022-07-25 11:39:00 【Brother Dong's cultivation diary】
QCheckBox
- QCheckBox Check box control , It has two states : Open and close , It is a tag with text Label Control for
- Checkboxes are often used to indicate features that a program can enable or disable
import sys
from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication
from PyQt5.QtCore import Qt
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
cb = QCheckBox('Show title', self)# label
cb.move(20, 20)
# We have set the window title , So we must also check the check box . By default , No window title is set and no check box is checked .
cb.toggle()
# We're going to customize changeTitle() Method to connect to stateChanged The signal . This method will switch the title of the form .
cb.stateChanged.connect(self.changeTitle)#signal
# Interface Qwidget
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('QCheckBox')
self.show()
# The status of the check box is via state Parameters of the incoming changeTitle() Method . Set the title of the form when the check box is checked , When unchecked, the title will be set as an empty string .
def changeTitle(self, state):#slot
if state == Qt.Checked:
self.setWindowTitle('QCheckBox')
else:
self.setWindowTitle('')
Switch button Toggle button
- ToggleButton yes QPushButton It's a special pattern of . It is a button with two states : Pressed and not pressed . Switch back and forth between these two states by clicking . This feature can be useful in some scenarios .
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton,
QFrame, QApplication)
from PyQt5.QtGui import QColor
class Example1(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.col = QColor(0, 0, 0)# This is the value of the initial black color .
# We create a QPushButton And through its setCheckable() Method to get one ToggleButton.
redb = QPushButton('Red', self)
redb.setCheckable(True)
redb.move(10, 10)
# take clicked The signal is connected to a user-defined method . We go through clicked The signal operates on a Boolean value .
redb.clicked[bool].connect(self.setColor)
greenb = QPushButton('Green', self)
greenb.setCheckable(True)
greenb.move(10, 60)
greenb.clicked[bool].connect(self.setColor)
blueb = QPushButton('Blue', self)
blueb.setCheckable(True)
blueb.move(10, 110)
blueb.clicked[bool].connect(self.setColor)
self.square = QFrame(self)
self.square.setGeometry(150, 20, 100, 100)
self.square.setStyleSheet("QWidget { background-color: %s }" %
self.col.name())
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Toggle button')
self.show()
def setColor(self, pressed):
source = self.sender()
if pressed:
val = 255
else:
val = 0
if source.text() == "Red":
self.col.setRed(val)
elif source.text() == "Green":
self.col.setGreen(val)
else:
self.col.setBlue(val)
self.square.setStyleSheet("QFrame { background-color: %s }" %
self.col.name())
Slider bar QSlider
- QSlider Is a control with a simple slider . The slider can be dragged back and forth . We can select a specific value by dragging . Sometimes it's more natural to use a slider than to enter a number directly or use a spin box .
- We will display a slider and a tab , Labels are used to display pictures , And through the slide bar control picture display
- Note that the picture here can only be used ico Format ,png Format pictures will not be displayed .
import sys
from PyQt5.QtWidgets import (QWidget, QSlider,
QLabel, QApplication)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
class Example2(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
sld = QSlider(Qt.Horizontal, self)# Create a horizontal slider
sld.setFocusPolicy(Qt.NoFocus)
sld.setGeometry(30, 40, 100, 30)
sld.valueChanged[int].connect(self.changeValue)# We will valueChanged The signal is connected to a custom changeValue() Method .
self.label = QLabel(self)# Created a QLabel Control and set an initial volume image for it .
self.label.setPixmap(QPixmap('audio.ico'))
self.label.setGeometry(160, 40, 80, 30)
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('QSlider')
self.show()
def changeValue(self, value):
if value == 0:
self.label.setPixmap(QPixmap('audio.ico'))
elif value > 0 and value <= 30:
self.label.setPixmap(QPixmap('min.ico'))
elif value > 30 and value < 80:
self.label.setPixmap(QPixmap('med.ico'))
else:
self.label.setPixmap(QPixmap('max.ico'))
Progress bar QProgressBar
- A progress bar is a control that shows the progress of a task .QProgressBar Control provides a horizontal or vertical PyQt5 Progress bar of tool kit .
- The creator can set the minimum and maximum values of the progress bar. The default is 0-99
import sys
from PyQt5.QtWidgets import (QWidget, QProgressBar,
QPushButton, QApplication)
from PyQt5.QtCore import QBasicTimer
class Example3(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.pbar = QProgressBar(self)#QProgressBar Construction method of
self.pbar.setGeometry(30, 40, 200, 25)
self.btn = QPushButton('Start', self)
self.btn.move(40, 80)
self.btn.clicked.connect(self.doAction)
self.timer = QBasicTimer()# We use a timer timer To activate QProgressBar
self.step = 0
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('QProgressBar')
self.show()
# Every QObject And its subclasses have a timerEvent() Event handler . We will re implement the event handler to respond to timer events .
def timerEvent(self, e):
if self.step >= 100:
self.timer.stop()
self.btn.setText('Finished')
return
self.step = self.step + 1
self.pbar.setValue(self.step)
def doAction(self):
if self.timer.isActive():
self.timer.stop()
self.btn.setText('Start')
else:# We call start() Method to start a timer . This method has two parameters : Timeouts and events that the object will receive .
self.timer.start(100, self)
self.btn.setText('Stop')
calendar control QCalendarWidget
- QCalendarWidget Provides a calendar control based on month . It allows users to select dates in a simple and intuitive way .
- Created a calendar control and a label control . The selected date will be displayed in the label control .
import sys
from PyQt5.QtWidgets import (QWidget, QCalendarWidget,
QLabel, QApplication)
from PyQt5.QtCore import QDate
class Example4(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
cal = QCalendarWidget(self)#QCalendarWidget Be created
cal.setGridVisible(True)
cal.move(20, 20)
cal.clicked[QDate].connect(self.showDate)# If we choose a date from the part , Click on [QDate] Signal . We connect this signal to a user-defined showDate() Method .
self.lbl = QLabel(self)
date = cal.selectedDate()
self.lbl.setText(date.toString())
self.lbl.move(130, 260)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Calendar')
self.show()
def showDate(self, date):
self.lbl.setText(date.toString())
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
import sys
from PyQt5.QtWidgets import (QWidget, QHBoxLayout,
QLabel, QApplication)
from PyQt5.QtGui import QPixmap
class Example5(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
pixmap = QPixmap("../essential_fuction_2/camera.png")# Creating 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 .
hbox.addWidget(lbl)# Horizontal layout
self.setLayout(hbox)
self.move(300, 200)
self.setWindowTitle('Red Rock')
self.show()
The text box QLineEdit
- QLineEdit Is a control for entering or editing single line text . It also has undo redo 、 Cut, copy and drag .
import sys
from PyQt5.QtWidgets import (QWidget, QLabel,
QLineEdit, QApplication)
class Example6(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)# When the content of the text box changes , Would call onChanged Method
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('QLineEdit')
self.show()
# take QLabel Control to the input . By calling adjustSize() Methods will QLabel Control is resized to the length of the text .
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
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 .
- Created three QFrame With two QSplitter. Note these in some topics QSplitter May not be visible .
import sys
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame,
QSplitter, QStyleFactory, QApplication)
from PyQt5.QtCore import Qt
class Example7(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
# We use a style framework in order to see QFrame Boundaries between widgets .
topleft = QFrame(self)#
topleft.setFrameShape(QFrame.StyledPanel)
topright = QFrame(self)
topright.setFrameShape(QFrame.StyledPanel)
bottom = QFrame(self)
bottom.setFrameShape(QFrame.StyledPanel)
# We create a QSplitter Widget and add two frames .
splitter1 = QSplitter(Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)
# We can also put QSplitter Add to another QSplitter Control .
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()
The drop-down list QComboBox
- QComboBox Is a control that allows the user to select from a drop-down list .
import sys
from PyQt5.QtWidgets import (QWidget, QLabel,
QComboBox, QApplication)
class Example8(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.lbl = QLabel("Ubuntu", self)
combo = QComboBox(self)# Created a with five options QComboBox
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)# When an entry is selected, it calls onActivated() Method .
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QComboBox')
self.show()
def onActivated(self, text):# In the method we will QLabel The content of the control is set to the selected item , Then adjust its size .
self.lbl.setText(text)
self.lbl.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example8()
sys.exit(app.exec_())
Message_box
import sys
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Message box')
self.show()
def closeEvent(self, event):# When we close the window , Triggered QCloseEvent. We need to rewrite closeEvent() Event handler .
reply = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
边栏推荐
- 各种控件==PYQT5
- shell-第六章练习
- Nowcodertop12-16 - continuous updating
- Small and micro enterprise smart business card management applet
- SQL language (6)
- W5500在处于TCP_Server模式下,在交换机/路由器网络中无法ping通也无法通讯。
- cookie and session
- Web mobile terminal: touchmove realizes local scrolling
- Introduction to shortcut keys in debug chapter
- Nowcodertop7-11 - continuous updating
猜你喜欢

SQL language (III)

Let sports happen naturally, and fire creates a new lifestyle

只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体

工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖

SQL注入 Less23(过滤注释符)

Fillet big killer, use filter to build fillet and wave effect!

大话DevOps监控,团队如何选择监控工具?

Small program of vegetable distribution in community

【mysql学习08】

WIZnet W5500系列培训活动之“MQTT协议讲解和实践(接入OneNET)”
随机推荐
leetcode 剑指 Offer 28. 对称的二叉树
谣言检测文献阅读十一—Preventing rumor spread with deep learning
玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧
txt转csv文件,隔行出现空行
MLX90640 红外热成像仪测温模块开发笔记(五)
贪心问题01_活动安排问题
flinksql client 连接kafka select * from table没有数据报错,如何解决?
布局管理==PYQT5
Nowcodertop7-11 - continuous updating
cookie and session
Introduction to shortcut keys in debug chapter
圆角大杀器,使用滤镜构建圆角及波浪效果!
shell-第四天作业
矩阵的特征值和特征向量
SQL注入 Less18(头部注入+报错注入)
基于W5500实现的考勤系统
常见的几种PCB表面处理技术!
Loadbalancerlife lifecycle requested by feign client
MIIdock简述
大话DevOps监控,团队如何选择监控工具?