0

I have a html table in a QMessageBox that is styled using css, in the table there are nine cells that will be filled by a list of variables, and I can't put the variables into the html + css string without crashing the program.

The example:

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()

The list I put in format method is a list dynamically generated by the program, and I want an elegant method to fill in the blanks.

When actually run, the list is a variable.

Trying to run the code results the following error:

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

Now if I replace all {n}s with %s and use string interpolation operator:

---------------------------------------------------------------------------
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

I have confirmed by removing the styling the data will be displayed correctly, and by removing the data the styling will work, but how can I have both?

Ξένη Γήινος
  • 2,181
  • 1
  • 9
  • 35

1 Answers1

1

In the section:

table {
    border-width: ...
}

replace it to

table {{
    border-width: ...
}}

because python thinks it's a format string, as {}

enter image description here

Jonathan1609
  • 1,809
  • 1
  • 5
  • 22