1

How to convert My QLineEdit into a Capitalize or all upper Case at Entry Level ?

( If I enter string into my text box (QLineEdit), automatically its converts or format the input string to, as per user defined method. ( Capitalize or Upper Case ))

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


class textbox_example(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle(" QLine Edit Example")
        self.setGeometry(50, 50, 1500, 600)

        self.tbx_search = QLineEdit(self)
        self.tbx_search.setGeometry(50, 50, 300, 30)
        self.tbx_search.setPlaceholderText("Enter,Name of the Company")
        self.tbx_search.setFont(QFont("caliber", 10, QFont.Capitalize))

def main():
    myapp = QApplication(sys.argv)
    mywindow = textbox_example()
    mywindow.show()
    sys.exit(myapp.exec_())


if __name__ == "__main__":
    main()

If I enter a name of the company as "google inc" then its converts as follows " Google Inc" .

Bala
  • 648
  • 5
  • 15
  • Typo: add `)`at the end. – eyllanesc May 12 '20 at 13:17
  • ")" is missed here, but in my programme , i Put the bracket and my programe format is as shown below : self.tbx_search.setFont(QFont("caliber",20,QFont.Capitalize)) – Bala May 12 '20 at 13:22
  • 1) Correct the code of your post, 2) Provide a [mre] and 3) Run your code in the CMD / console so that you get an error message with more information than a simple numeric code. – eyllanesc May 12 '20 at 13:25
  • thanks @eyllanesc, attach my full code and myrequirements. – Bala May 12 '20 at 13:56
  • Duplicate of [Uppercase input in QLineEdit python way](https://stackoverflow.com/questions/28962266) – eyllanesc May 12 '20 at 14:00
  • Yes, In my google search, I go through the post.(as you mentioned) That post is in PyQt4 Format and more over its more than 4 yrs and 11 months before. So i think, there is any simple way or any new techniques ,maybe now available in PyQt5. So only I ask it again. – Bala May 12 '20 at 14:10
  • No, there are no substantial changes between PyQt4 and PyQt5 if we refer to the QtGui and QtWidgets sub-modules. Qt5 has moved in other directions: add more modules or improve QML. The Qt Widgets are already mature software (over 20 years old) so there will be no substantial changes, maybe Qt6 will make changes but they will be internal – eyllanesc May 12 '20 at 14:13

1 Answers1

1

The following code works to me fine. I am also new to PyQt5 and Python. so if you can make this more pythonic please let me know

import sys
from PyQt5.QtWidgets import *

class textbox_example(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle(" QLine Edit Example")
        self.setGeometry(50, 50, 1500, 600)

        self.tbx_search = QLineEdit(self)
        self.tbx_search.setGeometry(50, 50, 300, 30)
        self.tbx_search.setPlaceholderText("Enter,Name of the Company")
        self.tbx_search.textChanged.connect(self.auto_capital)

    def auto_capital(self, txt):
        cap_text = txt.title()  
        upp_text = txt.upper()  # All Upper Case
        self.tbx_search.setText(cap_text)

def main():
    myapp = QApplication(sys.argv)
    mywindow = textbox_example()
    mywindow.show()
    sys.exit(myapp.exec_())

if __name__ == "__main__":
    main()
Kumar
  • 592
  • 6
  • 18
  • 1
    Note that `setText` also moves the cursor to the *end* of the string, so if the text is changed before the last character the resulting behavior can be annoying to the user. You should store the current position in a local variable using `oldPos = self.tbx_search.cursorPosition()` *before* `setText()`, and then restore it back with `self.tbx_search.setCursorPosition(oldPos)` *after*. – musicamante Aug 22 '21 at 21:25