欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQt5實(shí)現(xiàn)QLineEdit正則表達(dá)式輸入驗(yàn)證器

 更新時(shí)間:2021年04月07日 09:44:24   作者:魚(yú)子醬126  
這篇文章主要介紹了PyQt5實(shí)現(xiàn)QLineEdit正則表達(dá)式輸入驗(yàn)證器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

本文主要介紹了QLineEdit正則表達(dá)式輸入驗(yàn)證器,分享給大家,具體如下:

from PyQt5 import QtWidgets, QtCore, QtGui, Qt
import re

############## QLineEdit正則表達(dá)式輸入驗(yàn)證器
class LineEditRegExpValidator(QtGui.QValidator):

  '''
  # 默認(rèn)為科學(xué)計(jì)數(shù)法輸入驗(yàn)證器

  用法
  SciNotValidator = LineEditRegExpValidator() # 創(chuàng)建一個(gè)QLineEdit正則表達(dá)式輸入驗(yàn)證器的類,默認(rèn)為科學(xué)計(jì)數(shù)法輸入驗(yàn)證器

  self.LineEdit1.setValidator(SciNotValidator) # 設(shè)置驗(yàn)證器(啟用)
  self.LineEdit1.installEventFilter(SciNotValidator) # QLineEdit清空內(nèi)容且游標(biāo)失焦時(shí),自動(dòng)填充上一次的字符串內(nèi)容

  self.LineEdit2.setValidator(SciNotValidator)
  self.LineEdit2.installEventFilter(SciNotValidator)

  self.LineEdit3.setValidator(SciNotValidator)
  self.LineEdit3.installEventFilter(SciNotValidator)

  Validator.validate() is abstract and must be overriddenValidator.validate() is abstract and must be overridden
  '''

  def __init__(
    self, 

    # 編輯狀態(tài)框輸入結(jié)束允許的字符串
    fullPatterns=[
      r"[+|-]?[0-9]+\.?[0-9]*(?:[Ee][+|-]?[0-9]+)?", 
      r'[+|-]{0,1}nan', r'[+|-]{0,1}inf'
      ], 
    
    # 編輯狀態(tài)框輸入尚未結(jié)束允許的字符串
    partialPatterns=[
      r'[+|-]?[0-9]+\.?[0-9]*(?:[Ee][+|-]?)?', 
      r'-', 
      r'\+', 
      r'[+|-]{0,1}nan', 
      r'[+|-]{0,1}na', 
      r'[+|-]{0,1}n', 
      r'[+|-]{0,1}inf', 
      r'[+|-]{0,1}in', 
      r'[+|-]{0,1}i'
      ],
    
    fixupString='1.0'
    ):

    super(LineEditRegExpValidator, self).__init__()
    self.fullPatterns = fullPatterns
    self.partialPatterns = partialPatterns
    self.fixupString = fixupString
  

  # 實(shí)時(shí)監(jiān)聽(tīng)文本框的改變
  # 可能是鍵盤單個(gè)字符'n'輸入, 也有可能是粘貼多個(gè)字符'nan'輸入
  def validate(self, string, pos) -> QtGui.QValidator.State: # string為編輯狀態(tài)框中可見(jiàn)的字符串+輸入字符/字符串

    # 編輯過(guò)程結(jié)束,若返回True,將編輯狀態(tài)框中的字符串填入LineEdit,若返回Flase則自動(dòng)調(diào)用self.fixup方法,將fixup方法返回的字符串填入LineEdit
    if self.acceptable_check(string):
      #print(f'QtGui.QValidator.Acceptable:{QtGui.QValidator.Acceptable}')
      return QtGui.QValidator.Acceptable, string, pos # QtGui.QValidator.Acceptable = 2; 
    

    # 編輯過(guò)程中允許出現(xiàn)的字符串
    if self.intermediate_check(string):
      #print(f'QtGui.QValidator.Intermediate:{QtGui.QValidator.Intermediate}')
      return QtGui.QValidator.Intermediate, string, pos # QtGui.QValidator.State = 1;
    # 編輯過(guò)程中不允許出現(xiàn)的字符串(本次輸入的單個(gè)字符或字符串無(wú)效)
    else:
      #print(f'QtGui.QValidator.Invalid:{QtGui.QValidator.Invalid}')
      return QtGui.QValidator.Invalid, string, pos


  # 編輯狀態(tài)框驗(yàn)證通過(guò), 編輯狀態(tài)框單個(gè)字輸入符成功
  def acceptable_check(self, string) -> bool:
    True_ = 0
    for fullPattern in self.fullPatterns:
      if re.fullmatch(fullPattern, string):
        True_ += 1
      else:
        continue
    if True_ != 0:
      return True
    else:
      return False

  # 輸入還未結(jié)束允許的字符串
  def intermediate_check(self, string): #-> bool;  string為編輯狀態(tài)框中可見(jiàn)的字符串
    """
    Checks if string makes a valid partial float, keeping in mind locale dependent decimal separators.
    """
    if string == '':
      return True
    for partialPattern in self.partialPatterns:
      if re.fullmatch(partialPattern, string):
        return True
      else:
        pass

  # 
  def eventFilter(self, lineEdit, event): # -> bool
    # FocusIn event
    # 每當(dāng)fous in時(shí),更新LineEditRegExpValidator的fixupString
    # 輸入驗(yàn)證器
    '''
    SciNotValidator = LineEditRegExpValidator()

    self.LineEdit1.setValidator(SciNotValidator)
    self.LineEdit1.installEventFilter(SciNotValidator)
    '''

    if event.type() == QtCore.QEvent.FocusIn:
      # do custom stuff
      # print('focus in')

      # self.lineEdit_zhuansu.installEventFilter(SciNotValidator), 在本類中,widget是self.lineEdit,執(zhí)行函數(shù)self.lineEdit.text(), 其它類不一定有text()方法

      #lineEdit.selectAll()
      QtCore.QTimer.singleShot(0, lineEdit.selectAll) # 0ms
      self.fixupString = lineEdit.text()

      #print(self.fixupString)
      # return False so that the lineEdit will also handle the event
      # otherwise it won't focus out
      return False
    else:
      # we don't care about other events
      return False

  # 重寫QValidator的fixup(str)方法??梢栽谇袚Q焦點(diǎn)后,直接修改不合規(guī)則的字符串。參數(shù)str是經(jīng)過(guò)validate()方法驗(yàn)證后的字符串;
  def fixup(self, string) -> str:
    """
    Fixes up input text to create a valid float. Puts an empty string on failure.
    """
    print(string)

    True_ = 0
    for fullPattern in self.fullPatterns:
      if re.fullmatch(fullPattern, string):
        True_ += 1
      else:
        continue
    if True_ != 0:
      return string
    else:
      return self.fixupString


