zoukankan      html  css  js  c++  java
  • pyqt5学习之QLinneEdit

    QLinneEdit即单行文本编辑器,允许用户输入和编辑单行纯文本

    案例说明

    1. 文本设置和获取
    2. 文本输出模式
    3. 占位提示字符串
    4. 清空按钮显示
    5. 添加操作行为
    6. 自动补全
    7. 输入限制
    8. 输入设置验证器方式一
    9. 输入设置验证器方式二
    10. 掩码字符
    11. 光标控制
    12. 文本边框设置
    13. 对齐方式
    14. 信号
    from PyQt5.Qt import *
    import sys
    
    
    # *************文本设置和获取***************开始
    #应用场景:通过代码来控制输入文本内容
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # le_a.move(100, 200)
    # le_a.setText('12')  # 设置文本
    # print(le_a.text())  # 获取单行文本框文本
    # le_a.insert('34')  # 插入文本
    # print(le_a.displayText())  # 获取用户看到的文本
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************文本设置和获取***************结束
    
    
    
    # *************文本输出模式***************开始
    # # 设置输出模式,来适应不同的应用场景
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # le_a.move(100, 200)
    # le_a.setText('12')
    # le_a.setEchoMode(QLineEdit.NoEcho)  # 设置文本展示形式;文本不输出
    # print(le_a.echoMode())  # 获取输出模式
    # print(le_a.text())
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 300)
    # le_b.setText('56')
    # le_b.setEchoMode(QLineEdit.Password)  # 密文输出
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************文本输出模式***************结束
    
    
    
    # *************占位提示字符串***************开始
    # 在用户输入文本内容之前,给用户的提示语句
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # le_a.setPlaceholderText('提示')  # 设置提示字符串
    # print(le_a.placeholderText())
    # le_a.move(100, 200)
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 300)
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************占位提示字符串***************结束
    
    # *************清空按钮显示***************开始
    # 用作快速清空文本框内容
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # le_a.setText('12')
    # le_a.setClearButtonEnabled(True)  # 设置清空按钮
    # print(le_a.isClearButtonEnabled())  # 查看状态
    # le_a.move(100, 200)
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 300)
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************清空按钮显示***************结束
    
    # *************添加操作行为***************开始
    # # 应用场景:为文本框添加附加的行为操作
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # le_a.move(100, 200)
    #
    # # 1.设置行为
    # action = QAction(QIcon('dp.png'),'',le_a)
    # # 2.添加行为
    # le_a.addAction(action, QLineEdit.LeadingPosition)  # 第二个参数是设置行为在文本框的位置
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 300)
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************添加操作行为***************结束
    
    # *************自动补全***************开始
    # # 应用场景:根据用户输入的字符串,快速联想补全
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # completer = QCompleter(['yy','xx','oo'], le_a)  # 设置提示字符
    # le_a.setCompleter(completer)  #  添加提示对象
    # le_a.move(100, 200)
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 300)
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************自动补全***************结束
    
    # *************输入限制***************开始
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # le_a.move(100, 200)
    # le_a.setMaxLength(3)  # 设置输入限制长度
    # print(le_a.maxLength())  # 获取输入限制长度
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 300)
    # le_b.setReadOnly(True)  # 设置只读
    # print(le_b.isReadOnly())  # 查看状态
    #
    # window.show()
    #
    # sys.exit(app.exec_())
    # *************输入限制***************结束
    
    
    # *************输入设置验证器方式一***************开始
    # 验证器用于验证用户输入数据的合法性
    # class AgeVadidator(QValidator):
    #     def validate(self, input_str, pos_int):  # 第一个参数是输入的文本;第二个参数是光标的位置
    #         print(input_str, pos_int)
    #
    #         # 判定
    #         # 结果字符串, 应该全部都是由一些数字组成
    #         # return
    #         try:
    #             if 18 <= int(input_str) <= 180:
    #                 return (QValidator.Acceptable, input_str, pos_int)  # 验证通过
    #             elif 1 <= int(input_str) <= 17:
    #                 return (QValidator.Intermediate, input_str, pos_int)  # 暂不做判定
    #             else:
    #                 return (QValidator.Invalid, input_str, pos_int)  # 验证不通过
    #         except:
    #             if len(input_str) == 0:
    #                 return (QValidator.Intermediate, input_str, pos_int)
    #             return (QValidator.Invalid, input_str, pos_int)
    #
    #     def fixup(self, p_str):  # 用于修复错误输入
    #         print("xxx", p_str)
    #         try:
    #             if int(p_str) < 18:
    #                 return "18"
    #             return "180"
    #         except:
    #             return "18"
    #
    # class Window(QWidget):
    #     def __init__(self):
    #         super().__init__()
    #         self.setWindowTitle("QLineEidt验证器的学习")
    #         self.resize(500, 500)
    #         self.setup_ui()
    #
    #     def setup_ui(self):
    #         le = QLineEdit(self)
    #         le.move(100, 100)
    #         # 1.设置一个验证器
    #         vadidator = AgeVadidator()  # AgeVadidator是一个抽象类需要实例化一下
    #         # 2.控件添加验证器
    #         le.setValidator(vadidator)  # 设置验证器
    #
    #
    #         le2 = QLineEdit(self)
    #         le2.move(200, 200)
    #
    #
    # app = QApplication(sys.argv)
    #
    # window = Window()
    #
    # window.show()
    #
    # sys.exit(app.exec_())
    # *************输入设置验证器方式一***************结束
    
    
    # *************输入设置验证器方式二***************开始
    # class MyAgeVadidator(QIntValidator):  #验证整形数据
    #     def fixup(self, p_str):
    #         print("xxx", p_str)
    #         if len(p_str) == 0 or int(p_str) < 18:
    #         # if int(p_str) < 18 or len(p_str) == 0:
    #             return "18"
    #
    # class Window(QWidget):
    #     def __init__(self):
    #         super().__init__()
    #         self.setWindowTitle("QLineEidt验证器的学习")
    #         self.resize(500, 500)
    #         self.setup_ui()
    #
    #     def setup_ui(self):
    #         le = QLineEdit(self)
    #         le.move(100, 100)
    #         # 1.设置一个验证器
    #         vadidator = MyAgeVadidator(18, 180)  # AgeVadidator是一个抽象类需要实例化一下
    #         # 2.控件添加验证器
    #         le.setValidator(vadidator)  # 设置验证器
    #
    #
    #         le2 = QLineEdit(self)
    #         le2.move(200, 200)
    #
    #
    # app = QApplication(sys.argv)
    #
    # window = Window()
    #
    # window.show()
    #
    # sys.exit(app.exec_())
    # *************输入设置验证器方式二***************结束
    
    
    # *************掩码字符***************开始
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # le_a.move(100, 200)
    # le_a.setInputMask('999.999.999;x')  # 设置掩码,分号之前是掩码说明,分号之后是占位符
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 300)
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************掩码字符***************结束
    
    
    # *************光标控制***************开始
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # le_a.move(100, 200)
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 300)
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************光标控制***************结束
    
    
    # *************光标控制***************开始
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le = QLineEdit(window)
    # le.move(100, 100)
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 200)
    #
    # btn = QPushButton(window)
    # btn.setText("按钮")
    # btn.move(100, 300)
    #
    # def cursor_move():
    #     le.cursorBackward(True, 2)  # 向左移动;Ture带选中效果,2移动两个字符
    #     le.cursorForward(True, 3)  # 向右移动;Ture带选中效果,3移动两个字符
    #     le.cursorWordBackward(True)  # 向左移动一个单词长度
    #     le.home(True)  # 移动到首行;Ture带选中效果
    #     le.end(False)  # 移动到末尾;Ture带选中效果
    #     le.setFocus()
    #     le.setCursorPosition(len(le.text()) / 2)  # 设置光标位置
    #     le.setCursorPosition(1.5)
    #     print(le.cursorPosition())  # 获取光标位置
    #     print(le.cursorPositionAt(QPoint(55, 105)))
    #
    # btn.clicked.connect(cursor_move)
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************光标控制***************结束
    
    
    # *************文本边框设置***************开始
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # le_a.move(100, 200)
    # le_a.setStyleSheet("background-color: cyan;")
    # le_a.setTextMargins(50, 0, 50, 50)  # 设置文本边距;int(lift,top,right,bottom)
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 300)
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************文本边框设置***************结束
    
    
    
    # *************对齐方式***************开始
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # le_a.move(100, 200)
    # le_a.setStyleSheet("background-color: cyan;")
    # le_a.setTextMargins(50, 0, 50, 50)
    # le_a.setAlignment(Qt.AlignRight | Qt.AlignBottom)  # 设置对齐方式
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 300)
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************对齐方式***************结束
    
    
    
    # *************信号***************开始
    # app = QApplication(sys.argv)
    #
    # window = QWidget()
    # window.setWindowTitle("QLineEdit功能测试")
    # window.resize(500, 500)
    #
    # le_a = QLineEdit(window)
    # le_a.move(100, 100)
    # le_a.setStyleSheet("background-color: cyan;")
    #
    # le_b = QLineEdit(window)
    # le_b.move(100, 200)
    #
    # le_a.textEdited.connect(lambda val: print("文本框编辑的时候", val))  # 文本编辑时发射信号
    # le_a.textChanged.connect(lambda val: print("文本框内容发生改变", val))  # 文本框发生改变时发射信号
    # le_a.returnPressed.connect(lambda :print("回车键被按下"))  # 按下回车键时发射信号
    # le_a.editingFinished.connect(lambda :print("结束编辑"))  # 结束编辑时发射信号
    #
    # def cao():
    #     a = le_a.text()
    #     b = le_b.text()
    #     print(a, b)
    #
    # btn = QPushButton(window)
    # btn.resize(50,50)
    # btn.move(100, 300)
    # btn.setText('按钮')
    # btn.clicked.connect(cao)
    #
    # window.show()
    # sys.exit(app.exec_())
    # *************信号***************结束
    View Code
  • 相关阅读:
    MSCRM 2011 自定义页面 CrmService 实现增,删,改,查需要注意的
    Microsoft Dynamics CRM 4.0,IFD验证下,自定义aspx页面,如何获取当前的用户ID ?
    CRM 4.0表单脚本开发一览
    MSCRM关于时区时间的操作
    常用JS操作方法
    详解 Visual C# 数据库编程
    Java 日志
    《信息检索导论》第二十章总结
    java文件添加包语句后的编译和运行问题
    查看端口占用情况:FPort和Moo0 ConnectionWatcher软件介绍
  • 原文地址:https://www.cnblogs.com/mosewumo/p/12518413.html
Copyright © 2011-2022 走看看