python - Python如何将字符串列表放入html + css字符串?
问题描述
我在 QMessageBox 中有一个使用 css 样式的 html 表,在表中有九个单元格将由变量列表填充,并且我不能将变量放入 html + css 字符串中而不会使程序崩溃。
这个例子:
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
app = QApplication([])
message = QMessageBox()
message.setText(
'<html><body>' \
'Differences between the inputted names and retrieved names have been detected' \
' <p align="center">Comparison:</p>' \
' <style>' \
' table {' \
' border-width: 2px;' \
' border-style: groove;' \
' border-color: #4080c0;' \
' border-radius: 4px;' \
' background: #202020;' \
' color: #00aeef;' \
' }' \
' </style>' \
' <table "cellpadding=3">' \
' <tr>' \
' <td width="25%">Field</td>' \
' <td width="25%">Inputed</td>' \
' <td width="25%">Equivalence</td>' \
' <td width="25%">Retrieved</td>' \
' </tr>' \
' <tr>' \
' <td width="25%">Artist</td>' \
' <td width="25%"><b><i>{0}</b></i></td>' \
' <td width="25%">{1}</td>' \
' <td width="25%"><b><i>{2}</b></i></td>' \
' </tr>' \
' <tr>' \
' <td width="25%">Album</td>' \
' <td width="25%"><b><i>{3}</b></i></td>' \
' <td width="25%">{4}</td>' \
' <td width="25%"><b><i>{5}</b></i></td>' \
' </tr>' \
' <tr>' \
' <td width="25%">Title</td>' \
' <td width="25%"><b><i>{6}</b></i></td>' \
' <td width="25%">{7}</td>' \
' <td width="25%"><b><i>{8}</b></i></td>' \
' </tr>' \
' </table>' \
'Are you sure the provided information is correct?' \
'</body></html>'.format(*["1", "<b> NOT equals </b>", "Electus", "1", "<b> NOT equals </b>", "Temple of Light", "1", "<b> NOT equals </b>", "Temple of Light"])
)
message.setStandardButtons(QMessageBox.StandardButton.Ok)
message.setTextFormat(Qt.TextFormat.MarkdownText)
message.exec()
我在format方法中放入的列表是程序动态生成的列表,我想要一个优雅的方法来填补空白。
实际运行时,列表是一个变量。
尝试运行代码会导致以下错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-1-7082e175a4fa> in <module>
8
9 message.setText(
---> 10 '<html><body>' \
11 'Differences between the inputted names and retrieved names have been detected' \
12 ' <p align="center">Comparison:</p>' \
KeyError: ' border-width'
现在,如果我将所有{n}
s替换为%s
并使用字符串插值运算符:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-2a3d4e1d6147> in <module>
8
9 message.setText(
---> 10 '<html><body>' \
11 'Differences between the inputted names and retrieved names have been detected' \
12 ' <p align="center">Comparison:</p>' \
ValueError: unsupported format character '"' (0x22) at index 441
我已经通过删除样式确认数据将正确显示,并且通过删除样式将起作用,但是我怎样才能同时拥有这两者?
解决方案
推荐阅读
- java - 从 Fragment 到 Activity 从 EditText 获取数据
- javascript - 在检索其内容之前执行页面脚本
- c++ - 检测 C++ 中异常处理的实现类型
- javascript - 为什么 console.log(fibonacci(3)); 打印未定义?
- javascript - 数据推送到数组后切换表中的行 | Vue
- azure - Zip 使用 Azure CLI 将 Azure 功能部署到 Azure 门户
- javascript - Ramda:我怎样才能让这个命令式 reducer 更具声明性?
- c++ - 将参数初始化为构造函数,而不是第一个
- javascript - devDependencies 会影响包大小吗?
- php - 在 Laravel 视图中使用 PHP 获取 HTTP 请求数据?