0

I have a dataframe with many columns (about 20 columns). I need to display the dataframe in QTextEdit in a way that the headers of columns will be aligned with column values. Here for simplicity I used just 3 columns.

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
import pandas as pd

class Window(QWidget):
    singleton: 'Window' = None

    def __init__(self):
        super(Window, self).__init__()
        self.setWindowTitle("Software tool")
        self.setGeometry(50, 50, 1800, 900)
        self.mainLayout=QHBoxLayout()
        self.setLayout(self.mainLayout)
        self.UI()

    def UI(self):
        self._view()
        self._fillTextEdit()
        self.show()

    def _view(self):
        self.textEdit = QTextEdit()
        self.textEdit.setLineWrapMode(QTextEdit.NoWrap)
        self.mainLayout.addWidget(self.textEdit)

    def _fillTextEdit(self):
        list = {
            'Name of the room': ['Bedroom', 'Kitchen', 'Bathroom'],
            'Area in square meters': [12, 18, 6],
            'Paint color': ['White', 'Green', 'Blue']}
        self.df = pd.DataFrame(list)
        self.textEdit.append(self.df.to_string(index = False))
        
def main():
  App=QApplication(sys.argv)
  window =Window()
  sys.exit(App.exec_())

if __name__ == '__main__':
  main()

Current output:

Name of the room  Area in square meters Paint color
       Bedroom              12        White
       Kitchen             18        Green
      Bathroom               6         Blue

Here is what I would like as my output:

Name of the room  Area in square meters Paint color
         Bedroom                     12       White
         Kitchen                     18       Green
        Bathroom                      6        Blue

0 Answers0