【问题标题】:Python: How to control Lineedit input and output in PyQtPython:如何在 PyQt 中控制 Lineedit 的输入和输出
【发布时间】:2014-03-19 23:59:44
【问题描述】:

下面发布的代码创建了一个只有小部件的简单对话框窗口:QLineEdit。 在此字段中键入任何内容都会触发 fixText() 方法(在 textChanged 上),该方法会清除传入的字符串参数。在字符串被“清理”后,该方法会使用结果更新 QLineEdit。

目标:无论用户在 lineedit 字段中输入什么内容:

前四个字母总是大写。 第五个字符始终是下划线。

除了 cleanupString() 所做的之外,不必清除字符串中的剩余字符。

需要实现的示例:ABCD_helloWorld

遇到的问题:无法插入下划线字符而不搞砸。 负责插入下划线的代码行目前已注释...

import sys, os
from PyQt4 import QtCore, QtGui  
class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()       


        self.myQLineEdit = QtGui.QLineEdit("Type text here")
        self.myQLineEdit.textChanged.connect(self.fixText)

        myBoxLayout.addWidget(self.myQLineEdit)        

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.setWindowTitle('Dialog 01')

    def fixText(self, arg):
        arg=str(arg)
        if not arg: return

        arg=self.cleanupString(arg)

        if len(arg)<3: result=arg.upper()
        else:     result = arg[0:4].upper()+arg[4:]
        # resultList=list(result)
        # resultList.insert(4, '_')
        # result=''.join(resultList)
        self.myQLineEdit.blockSignals(True)
        self.myQLineEdit.setText(result)
        self.myQLineEdit.blockSignals(False)

    def cleanupString(self, line=None):
        if line==None: return
        invalid = invalid = ['!','"','#','$','%','&','\\','(',')','*','+',',','-','.','/'
                    ,':',';','<','=','>','?','@','[',"'",']','^','`','{','|','}','~', ' ']
        for c in invalid: 
            if len(line)>0: line=line.replace(c,'_')
        return line


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

固定代码:

import sys, os
from PyQt4 import QtCore, QtGui    

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()       


        self.myQLineEdit = QtGui.QLineEdit("Type text here")
        self.myQLineEdit.textChanged.connect(self.fixText)

        myBoxLayout.addWidget(self.myQLineEdit)        

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.setWindowTitle('Dialog 01')

    def fixText(self, arg):
        arg=str(arg)
        if not arg: return

        arg=self.cleanupString(arg)

        if len(arg)<=3: result=arg.upper()
        else:     result = arg[0:4].upper()+"_"+arg[5:]

        self.myQLineEdit.blockSignals(True)
        self.myQLineEdit.setText(result)
        self.myQLineEdit.blockSignals(False)

    def cleanupString(self, line=None):
        if line==None: return
        invalid = invalid = ['!','"','#','$','%','&','\\','(',')','*','+',',','-','.','/'
                    ,':',';','<','=','>','?','@','[',"'",']','^','`','{','|','}','~', ' ']
        for c in invalid: 
            if len(line)>0: line=line.replace(c,'_')
        return line

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

【问题讨论】:

    标签: python pyqt qlineedit textchanged


    【解决方案1】:

    你可以这样做:

        if len(arg)<=3: result=arg.upper()
        else: result = arg[0:4].upper()+"_"+arg[5:]
    

    虽然,您将无法使用退格键删除...您必须选择要删除的文本。

    【讨论】:

    • 太棒了!谢谢!简单!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2019-08-20
    • 2021-11-02
    • 2014-11-22
    • 1970-01-01
    • 2019-05-01
    • 2011-09-13
    相关资源
    最近更新 更多