当前位置:网站首页>[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
 Insert picture description here
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 !

原网站

版权声明
本文为[Teacher Detian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230636066833.html