在 PyQt5 中,如何将普通的 QLineEdit (文本框) 转换为完美的大写/大写 QLineEdit 框?



如何在入门级将我的QLineEdit转换为大写或全部大写

(如果我在文本框(QLineEdit(中输入字符串,则根据用户定义的方法,它会自动转换或格式化输入字符串。(大写或大写((

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()

如果我输入公司名称为"谷歌公司",那么它的转换如下"谷歌公司"。

以下代码对我很好用。 我也是PyQt5和Python的新手。 所以如果你能让它更 pythonic,请告诉我

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()

最新更新