当前位置:网站首页>[pyqt5 series] modify the counter to realize control
[pyqt5 series] modify the counter to realize control
2022-06-23 07:29:00 【Teacher Detian】
Qt5— Adjustment of the counter
I haven't sent a document for a long time , I've been learning recently PyQt5 in , In particular, we will feed back our learning experience to everyone to exchange and grow together
The following is the source code , Notes attached .
import sys
from PyQt5.QtWidgets import *
'''
By setting the data on the counter , Thus, when the counter is 0 when , Increase the effective , The counter is 9 when , Downward reduction is effective , Otherwise, the counter returns a null value .
And pass MyASB Class internal methods stepBy, Return results p_int Output .
'''
class MyASB(QAbstractSpinBox):
# because MyASB Is inherited from the parent class of the counter , So the following self All empty fingers ‘ Counter ’
# here parent=None Is to define a parent class method that can be None
# here num=0 Is to define an initial value , If you do not give num Pass value , The system calls automatically 0
def __init__(self,parent=None,num=0,*args,**kwargs):
# Note here parent Can not be equal to None, Because this is to call the parent method, not to define the method
super().__init__(parent,*args,**kwargs)
# here lineEdit() Is a combination control of counters - Line edit , Used to display data
self.lineEdit().setText(str(num))
def stepEnabled(self): # Execute acquisition first p_int value , For the back stepBy() Methods use
# here current_num Used to receive text from the counter , Turn into int Pass on , Realization if Judge
current_num = int(self.text())
if int(self.text()) == 0:
return QAbstractSpinBox.StepUpEnabled # Clicking the button up is effective
elif current_num == 9:
return QAbstractSpinBox.StepDownEnabled # Clicking the button down is effective
elif current_num < 0 or current_num > 9: # Clicking up or down is invalid , Here the value is passed p_int
return QAbstractSpinBox.StepNone
else:
# Both up and down can be used effectively
return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
#p_int: With the up click , Increase in value 1, Click down on the value -1, Receive the click value of the mouse
def stepBy(self, p_int):
#int(self.text())--- It refers to converting the obtained count text into integer data and adding it to the original data , Achieve cumulative increase effect
#p_int Refers to the step value
print(p_int)
current_num = int(self.text()) + p_int
# Combined from the current counter lineEdit Calling method setText Realize the effect of modifying the display data
self.lineEdit().setText(str(current_num))
class Window(QWidget):
def __init__(self):
super().__init__()
# Set title and initial size
self.setWindowTitle(' Summation problem ')
self.resize(500, 500)
self.setup_ui()
# Define counters
def setup_ui(self):
# there self Must write , Otherwise, the counter cannot be displayed , there self yes MyASB Inherited parent object QAbstractSpinBox
asb = MyASB(self,9)# Parameters 9 Is passed to variable num Custom data values for
asb.resize(100,30) # Counter size
asb.move(50,50)# Counter position
# The following is used to execute the program
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
Practice using layout Layout code
In order to better use the layout method , The following is the effect of the above code modification and re layout 
The source code is as follows :
import sys
from PyQt5.QtWidgets import *
'''
By setting the data on the counter , Thus, when the counter is 0 when , Increase the effective , The counter is 9 when , Downward reduction is effective , Otherwise, the counter returns a null value .
And pass MyASB Class internal methods stepBy, Return results p_int Output .
'''
class MyASB(QAbstractSpinBox):
# because MyASB Is inherited from the parent class of the counter , So the following self All empty fingers ‘ Counter ’
# here parent=None Is to define a parent class method that can be None
# here num=0 Is to define an initial value , If you do not give num Pass value , The system calls automatically 0
def __init__(self,parent=None,num=0,*args,**kwargs):
# Note here parent Can not be equal to None, Because this is to call the parent method, not to define the method
super().__init__(parent,*args,**kwargs)
# here lineEdit() Is a combination control of counters - Line edit , Used to display data
self.lineEdit().setText(str(num))
def stepEnabled(self): # Execute acquisition first p_int value , For the back stepBy() Methods use
# here current_num Used to receive text from the counter , Turn into int Pass on , Realization if Judge
current_num = int(self.text())
if int(self.text()) == 0:
return QAbstractSpinBox.StepUpEnabled # Clicking the button up is effective
elif current_num == 9:
return QAbstractSpinBox.StepDownEnabled # Clicking the button down is effective
elif current_num < 0 or current_num > 9: # Clicking up or down is invalid , Here the value is passed p_int
return QAbstractSpinBox.StepNone
else:
# Both up and down can be used effectively
return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
#p_int: With the up click , Increase in value 1, Click down on the value -1, Receive the click value of the mouse
def stepBy(self, p_int):
#int(self.text())--- It refers to converting the obtained count text into integer data and adding it to the original data , Achieve cumulative increase effect
#p_int Refers to the step value
print(p_int)
current_num = int(self.text()) + p_int
# Combined from the current counter lineEdit Calling method setText Realize the effect of modifying the display data
self.lineEdit().setText(str(current_num))
class Window(QWidget):
def __init__(self):
super().__init__()
# Set title and initial size
self.setWindowTitle(' Counter adjustment ')
self.resize(100, 50)
layout = QVBoxLayout()
self.label = QLabel(' My counter ',self)
self.setup_ui()
self.my_layout()
# Define counters
def setup_ui(self):
# there self Must write , Otherwise, the counter cannot be displayed , there self yes MyASB Inherited parent object QAbstractSpinBox
asb = MyASB(self,9)# Parameters 9 Is passed to variable num Custom data values for
self.asb = asb
asb.resize(100,30) # Counter size
asb.move(50,50)# Counter position
def my_layout(self):
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.asb)
self.setLayout(layout)
# The following is used to execute the program
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
Experience :
Code usage , We must practice more , Be diligent in your hands , In order to have in-depth thinking about the problems arising from the use of code
During code use , Problems arise , Search and find the answer in time to avoid making mistakes again in the future , Code learning is a sequential process of continuous error correction , Sometimes it takes a long time to find a mistake , But the time spent is valuable , Maybe when you find the problem , There will be an unforgettable memory , Similar mistakes will never be made again , You know all the problems you don't understand at once , I wish you all a happy study !
边栏推荐
- Initialization layer implementation
- 【PyQt5系列】修改计数器实现控制
- Simpledateformat thread safety issues
- leetcode210. 课程表 II 207. 课程表 拓扑排序 dfs bfs
- Akamai-1.75版本-_abck参数生成-js逆向分析
- Elaborate on the operation of idea
- 作为思摩尔应对气候变化紧急事件的一项举措,FEELM加入碳披露项目
- Redis setting password
- Arthas thread command locates thread deadlock
- Mysql事务隔离级别
猜你喜欢
随机推荐
Both are hard disk partitions. What is the difference between C disk and D disk?
U-Net: Convolutional Networks for Biomedical Image Segmentation
20BN-Jester完整数据集下载
Heuristic search strategy
Console Application
How to quickly and gracefully download large files from Google cloud disk (II)
【2022毕业季】从毕业到转入职场
315. calculate the number of elements on the right that are smaller than the current element
异构交易场景交互流程及一致性保证
Mysql事务隔离级别
Yan's DP analysis
Product axure9 (English version), prototype design and production pull-down secondary menu
312. poke the balloon
链游飞船开发 农民世界链游开发 土地链游开发
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参1
Quartz调度框架的学习使用
3dmax插件开发环境配置及FileExport和Utilities模板测试
Tp6+redis+think-queue+supervisor implements the process resident message queue /job task
g++编译命令使用
'Latin-1' codec can't encode characters in position 103-115: body ('string of Chinese ') is not valid Latin-1









![[* * * array * * *]](/img/fe/2520d85faab7d1fbf5036973ff5965.png)