简体   繁体   English

如何在 PyQt5 的 QErrorMessage 中的文本编辑框中格式化字符串?

[英]How to Format a string on a textedit box in QErrorMessage in PyQt5?

How to make a new line on TextEdit Box in QErrorMessage?如何在 QErrorMessage 中的 TextEdit Box 上新建一行? and How to trim the blank space in windows headings?以及如何修剪 windows 标题中的空格? Here is My Code,这是我的代码,

    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}')

This code produces the following result:此代码产生以下结果:

在此处输入图像描述

How to trim the blank space in windows headings ( Space between AttributeError and question mark) and How to make a new line in Qtextedit box ( File name is in first line, Line No in next line)如何修剪 windows 标题中的空格(AttributeError 和问号之间的空格)以及如何在 Qtextedit 框中新建一行(文件名在第一行,行号在下一行)

The text field is a standard QTextEdit, and showMessage() actually uses setHtml() .文本字段是标准的 QTextEdit, showMessage()实际上使用setHtml()

In HTML, new line characters are treated as white spaces (and merged with them in the same manner).在 HTML 中,换行符被视为空格(并以相同的方式与它们合并)。 Just use the standard HTML line break separator:只需使用标准的 HTML 换行符:

n1 = '<br/>'

That question mark has nothing to do with the title text , nor QErrorMessage: it's the standard context help button that exists on any standard Windows dialog (so, all QDialog subclasses use it by default).该问号与标题文本无关,也与 QErrorMessage 无关:它是任何标准 Windows 对话框中存在的标准上下文帮助按钮(因此,所有 QDialog 子类默认使用它)。 Since it's a window flag, just use the standard method for customizing window hints:由于它是一个 window 标志,只需使用自定义 window 提示的标准方法:

self.error_msg.setWindowFlag(Qt.WindowContextHelpButtonHint, False))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM