在 PyQt5 中,如何将普通的 QLineEdit(文本框)转换为完美的大写/大写 QLineEdit 框?
- 作者: 我假装不懂的样子
- 来源: 51数据库
- 2022-12-23
问题描述
如何在入门级将我的 QLineEdit 转换为大写或全部大写?
How to convert My QLineEdit into a Capitalize or all upper Case at Entry Level ?
(如果我在我的文本框(QLineEdit)中输入字符串,它会根据用户定义的方法自动将输入字符串转换或格式化.(大写或大写))
( 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()
如果我输入公司名称为google inc",则其转换如下Google Inc".
If I enter a name of the company as "google inc" then its converts as follows " Google Inc" .
推荐答案
以下代码对我来说很好.我也是 PyQt5 和 Python 的新手.所以如果你能让这个更pythonic,请告诉我
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()
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
