【问题标题】:Uppercase input in QLineEdit python wayQLineEdit python方式中的大写输入
【发布时间】:2015-03-10 11:15:29
【问题描述】:

我使用 QT Designer 绘制了一个 UI,但发现没有参数可以让我将 QLineEdit 输入设置为大写。

在进行了一些在线搜索后,我只看到了极少数满足我需求的结果,但是所有这些都是用 Qt 编码的。比如这个link

那么,我有没有办法以 Python 的方式做到这一点?

【问题讨论】:

  • 首先,您的链接是关于 C++,而不是 Python。那么,为什么不只使用string_got_from_lineEdit.upper()
  • @ForceBru upper() 在用户输入后使用,然后转换为大写,不是吗?我想要的是用户输入内容时的大写。

标签: python pyqt qlineedit


【解决方案1】:

最简单的方法是使用validator

这将立即将用户键入或粘贴到行编辑中的任何内容大写:

from PyQt4 import QtCore, QtGui

class Validator(QtGui.QValidator):
    def validate(self, string, pos):
        return QtGui.QValidator.Acceptable, string.upper(), pos
        # for old code still using QString, use this instead
        # string.replace(0, string.count(), string.toUpper())
        # return QtGui.QValidator.Acceptable, pos

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.edit = QtGui.QLineEdit(self)
        self.validator = Validator(self)
        self.edit.setValidator(self.validator)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.edit)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 300, 100)
    window.show()
    sys.exit(app.exec_())

【讨论】:

  • 在尝试时,我收到以下错误 - # AttributeError: 'QString' object has no attribute 'upper',它来自 class Validator 中的 return 语句。
  • @dissidia。如果您仍在使用过时的QString API,则需要稍微不同的方法。请参阅我的更新答案。
  • 您的答案有效!非常感谢。但问题 - 我如何知道我是否使用过时的 QString API?
  • @dissidia。对于带有 Python2 的 PyQt4,默认情况下会得到 QString;但是使用 Python3,默认情况下你会得到普通的 Python 字符串。但是,可以使用sip.setapi 更改这些默认值。对于 PyQt5,根本没有 QString 支持,这就是我说这些 API 已经过时的原因。从我上面的回答中可以看出,当您不必处理 QString 时,PyQt 代码通常要简单得多(QVariant 也是如此)。
【解决方案2】:

试试这个, 我相信这符合您的目的。我不会称它为pythonic。更像是 PyQt 覆盖。

#次要代码编辑

from PyQt4 import QtGui
import sys
#===============================================================================
# MyEditableTextBox-  
#===============================================================================
class MyEditableTextBox(QtGui.QLineEdit):
#|-----------------------------------------------------------------------------|
# Constructor  
#|-----------------------------------------------------------------------------|

    def __init__(self,*args):
        #*args to set parent
        QtGui.QLineEdit.__init__(self,*args)

#|-----------------------------------------------------------------------------|
# focusOutEvent :- 
#|-----------------------------------------------------------------------------|
    def focusOutEvent(self, *args, **kwargs):
        text = self.text()
        self.setText(text.__str__().upper())
        return QtGui.QLineEdit.focusOutEvent(self, *args, **kwargs)


#|--------------------------End of focusOutEvent--------------------------------|
#|-----------------------------------------------------------------------------| 
# keyPressEvent
#|-----------------------------------------------------------------------------|
    def keyPressEvent(self, event):
        if not self.hasSelectedText():
            pretext = self.text()
            self.setText(pretext.__str__().upper())
        return QtGui.QLineEdit.keyPressEvent(self, event)

#|--------------------End of keyPressEvent-------------------------------------|

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QWidget()
    lay = QtGui.QHBoxLayout()
    w.setLayout(lay)
    le1 = MyEditableTextBox()
    lay.addWidget(le1)
    le2 = MyEditableTextBox()
    lay.addWidget(le2)
    w.show()
    sys.exit(app.exec_())

【讨论】:

  • 我收到错误 - # AttributeError: 'QString' object has no attribute 'upper',它来自 focusOutEvent 函数 - self.setText(text.upper()) 另外,QLineEdit 没有在我输入的字符中注册,可能是由于错误
  • 您使用的是什么 qt/pyqt 和 python 版本?在我的情况下它工作正常。您可能希望将 focusOutEvent 和 keyPressEvent 中对 upper 的调用替换为以下内容: text.__str__().upper() 和 pretext.__str__().upper() ...刚刚为此编辑了代码...跨度>
  • 这些是我的版本 - ('Qt version:', '4.8.2') 和 ('PyQt version:', '4.9.6')
  • 是的,它有效,但我意识到我输入的最后一个字符是小写的,例如。 “FIVe”,除非键入了另一个字符或按下了空格键。只是想知道它应该是这样的吗?
  • @smitkpatel。您可以通过调用QLineEdit.keyPressEvent重新设置文本来消除最后一个字符的问题(请注意,您不需要return)。此外,重置文本的更简单方法是使用self.setText(self.text().toUpper())
【解决方案3】:

嘿,我知道我有点晚了,但我希望这可以帮助像我这样花了一些时间寻找这个的人

Mycase: 我试图只将第一个字母转换为大写,这就是我最终得到的结果并且它起作用了(只是 python 的初学者,所以如果你能让这个更pythonic,请告诉我)

在定义函数中:line_edit_object.textChanged.connect(lambda:auto_capital(line_edit_object))

函数auto_capital:

def auto_capital(line_edit_object):
    edit=line_edit_object
    text=edit.text()
    edit.text(text.title())

这将解决所有问题。随意使它更 pytonic。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多