Не работает нажатие кнопки в диалоговым окне

Необходимо чтобы из диалогового окна добавлялись параметры в основное окно, в таблицу QTableWidget, но ничего не работает.
Даже кнопка диалогового окна не работает, подскажите пожалуйста, почему?

main.py:

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QTableWidgetItem
import addobject
import prognozapp
from prognozapp import Ui_PrognozApp
from addobject import *
import sys


class window(QtWidgets.QMainWindow, prognozapp.Ui_PrognozApp):
    def __init__(self):
        super(window, self).__init__()
        self.ui = Ui_PrognozApp()
        self.ui.setupUi(self)

        self.loadProducts()
        self.ui.pushButton.clicked.connect(self.Open_addobject)

        self.ui.tableWidget.doubleClicked.connect (self.doubleClick)

    def doubleClick(self):
        for item in self.ui.tableWidget.selectedItems():
            print(item.row(), item.column(), item.text())

    def loadProducts(self):

        products = []

        self.ui.tableWidget.setRowCount(len(products))
        self.ui.tableWidget.setColumnCount(5)

        self.ui.tableWidget.setHorizontalHeaderLabels(('Brand','Price', 'Example', 'Example', 'Example'))

        self.ui.tableWidget.setColumnWidth(0,50)
        self.ui.tableWidget.setColumnWidth(1,50)


        row_index = 0
        for product in products:
            self.ui.tableWidget.setItem(row_index,0,QTableWidgetItem(str(product['name'])))
            self.ui.tableWidget.setItem(row_index,1,QTableWidgetItem(str(product['price'])))
            self.ui.tableWidget.setItem(row_index, 2, QTableWidgetItem(str(product['example1'])))
            self.ui.tableWidget.setItem(row_index, 3, QTableWidgetItem(str(product['example2'])))
            self.ui.tableWidget.setItem(row_index, 4, QTableWidgetItem(str(product['example3'])))
            row_index += 1

    def Open_addobject(self):
        self.Dialog = QtWidgets.QDialog()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self.Dialog)
        self.Dialog.show()


class TwoWindow(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self):
        super(TwoWindow, self).__init__(self)
        self.ui = Ui_Dialog
        self.ui.setupUi(self)
        self.ui.add.clicked.connect(self.data_save)

    def data_save(self):
        # name = self.ui.lineEdit.text()
        print(5)
        # price = self.ui.lineEdit_2.text()
        # example1 = self.ui.lineEdit_3.text()
        # example2 = self.ui.lineEdit_4.text()
        # example3 = self.ui.lineEdit_5.text()
        #
        # if name and price is not None:
        #     rowCount = self.ui.tableWidget.rowCount()
        #     self.ui.tableWidget.insertRow(rowCount)
        #     self.ui.tableWidget.setItem(rowCount, 0, QTableWidgetItem(name))
        #     self.ui.tableWidget.setItem(rowCount, 1, QTableWidgetItem(price))
        #     self.ui.tableWidget.setItem(rowCount, 2, QTableWidgetItem(example1))
        #     self.ui.tableWidget.setItem(rowCount, 3, QTableWidgetItem(example2))
        #     self.ui.tableWidget.setItem(rowCount, 4, QTableWidgetItem(example3))

def create_app():
    app = QtWidgets.QApplication(sys.argv)
    win = window()
    win.show()
    sys.exit(app.exec_())

create_app()

