[英]How to Format a string on a textedit box in QErrorMessage in PyQt5?
如何在 QErrorMessage 中的 TextEdit Box 上新建一行? 以及如何修剪 windows 標題中的空格? 這是我的代碼,
self.error_msg = QErrorMessage()
self.error_msg.setFixedSize(500,200)
exc_type, exc_value, exc_traceback = sys.exc_info()
filename = exc_traceback.tb_frame.f_code.co_filename
lineno = exc_traceback.tb_lineno
name = exc_traceback.tb_frame.f_code.co_name
type = exc_type.__name__
message = exc_value
nl = '\n'
self.error_msg.setWindowTitle(f'{type}')
self.error_msg.showMessage(f'File Name : {filename[:-3]}{nl}Line No. : {lineno}')
print(f'File Name : {filename[:-2]}{nl} Line No. : {lineno}')
此代碼產生以下結果:
如何修剪 windows 標題中的空格(AttributeError 和問號之間的空格)以及如何在 Qtextedit 框中新建一行(文件名在第一行,行號在下一行)
文本字段是標准的 QTextEdit, showMessage()
實際上使用setHtml()
。
在 HTML 中,換行符被視為空格(並以相同的方式與它們合並)。 只需使用標准的 HTML 換行符:
n1 = '<br/>'
該問號與標題文本無關,也與 QErrorMessage 無關:它是任何標准 Windows 對話框中存在的標准上下文幫助按鈕(因此,所有 QDialog 子類默認使用它)。 由於它是一個 window 標志,只需使用自定義 window 提示的標准方法:
self.error_msg.setWindowFlag(Qt.WindowContextHelpButtonHint, False))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.