問題描述
如何在入門級將我的 QLineEdit 轉(zhuǎn)換為大寫或全部大寫?
How to convert My QLineEdit into a Capitalize or all upper Case at Entry Level ?
(如果我在我的文本框(QLineEdit)中輸入字符串,它會根據(jù)用戶定義的方法自動將輸入字符串轉(zhuǎn)換或格式化.(大寫或大寫))
( 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",則其轉(zhuǎn)換如下Google Inc".
If I enter a name of the company as "google inc" then its converts as follows " Google Inc" .
推薦答案
以下代碼對我來說很好.我也是 PyQt5 和 Python 的新手.所以如果你能讓這個(gè)更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()
這篇關(guān)于在 PyQt5 中,如何將普通的 QLineEdit(文本框)轉(zhuǎn)換為完美的大寫/大寫 QLineEdit 框?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!