prognozapp.py:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_PrognozApp(object):
    def setupUi(self, PrognozApp):
        PrognozApp.setObjectName("PrognozApp")
        PrognozApp.resize(914, 624)
        self.centralwidget = QtWidgets.QWidget(parent=PrognozApp)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(parent=self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(50, 50, 361, 211))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(0)
        self.tableWidget.setRowCount(0)
        self.pushButton = QtWidgets.QPushButton(parent=self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(120, 280, 211, 31))
        self.pushButton.setObjectName("pushButton")
        PrognozApp.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(parent=PrognozApp)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 914, 21))
        self.menubar.setObjectName("menubar")
        PrognozApp.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(parent=PrognozApp)
        self.statusbar.setObjectName("statusbar")
        PrognozApp.setStatusBar(self.statusbar)

        self.retranslateUi(PrognozApp)
        QtCore.QMetaObject.connectSlotsByName(PrognozApp)

    def retranslateUi(self, PrognozApp):
        _translate = QtCore.QCoreApplication.translate
        PrognozApp.setWindowTitle(_translate("PrognozApp", "PrognozApp"))
        self.pushButton.setText(_translate("PrognozApp", "Добавить объект"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    PrognozApp = QtWidgets.QMainWindow()
    ui = Ui_PrognozApp()
    ui.setupUi(PrognozApp)
    PrognozApp.show()
    sys.exit(app.exec())

addobject.py:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(427, 480)
        self.lineEdit = QtWidgets.QLineEdit(parent=Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(160, 110, 241, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(parent=Dialog)
        self.lineEdit_2.setGeometry(QtCore.QRect(160, 140, 241, 21))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_3 = QtWidgets.QLineEdit(parent=Dialog)
        self.lineEdit_3.setGeometry(QtCore.QRect(160, 170, 241, 20))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.lineEdit_4 = QtWidgets.QLineEdit(parent=Dialog)
        self.lineEdit_4.setGeometry(QtCore.QRect(160, 200, 241, 20))
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.lineEdit_5 = QtWidgets.QLineEdit(parent=Dialog)
        self.lineEdit_5.setGeometry(QtCore.QRect(160, 230, 241, 20))
        self.lineEdit_5.setObjectName("lineEdit_5")
        self.label = QtWidgets.QLabel(parent=Dialog)
        self.label.setGeometry(QtCore.QRect(100, 110, 47, 13))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(parent=Dialog)
        self.label_2.setGeometry(QtCore.QRect(100, 140, 47, 13))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(parent=Dialog)
        self.label_3.setGeometry(QtCore.QRect(90, 170, 47, 13))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(parent=Dialog)
        self.label_4.setGeometry(QtCore.QRect(90, 200, 47, 13))
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(parent=Dialog)
        self.label_5.setGeometry(QtCore.QRect(90, 230, 47, 13))
        self.label_5.setObjectName("label_5")
        self.add = QtWidgets.QPushButton(parent=Dialog)
        self.add.setGeometry(QtCore.QRect(190, 270, 75, 23))
        self.add.setObjectName("add")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "Name"))
        self.label_2.setText(_translate("Dialog", "Price"))
        self.label_3.setText(_translate("Dialog", "Example"))
        self.label_4.setText(_translate("Dialog", "Example"))
        self.label_5.setText(_translate("Dialog", "Example"))
        self.add.setText(_translate("Dialog", "Добавить"))



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec())

Главное окно

Диалоговое


Ответы (1 шт):

Автор решения: S. Nick

Я подправил ваш код, чтобы он работал правильно. Изменения отмечены по тексту кода. Если вам что-то не понятно - спросите.

main.py

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QTableWidgetItem
#? import addobject
#? import prognozapp
from prognozapp import Ui_PrognozApp                                 # !!!
#? from addobject import *
from addobject import Ui_Dialog                                      # !!!


#class window(QtWidgets.QMainWindow, prognozapp.Ui_PrognozApp):
class MainWindow(QtWidgets.QMainWindow, Ui_PrognozApp):              # !!!
    def __init__(self):
        super(MainWindow, self).__init__()
#        self.ui = Ui_PrognozApp()
#        self.ui.setupUi(self)
        self.setupUi(self)                                          # !!!
        
        self.loadProducts()
        
        self.pushButton.clicked.connect(self.open_addobject)
        self.tableWidget.doubleClicked.connect (self.doubleClick)

    def doubleClick(self):
        for item in self.tableWidget.selectedItems():
            print(item.row(), item.column(), item.text())

    def loadProducts(self):
#        products = []
#        row_index = 0
#        self.tableWidget.setRowCount(len(products))

        self.tableWidget.setColumnCount(5)
        self.tableWidget.setHorizontalHeaderLabels(
            ('Brand','Price', 'Example', 'Example', 'Example'))
        self.tableWidget.setColumnWidth(0, 50)
        self.tableWidget.setColumnWidth(1, 50)
        
# ???   vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        '''
        print(f'products = {products}') #
        for product in products:
            print(f'  product = {product}') #
            self.tableWidget.setItem(row_index, 0, 
                QTableWidgetItem(str(product['name'])))
            self.tableWidget.setItem(row_index, 1, 
                QTableWidgetItem(str(product['price'])))
            self.tableWidget.setItem(row_index, 2, 
                QTableWidgetItem(str(product['example1'])))
            self.tableWidget.setItem(row_index, 3, 
                QTableWidgetItem(str(product['example2'])))
            self.tableWidget.setItem(row_index, 4, 
                QTableWidgetItem(str(product['example3'])))
            row_index += 1
        '''    

    def open_addobject(self):
        '''
        self.Dialog = QtWidgets.QDialog()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self.Dialog)
        self.Dialog.show()
        '''
        self.twoWindow = TwoWindow(self)                             # + (self)
        self.twoWindow.open()
        

class TwoWindow(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent):                                      # + parent
        super(TwoWindow, self).__init__(parent)                      # + parent
#        self.ui = Ui_Dialog
        self.setupUi(self)
        
        self.parent = parent                                         # + parent
        self.add.clicked.connect(self.data_save)

    def data_save(self):
        print(5)
        name = self.lineEdit.text()
        price = self.lineEdit_2.text()
        example1 = self.lineEdit_3.text()
        example2 = self.lineEdit_4.text()
        example3 = self.lineEdit_5.text()
        
        print(f'name={name}; price={price};') #
#        if name and price is not None:
        if name.split() and price.split():
# !!! self.parent      vvvvvvvvvvv <------------------------------------- ...
            rowCount = self.parent.tableWidget.rowCount()
            self.parent.tableWidget.insertRow(rowCount)
            self.parent.tableWidget.setItem(rowCount, 0, QTableWidgetItem(name))
            self.parent.tableWidget.setItem(rowCount, 1, QTableWidgetItem(price))
            self.parent.tableWidget.setItem(rowCount, 2, QTableWidgetItem(example1))
            self.parent.tableWidget.setItem(rowCount, 3, QTableWidgetItem(example2))
            self.parent.tableWidget.setItem(rowCount, 4, QTableWidgetItem(example3))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())

введите сюда описание изображения

введите сюда описание изображения

→ Ссылка