zoukankan      html  css  js  c++  java
  • PyQt5 控件学习(一个一个学习之QAbstractSpinBox)

    QAbstractSpinBox 继承图:

    QAbstractSpinBox描述:

    步长调节器的样例:

    它既可以用鼠标来操作,又可用键盘来操作 !

    它有整形的 QSpinBox ,浮点型 QDoubleSpinBox的还有时间型 QDataTimeEdit  主要这几种!

    QAbstractSpinBox 继承:

    它继承与 QWidget  

    QAbstractSpinBox 功能作用:

    与其他抽象类不同的是,它这个类还是可以直接实例化使用的!(它内部没有什么抽象方法)

    from PyQt5.Qt import * #刚开始学习可以这样一下导入
    import sys
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("QAbstractSpinBox 的学习")
            self.resize(400,400)
            self.set_ui()
    
    
        def set_ui(self):
            abstractSpinBox = QAbstractSpinBox(self)
            abstractSpinBox.resize(100,30)
            abstractSpinBox.move(100,100)
    
    if __name__ == '__main__':
        app =QApplication(sys.argv)
    
        window = Window()
        window.show()
    
        sys.exit(app.exec_())
    可以直接实例化使用的

    QAbstractSpinBox 功能作用之使用:

    使用的时候还是尽量子类化它!

    from PyQt5.Qt import * #刚开始学习可以这样一下导入
    import sys
    
    class MyAbstractSpinBox(QAbstractSpinBox):
        def __init__(self,parent=None,num = '0',*args,**kwargs):  #此处是定义
            super().__init__(parent,*args,**kwargs)  #此处是调用  ,注意区别
            self.lineEdit().setText(num)
    
        def stepEnabled(self):
            current_num = int(self.text())
            if current_num == 0:
                return QAbstractSpinBox.StepUpEnabled
    
            elif current_num == 9:
                return QAbstractSpinBox.StepDownEnabled
    
            elif current_num <0 or current_num >9:
                return QAbstractSpinBox.StepNone
    
            else:
                return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
    
        def stepBy(self, p_int):  # 如果上面的方法返回的是有效的话,就会调用这个函数
    
            current_num = int(self.text())+p_int
            ############################关键是如何设置回去###############################
            self.lineEdit().setText(str(current_num))  # 它是个组合控件,左面是个单行输入框,
    
            ############################关键是如何设置回去###############################
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("QAbstractSpinBox 的学习")
            self.resize(400,400)
            self.set_ui()
        def set_ui(self):
            abstractSpinBox = MyAbstractSpinBox(self,'5')
            abstractSpinBox.resize(100,30)
            abstractSpinBox.move(100,100)
    
    if __name__ == '__main__':
        app =QApplication(sys.argv)
    
        window = Window()
        window.show()
    
        sys.exit(app.exec_())
    View Code

    QAbstractSpinBox 功能作用之长按调整步长加快频率:

    from PyQt5.Qt import * #刚开始学习可以这样一下导入
    import sys
    
    class MyAbstractSpinBox(QAbstractSpinBox):
        def __init__(self,parent=None,num = '0',*args,**kwargs):  #此处是定义
            super().__init__(parent,*args,**kwargs)  #此处是调用  ,注意区别
            self.lineEdit().setText(num)
    
        def stepEnabled(self):
            # current_num = int(self.text())
            # if current_num == 0:
            #     return QAbstractSpinBox.StepUpEnabled
            #
            # elif current_num == 9:
            #     return QAbstractSpinBox.StepDownEnabled
            #
            # elif current_num <0 or current_num >9:
            #     return QAbstractSpinBox.StepNone
            # else:
            return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
    
        def stepBy(self, p_int):  # 如果上面的方法返回的是有效的话,就会调用这个函数
    
            current_num = int(self.text())+p_int
            self.lineEdit().setText(str(current_num))  # 它是个组合控件,左面是个单行输入框,
            #设置加速
            self.setAccelerated(True)
            print(self.isAccelerated())
    
    
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("QAbstractSpinBox 的学习")
            self.resize(400,400)
            self.set_ui()
        def set_ui(self):
            abstractSpinBox = MyAbstractSpinBox(self,'5')
            abstractSpinBox.resize(100,30)
            abstractSpinBox.move(100,100)
    
    if __name__ == '__main__':
        app =QApplication(sys.argv)
    
        window = Window()
        window.show()
    
        sys.exit(app.exec_())
    View Code

    QAbstractSpinBox 功能作用之只读:

    from PyQt5.Qt import * #刚开始学习可以这样一下导入
    import sys
    
    class MyAbstractSpinBox(QAbstractSpinBox):
        def __init__(self,parent=None,num = '0',*args,**kwargs):  #此处是定义
            super().__init__(parent,*args,**kwargs)  #此处是调用  ,注意区别
            self.lineEdit().setText(num)
    
        def stepEnabled(self):
            # current_num = int(self.text())
            # if current_num == 0:
            #     return QAbstractSpinBox.StepUpEnabled
            #
            # elif current_num == 9:
            #     return QAbstractSpinBox.StepDownEnabled
            #
            # elif current_num <0 or current_num >9:
            #     return QAbstractSpinBox.StepNone
            # else:
            return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
    
        def stepBy(self, p_int):  # 如果上面的方法返回的是有效的话,就会调用这个函数
    
            current_num = int(self.text())+p_int
            self.lineEdit().setText(str(current_num))  # 它是个组合控件,左面是个单行输入框,
            #设置加速
            self.setAccelerated(True)
            print(self.isAccelerated())
    
    
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("QAbstractSpinBox 的学习")
            self.resize(400,400)
            self.set_ui()
        def set_ui(self):
            abstractSpinBox = MyAbstractSpinBox(self,'5')
            abstractSpinBox.resize(100,30)
            abstractSpinBox.move(100,100)
    
            print(abstractSpinBox.isReadOnly())   #查看只读  
            abstractSpinBox.setReadOnly(True)    #设置只读
    
    
    if __name__ == '__main__':
        app =QApplication(sys.argv)
    
        window = Window()
        window.show()
    
        sys.exit(app.exec_())
    View Code

    QAbstractSpinBox 功能作用之设置以及获取内容:

    上面已经有提到过了,注意点就是设置的时候要通过QLineEdit 来设置,

    它为什么没有直接提供设置方法呢?因为它这个控件主要的用途是获取值。

    其实这个控件的左边就是QLineEdit ,QLineEdit 里面的大部分功能,都可以用

    QAbstractSpinBox 功能作用之对齐方式:

    之前,我们通过拿到左面的单行文本编辑器也可以设置对齐方式,

    这里,也可以用它直接提供的方法设置对齐方式

    from PyQt5.Qt import * #刚开始学习可以这样一下导入
    import sys
    
    class MyAbstractSpinBox(QAbstractSpinBox):
        def __init__(self,parent=None,num = '0',*args,**kwargs):  #此处是定义
            super().__init__(parent,*args,**kwargs)  #此处是调用  ,注意区别
            self.lineEdit().setText(num)
    
        def stepEnabled(self):
            # current_num = int(self.text())
            # if current_num == 0:
            #     return QAbstractSpinBox.StepUpEnabled
            #
            # elif current_num == 9:
            #     return QAbstractSpinBox.StepDownEnabled
            #
            # elif current_num <0 or current_num >9:
            #     return QAbstractSpinBox.StepNone
            # else:
            return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
    
        def stepBy(self, p_int):  # 如果上面的方法返回的是有效的话,就会调用这个函数
    
            current_num = int(self.text())+p_int
            self.lineEdit().setText(str(current_num))  # 它是个组合控件,左面是个单行输入框,
            #设置加速
            self.setAccelerated(True)
            print(self.isAccelerated())
    
    
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("QAbstractSpinBox 的学习")
            self.resize(400,400)
            self.set_ui()
        def set_ui(self):
            abstractSpinBox = MyAbstractSpinBox(self,'5')
            abstractSpinBox.resize(100,30)
            abstractSpinBox.move(100,100)
    
            print(abstractSpinBox.isReadOnly())   #查看只读
            abstractSpinBox.setReadOnly(True)    #设置只读
    
        ###########################设置对齐###############################
            abstractSpinBox.setAlignment(Qt.AlignCenter)  
    
        
        
        ############################设置对齐###############################
    
    
    
    if __name__ == '__main__':
        app =QApplication(sys.argv)
    
        window = Window()
        window.show()
    
        sys.exit(app.exec_())
    View Code

    QAbstractSpinBox 功能作用之设置周边框架:

    from PyQt5.Qt import * #刚开始学习可以这样一下导入
    import sys
    
    class MyAbstractSpinBox(QAbstractSpinBox):
        def __init__(self,parent=None,num = '0',*args,**kwargs):  #此处是定义
            super().__init__(parent,*args,**kwargs)  #此处是调用  ,注意区别
            self.lineEdit().setText(num)
    
        def stepEnabled(self):
            # current_num = int(self.text())
            # if current_num == 0:
            #     return QAbstractSpinBox.StepUpEnabled
            #
            # elif current_num == 9:
            #     return QAbstractSpinBox.StepDownEnabled
            #
            # elif current_num <0 or current_num >9:
            #     return QAbstractSpinBox.StepNone
            # else:
            return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
    
        def stepBy(self, p_int):  # 如果上面的方法返回的是有效的话,就会调用这个函数
    
            current_num = int(self.text())+p_int
            self.lineEdit().setText(str(current_num))  # 它是个组合控件,左面是个单行输入框,
            #设置加速
            self.setAccelerated(True)
            print(self.isAccelerated())
    
    
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("QAbstractSpinBox 的学习")
            self.resize(400,400)
            self.set_ui()
        def set_ui(self):
            abstractSpinBox = MyAbstractSpinBox(self,'5')
            abstractSpinBox.resize(100,30)
            abstractSpinBox.move(100,100)
    
    
            print(abstractSpinBox.hasFrame())  #  默认就是True 的
            # abstractSpinBox.setFrame(False)
    
    
    
    
    if __name__ == '__main__':
        app =QApplication(sys.argv)
    
        window = Window()
        window.show()
    
        sys.exit(app.exec_())
    View Code

    QAbstractSpinBox 功能作用之清空文本框内容:

    from PyQt5.Qt import * #刚开始学习可以这样一下导入
    import sys
    
    class MyAbstractSpinBox(QAbstractSpinBox):
        def __init__(self,parent=None,num = '0',*args,**kwargs):  #此处是定义
            super().__init__(parent,*args,**kwargs)  #此处是调用  ,注意区别
            self.lineEdit().setText(num)
    
        def stepEnabled(self):
            # current_num = int(self.text())
            # if current_num == 0:
            #     return QAbstractSpinBox.StepUpEnabled
            #
            # elif current_num == 9:
            #     return QAbstractSpinBox.StepDownEnabled
            #
            # elif current_num <0 or current_num >9:
            #     return QAbstractSpinBox.StepNone
            # else:
            return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
    
        def stepBy(self, p_int):  # 如果上面的方法返回的是有效的话,就会调用这个函数
    
            current_num = int(self.text())+p_int
            self.lineEdit().setText(str(current_num))  # 它是个组合控件,左面是个单行输入框,
            #设置加速
            self.setAccelerated(True)
            print(self.isAccelerated())
    
    
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("QAbstractSpinBox 的学习")
            self.resize(400,400)
            self.set_ui()
        def set_ui(self):
            abstractSpinBox = MyAbstractSpinBox(self,'5')
            abstractSpinBox.resize(100,30)
            abstractSpinBox.move(100,100)
    
    
    ############################清空###############################
            abstractSpinBox.clear()
            #如果它提供的话,可以这样
            #1,直接设置空字符串
            #2,拿到左面的lineEdit 去清空
    
    ############################清空###############################
    
    
    
    if __name__ == '__main__':
        app =QApplication(sys.argv)
    
        window = Window()
        window.show()
    
        sys.exit(app.exec_())
    View Code

    QAbstractSpinBox 功能作用之控制组合控件右面的样式:

    from PyQt5.Qt import * #刚开始学习可以这样一下导入
    import sys
    
    class MyAbstractSpinBox(QAbstractSpinBox):
        def __init__(self,parent=None,num = '0',*args,**kwargs):  #此处是定义
            super().__init__(parent,*args,**kwargs)  #此处是调用  ,注意区别
            self.lineEdit().setText(num)
    
        def stepEnabled(self):
            # current_num = int(self.text())
            # if current_num == 0:
            #     return QAbstractSpinBox.StepUpEnabled
            #
            # elif current_num == 9:
            #     return QAbstractSpinBox.StepDownEnabled
            #
            # elif current_num <0 or current_num >9:
            #     return QAbstractSpinBox.StepNone
            # else:
            return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
    
        def stepBy(self, p_int):  # 如果上面的方法返回的是有效的话,就会调用这个函数
    
            current_num = int(self.text())+p_int
            self.lineEdit().setText(str(current_num))  # 它是个组合控件,左面是个单行输入框,
            #设置加速
            self.setAccelerated(True)
            print(self.isAccelerated())
    
    
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("QAbstractSpinBox 的学习")
            self.resize(400,400)
            self.set_ui()
        def set_ui(self):
            abstractSpinBox = MyAbstractSpinBox(self,'5')
            abstractSpinBox.resize(100,30)
            abstractSpinBox.move(100,100)
            ############################设置组合控件的  右面的按钮标识  ###############################
    
    
            abstractSpinBox.setButtonSymbols(QAbstractSpinBox.NoButtons)  #里面放的是枚举值
                        #此时就将右面的按钮给隐藏了,如果还想调节它,可以用键盘上的 上下键 
    
            ############################设置组合控件的  右面的按钮标识  ###############################
    
    
    
    if __name__ == '__main__':
        app =QApplication(sys.argv)
    
        window = Window()
        window.show()
    
        sys.exit(app.exec_())
    View Code

    QAbstractSpinBox 功能作用之内容验证:

    from PyQt5.Qt import * #刚开始学习可以这样一下导入
    import sys
    
    class MyAbstractSpinBox(QAbstractSpinBox):
        def __init__(self,parent=None,num = '0',*args,**kwargs):  #此处是定义
            super().__init__(parent,*args,**kwargs)  #此处是调用  ,注意区别
            self.lineEdit().setText(num)
    
        def stepEnabled(self):
            # current_num = int(self.text())
            # if current_num == 0:
            #     return QAbstractSpinBox.StepUpEnabled
            #
            # elif current_num == 9:
            #     return QAbstractSpinBox.StepDownEnabled
            #
            # elif current_num <0 or current_num >9:
            #     return QAbstractSpinBox.StepNone
            # else:
            return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
    
        def stepBy(self, p_int):  # 如果上面的方法返回的是有效的话,就会调用这个函数
    
            current_num = int(self.text())+p_int
            self.lineEdit().setText(str(current_num))  # 它是个组合控件,左面是个单行输入框,
            #设置加速
            self.setAccelerated(True)
            print(self.isAccelerated())
    
    
    
    ############################验证器   ###############################
        def  validate(self, p_str, p_int):
            # 18-180
            num = int(p_str)
            if num<18:
                return (QValidator.Intermediate,p_str,p_int)
            elif num <= 180:
                return (QValidator.Acceptable,p_str,p_int)
            else:
                return (QValidator.Invalid,p_str,p_int)
    
        def fixup(self, p_str):  # 当 验证器中返回的状态是 无效或者是 中间状态时会再给它一次机会!
            print(p_str)
            return "18"
    
    ############################验证器   ###############################
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("QAbstractSpinBox 的学习")
            self.resize(400,400)
            self.set_ui()
        def set_ui(self):
            abstractSpinBox = MyAbstractSpinBox(self,'5')
            abstractSpinBox.resize(100,30)
            abstractSpinBox.move(100,100)
            ############################设置组合控件的  右面的按钮标识  ###############################
    
    
            abstractSpinBox.setButtonSymbols(QAbstractSpinBox.NoButtons)  #里面放的是枚举值
                        #此时就将右面的按钮给隐藏了,如果还想调节它,可以用键盘上的 上下键
    
            ############################设置组合控件的  右面的按钮标识  ###############################
    
    
    
    
    if __name__ == '__main__':
        app =QApplication(sys.argv)
    
        window = Window()
        window.show()
    
        sys.exit(app.exec_())
    View Code

    QAbstractSpinBox 信号:

    from PyQt5.Qt import * #刚开始学习可以这样一下导入
    import sys
    
    class MyAbstractSpinBox(QAbstractSpinBox):
        def __init__(self,parent=None,num = '0',*args,**kwargs):  #此处是定义
            super().__init__(parent,*args,**kwargs)  #此处是调用  ,注意区别
            self.lineEdit().setText(num)
    
        def stepEnabled(self):
            # current_num = int(self.text())
            # if current_num == 0:
            #     return QAbstractSpinBox.StepUpEnabled
            #
            # elif current_num == 9:
            #     return QAbstractSpinBox.StepDownEnabled
            #
            # elif current_num <0 or current_num >9:
            #     return QAbstractSpinBox.StepNone
            # else:
            return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
    
        def stepBy(self, p_int):  # 如果上面的方法返回的是有效的话,就会调用这个函数
    
            current_num = int(self.text())+p_int
            self.lineEdit().setText(str(current_num))  # 它是个组合控件,左面是个单行输入框,
            #设置加速
            self.setAccelerated(True)
            print(self.isAccelerated())
    
    
    
    ############################验证器   ###############################
        def  validate(self, p_str, p_int):
            # 18-180
            num = int(p_str)
            if num<18:
                return (QValidator.Intermediate,p_str,p_int)
            elif num <= 180:
                return (QValidator.Acceptable,p_str,p_int)
            else:
                return (QValidator.Invalid,p_str,p_int)
    
        def fixup(self, p_str):  # 当 验证器中返回的状态是 无效或者是 中间状态时会再给它一次机会!
            print(p_str)
            return "18"
    
    ############################验证器   ###############################
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("QAbstractSpinBox 的学习")
            self.resize(400,400)
            self.set_ui()
        def set_ui(self):
            abstractSpinBox = MyAbstractSpinBox(self,'5')
            abstractSpinBox.resize(100,30)
            abstractSpinBox.move(100,100)
    
            abstractSpinBox.editingFinished.connect(lambda :print("结束编辑"))
    
    
    if __name__ == '__main__':
        app =QApplication(sys.argv)
    
        window = Window()
        window.show()
    
        sys.exit(app.exec_())
    View Code

    总结: 

    这就是步长调节器的基类 QAbstractSpinBox  的内容:

    下面就看它具体的子类:先看 QSpinBox :https://www.cnblogs.com/zach0812/p/11387108.html

     

  • 相关阅读:
    Coursera机器学习week11 单元测试
    关于 TypeReference 的解释
    getModifiers 方法解释。
    instanceof isInstance isAssignableFrom 比较
    elasticsearch 基础 语法总结
    kibana 启动 关闭 和进程查找
    MD5 SHA1 SHA256 SHA512 SHA1WithRSA 的区别
    spring boot 项目 热启动
    java zip 压缩文件
    Packet for query is too large (1660 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.
  • 原文地址:https://www.cnblogs.com/zach0812/p/11386757.html
Copyright © 2011-2022 走看看