
momentan wundere ich mich, warum sich MainWindow und Layout meiner GUI.py nicht automatisch anpassen, sobald ich ein QTableWidget aus einer externen Table.py-Datei einbinde. Erstelle ich die Tabelle direkt in der grafischen Oberfläche, wird auch das Layout angepasst. Habe ich hier etwas vergessen oder ist das ein Bug?
Zum Quellcode:
Table.py:
Code: Alles auswählen
import csv, sys
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QFont, QColor
from PyQt6.QtWidgets import QTableWidget, QTableWidgetItem, QApplication, QWidget, QHeaderView
from os.path import isfile
class Table(QWidget):
def __init__(self, fileName):
super(Table, self).__init__()
self.lst = []
self.tmp = []
self.column = 0
self.row = 0
self.cnt = 1
# Files exists?
if isfile(fileName):
# Get all columns
self.column = csv.reader(open(fileName), delimiter=";").__next__().__len__() - 1 # The last sign is \n
for element in csv.reader(open(fileName, "r")):
self.lst.append(element[0])
# Get all rows
self.row = self.lst.__len__()
#
self.header = self.lst[0].split(";")
# Create the table
self.table = QTableWidget(self.row, self.column, self)
self.fontBold = QFont("Open Sans", 12)
self.fontNormal = QFont("Open Sans", 12)
self.fontBold.setBold(True)
# Hide horizonal and vertical header
self.table.horizontalHeader().hide()
self.table.verticalHeader().hide()
#
if self.column == 5:
self.head1 = QTableWidgetItem(self.header[0])
self.head2 = QTableWidgetItem(self.header[1])
self.head3 = QTableWidgetItem(self.header[2])
self.head4 = QTableWidgetItem(self.header[3])
self.head5 = QTableWidgetItem(self.header[4])
self.head1.setFlags(Qt.ItemFlag.ItemIsEditable)
self.head1.setBackground(QColor("#FFDC00"))
self.head1.setForeground(QColor("Black"))
self.head1.setFont(self.fontBold)
self.head2.setFlags(Qt.ItemFlag.ItemIsEditable)
self.head2.setBackground(QColor("#FFDC00"))
self.head2.setForeground(QColor("Black"))
self.head2.setFont(self.fontBold)
self.head3.setFlags(Qt.ItemFlag.ItemIsEditable)
self.head3.setBackground(QColor("#FFDC00"))
self.head3.setForeground(QColor("Black"))
self.head3.setFont(self.fontBold)
self.head4.setFlags(Qt.ItemFlag.ItemIsEditable)
self.head4.setBackground(QColor("#FFDC00"))
self.head4.setForeground(QColor("Black"))
self.head4.setFont(self.fontBold)
self.head5.setFlags(Qt.ItemFlag.ItemIsEditable)
self.head5.setBackground(QColor("#FFDC00"))
self.head5.setForeground(QColor("Black"))
self.head5.setFont(self.fontBold)
self.table.setItem(0, 0, self.head1)
self.table.setItem(0, 1, self.head2)
self.table.setItem(0, 2, self.head3)
self.table.setItem(0, 3, self.head4)
self.table.setItem(0, 4, self.head5)
elif self.column == 4:
self.head1 = QTableWidgetItem(self.header[0])
self.head2 = QTableWidgetItem(self.header[1])
self.head3 = QTableWidgetItem(self.header[2])
self.head4 = QTableWidgetItem(self.header[3])
self.head1.setFlags(Qt.ItemFlag.ItemIsEditable)
self.head1.setBackground(QColor("#FFDC00"))
self.head1.setForeground(QColor("Black"))
self.head1.setFont(self.fontBold)
self.head2.setFlags(Qt.ItemFlag.ItemIsEditable)
self.head2.setBackground(QColor("#FFDC00"))
self.head2.setForeground(QColor("Black"))
self.head2.setFont(self.fontBold)
self.head3.setFlags(Qt.ItemFlag.ItemIsEditable)
self.head3.setBackground(QColor("#FFDC00"))
self.head3.setForeground(QColor("Black"))
self.head3.setFont(self.fontBold)
self.head4.setFlags(Qt.ItemFlag.ItemIsEditable)
self.head4.setBackground(QColor("#FFDC00"))
self.head4.setForeground(QColor("Black"))
self.head4.setFont(self.fontBold)
self.table.setItem(0, 0, self.head1)
self.table.setItem(0, 1, self.head2)
self.table.setItem(0, 2, self.head3)
self.table.setItem(0, 3, self.head4)
self.lst.pop(0) # Delete header
# Insert content in the table
for element in self.lst:
self.tmp = element.split(";")
if self.column == 4:
self.content1 = QTableWidgetItem(self.tmp[0])
self.content2 = QTableWidgetItem(self.tmp[1])
self.content3 = QTableWidgetItem(self.tmp[2])
self.content4 = QTableWidgetItem(self.tmp[3])
self.content1.setFlags(Qt.ItemFlag.ItemIsEditable)
self.content1.setForeground(QColor("Black"))
self.content1.setFont(self.fontNormal)
self.content2.setFlags(Qt.ItemFlag.ItemIsEditable)
self.content2.setForeground(QColor("Black"))
self.content2.setFont(self.fontNormal)
self.content3.setFlags(Qt.ItemFlag.ItemIsEditable)
self.content3.setForeground(QColor("Black"))
self.content3.setFont(self.fontNormal)
self.content4.setFlags(Qt.ItemFlag.ItemIsEditable)
self.content4.setForeground(QColor("Black"))
self.content4.setFont(self.fontNormal)
self.table.setItem(self.cnt, 0, self.content1)
self.table.setItem(self.cnt, 1, self.content2)
self.table.setItem(self.cnt, 2, self.content3)
self.table.setItem(self.cnt, 3, self.content4)
elif self.column == 5:
self.content1 = QTableWidgetItem(self.tmp[0])
self.content2 = QTableWidgetItem(self.tmp[1])
self.content3 = QTableWidgetItem(self.tmp[2])
self.content4 = QTableWidgetItem(self.tmp[3])
self.content5 = QTableWidgetItem(self.tmp[4])
self.content1.setFlags(Qt.ItemFlag.ItemIsEditable)
self.content1.setForeground(QColor("Black"))
self.content1.setFont(self.fontNormal)
self.content2.setFlags(Qt.ItemFlag.ItemIsEditable)
self.content2.setForeground(QColor("Black"))
self.content2.setFont(self.fontNormal)
self.content3.setFlags(Qt.ItemFlag.ItemIsEditable)
self.content3.setForeground(QColor("Black"))
self.content3.setFont(self.fontNormal)
self.content4.setFlags(Qt.ItemFlag.ItemIsEditable)
self.content4.setForeground(QColor("Black"))
self.content4.setFont(self.fontNormal)
self.content5.setFlags(Qt.ItemFlag.ItemIsEditable)
self.content5.setForeground(QColor("Black"))
self.content5.setFont(self.fontNormal)
self.table.setItem(self.cnt, 0, self.content1)
self.table.setItem(self.cnt, 1, self.content2)
self.table.setItem(self.cnt, 2, self.content3)
self.table.setItem(self.cnt, 3, self.content4)
self.table.setItem(self.cnt, 4, self.content5)
self.tmp.clear()
self.cnt += 1
#
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents) # Auto resize table
Code: Alles auswählen
import sys
from helper.miscellaneous import getPrinter
from PyQt6.QtGui import QIcon, QCursor
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMainWindow, QHBoxLayout, QVBoxLayout, QRadioButton, QGroupBox, QLabel, QComboBox, QCheckBox, QPushButton, QTableWidget
from PyQt6.QtCore import Qt
from helper.Table import Table
class MainWindow(QMainWindow):
def __init__(self, title = ""):
super(MainWindow, self).__init__()
self.widget = QWidget()
self.grpTable = QGroupBox()
self.lytTable = QHBoxLayout()
self.lytFrame = QVBoxLayout()
self.tblDevices = Table("files/Tablets.csv")
...
self.lytTable.addWidget(self.tblDevices)
...
self.grpTable.setLayout(self.lytTable)
...
self.lytFrame.addWidget(self.grpTable)
self.widget.setLayout(self.lytFrame)
self.setCentralWidget(self.widget) # Set the default layout
...
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow("Test")
# Only set the close button and a title bar icon
win.setWindowFlags(Qt.WindowType(Qt.WindowType.CustomizeWindowHint | Qt.WindowType.WindowCloseButtonHint))
win.setWindowIcon(QIcon("Logo.ico"))
#
win.show() # Make the main window visible
sys.exit(app.exec()) # Execute the event loop
LG McProgger