# listWidget、tableWidget輸入數(shù)據(jù)檢查
class LineEditDelegate_Regx(QtWidgets.QStyledItemDelegate):
 # 科學(xué)計(jì)數(shù)法正則表達(dá)式
  regx = r"-?\ *[0-9]+\.?[0-9]*(?:[Ee]\ *-?\ *[0-9]+)?" #
  """
  -?    optionally matches a negative sign (zero or one negative signs)
  \ *    matches any number of spaces (to allow for formatting variations like - 2.3 or -2.3)
  [0-9]+  matches one or more digits
  \.?    optionally matches a period (zero or one periods)
  [0-9]*  matches any number of digits, including zero
  (?: ... ) groups an expression, but without forming a "capturing group" (look it up)
  [Ee]   matches either "e" or "E"
  \ *    matches any number of spaces (to allow for formats like 2.3E5 or 2.3E 5)
  -?    optionally matches a negative sign
  \ *    matches any number of spaces
  [0-9]+  matches one or more digits
  ?     makes the entire non-capturing group optional (to allow for the presence or absence of the exponent - 3000 or 3E3

  https://stackoverflow.com/questions/18152597/extract-scientific-number-from-string
  """

  """
  用法:
  def __init__(self, parent=None):
    super(NewClassName, self).__init__(parent)
    self.setupUi(self)

    delegate = LineEditDelegate_Regx(regx=None)
    self.listWidget_ShuZhiLieBiao.setItemDelegate(delegate)
    self.tableWidget.setItemDelegate(delegate)
  """
  def __init__(self, regx=None, parent=None):
    super(LineEditDelegate_Regx, self).__init__(parent)
    if regx == None:
      pass
    else:
      self.regx = regx

  # 方法重寫
  def createEditor(self, parent, option, index): # self, parent, option, index四個(gè)參數(shù)均不能少
    editor_qlineedit = QtWidgets.QLineEdit(parent)
    #SciNotValidator = QtGui.QRegExpValidator(QtCore.QRegExp(self.regx))
    SciNotValidator = LineEditRegExpValidator()
    editor_qlineedit.setValidator(SciNotValidator)
    return editor_qlineedit # LineEditDelegate_Regx(regx=None, parent=None), QStyledItemDelegate(parent: QObject = None)

"""
# LineEdit輸入數(shù)據(jù)檢查
def LineEditInputChecking(lineEdit, regx=None):
  '''
  用法:
  LineEditInputChecking(lineEdit=self.lineEdit_zhuansu)
  '''
  if regx == None:
    regx = r"-?\ *[0-9]+\.?[0-9]*(?:[Ee]\ *-?\ *[0-9]+)?"
  reg_ex = QtCore.QRegExp(regx)
  input_validator = QtGui.QRegExpValidator(reg_ex, lineEdit)
  lineEdit.setValidator(input_validator)
"""

