that may be an easy Question for many, however i still struggle with acessing Items that i created in QT Designer.
I created a simple GUI that shows a 16x5 Table with QT Designer and converted the .ui to a .py file.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(686, 539)
self.widget = QtWidgets.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(10, 60, 258, 223))
self.widget.setObjectName("widget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.tableWidget = QtWidgets.QTableWidget(self.widget)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(4)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.verticalLayout.addWidget(self.tableWidget)
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("Form", "1"))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("Form", "2"))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("Form", "3"))
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("Form", "4"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("Form", "A"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("Form", "B"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("Form", "C"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("Form", "D"))
At first i tried to Code within the file that was generated but thanks to User:musicamante, which commented on my Last Question, i threw away that idea.
Now i just load the file like that.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QTableWidgetItem
import sys
from GUI import Ui_Form
class ExampleApp(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(ExampleApp, self).__init__(parent)
self.setupUi(self)
def update_now():
#Store some Measurement Values
WriteDatatoTable()
def WriteDatatoTable():
**Test=Ui_Form()**
**Test.tableWidget.setItem(0,1, QTableWidgetItem("123")**
def main():
app = QApplication(sys.argv)
form = ExampleApp()
timer=QtCore.QTimer()
timer.timeout.connect(update_now)
timer.start(1000)
form.show()
app.exec_()
if __name__ == '__main__':
main()
I just want to print the Data to my GUI Table. But when i tried my approach i get the Error that Ui_Form does not have a attribute called "tableWidget". Besides knowing why that Error occurs, i sadly have no idea how to fix that. Or am i completly wrong with my idea?
I hope anyone can help.
CodePudding user response:
You should make it that your functions are methods of the ExampleApp class and make it the connections of what is about an "UI class" inside that class. This down here should work. Maybe this is not the best way, but this is how I would do it. I hope it helps you.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QTableWidgetItem
import sys
from GUI import Ui_Form
class ExampleApp(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(ExampleApp, self).__init__(parent)
self.setupUi(self)
self.timer = QtCore.QTimer()
self._connect()
self.timer.start(1000)
def _connect(self):
self.timer.timeout.connect(self.update_now)
def update_now(self):
self.writeDataToTable()
def writeDataToTable(self):
self.tableWidget.setItem(0, 1, QTableWidgetItem("123"))
def main():
app = QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()