參考:

https://stackoverflow.com/questions/39202697/qt-qlineedit-input-validation

https://stackoverflow.com/questions/15829782/how-to-restrict-user-input-in-qlineedit-in-pyqt

到此這篇關(guān)于PyQt5實(shí)現(xiàn)QLineEdit正則表達(dá)式輸入驗(yàn)證器的文章就介紹到這了,更多相關(guān)PyQt5 QLineEdit驗(yàn)證器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 用Python手把手教你實(shí)現(xiàn)2048小游戲

    用Python手把手教你實(shí)現(xiàn)2048小游戲

    感覺(jué)好久沒(méi)和大家一起寫小游戲玩了,今天恰巧有空.這次我們來(lái)用Python做個(gè)2048小游戲吧.廢話不多說(shuō),文中有非常詳細(xì)的代碼示例,需要的朋友可以參考下
    2021-06-06
  • python 腳本生成隨機(jī) 字母 + 數(shù)字密碼功能

    python 腳本生成隨機(jī) 字母 + 數(shù)字密碼功能

    本文通過(guò)一小段簡(jiǎn)單的代碼給大家分享基于python 腳本生成隨機(jī) 字母 + 數(shù)字密碼功能,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-05-05
  • 在Linux命令行終端中使用python的簡(jiǎn)單方法(推薦)

    在Linux命令行終端中使用python的簡(jiǎn)單方法(推薦)

    下面小編就為大家?guī)?lái)一篇在Linux命令行終端中使用python的簡(jiǎn)單方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Python基礎(chǔ)之模塊相關(guān)知識(shí)總結(jié)

    Python基礎(chǔ)之模塊相關(guān)知識(shí)總結(jié)

    今天帶大家復(fù)習(xí)Python基礎(chǔ)知識(shí),文中對(duì)模塊相關(guān)知識(shí)介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Python 讀取千萬(wàn)級(jí)數(shù)據(jù)自動(dòng)寫入 MySQL 數(shù)據(jù)庫(kù)

    Python 讀取千萬(wàn)級(jí)數(shù)據(jù)自動(dòng)寫入 MySQL 數(shù)據(jù)庫(kù)

    這篇文章主要介紹了Python 讀取千萬(wàn)級(jí)數(shù)據(jù)自動(dòng)寫入 MySQL 數(shù)據(jù)庫(kù),本篇文章會(huì)給大家系統(tǒng)的分享千萬(wàn)級(jí)數(shù)據(jù)如何寫入到 mysql,分為兩個(gè)場(chǎng)景,兩種方式
    2022-06-06
  • python實(shí)現(xiàn)的自動(dòng)發(fā)送消息功能詳解

    python實(shí)現(xiàn)的自動(dòng)發(fā)送消息功能詳解

    這篇文章主要介紹了python實(shí)現(xiàn)的自動(dòng)發(fā)送消息功能,涉及Python基于requests、itchat庫(kù)的數(shù)據(jù)請(qǐng)求與信息處理相關(guān)操作技巧,需要的朋友可以參考下
    2019-08-08
  • Python模塊zipfile原理及使用方法詳解

    Python模塊zipfile原理及使用方法詳解

    這篇文章主要介紹了Python模塊zipfile原理及使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • python Elasticsearch索引建立和數(shù)據(jù)的上傳詳解

    python Elasticsearch索引建立和數(shù)據(jù)的上傳詳解

    在本篇文章里小編給大家整理的是關(guān)于基于python的Elasticsearch索引的建立和數(shù)據(jù)的上傳的知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考下。
    2019-08-08
  • Python中死鎖的形成示例及死鎖情況的防止

    Python中死鎖的形成示例及死鎖情況的防止

    由于Python中允許創(chuàng)建多個(gè)線程,那么互斥鎖或者線程同時(shí)獲取多個(gè)鎖的情況就有可能發(fā)生,這里我們就來(lái)看一下Python中死鎖的形成示例及死鎖情況的防止:
    2016-06-06
  • Python3 正在毀滅 Python的原因分析

    Python3 正在毀滅 Python的原因分析

    Python 2強(qiáng)大的一個(gè)地方是它身后巨大的第三方庫(kù),恩,可以做任何事。Python 3沒(méi)有這個(gè)。是的,很多庫(kù)已經(jīng)移植了,但是仍然有數(shù)以十倍的庫(kù)沒(méi)有移植過(guò)去,而且也不太容易
    2014-11-11

最新評(píng)論