L
L
LVitA2017-03-19 12:39:24
Python
LVitA, 2017-03-19 12:39:24

How to close PyQT5 window?

Good day! I encountered such a problem, after closing the window and reopening it, all the functions of the class, when they are called, start to falsely work several times, and the number of false positives is equal to the number of closing the window (i.e. 2 times the window was closed, then after opening the functions will work falsely 2 times). Help solve the problem or tell me what I'm doing wrong!
here are 2 classes that implement 2 windows:

from PyQt5 import QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

from basedata import addCategory, getCategory, addProduct, getProduct
from structs import ProductStruct


class ProductList(QWidget):
    def __init__(self):
        super().__init__()
        self.s = []
        self.add_window = AddProduct(self)
        self.exit = QPushButton('Выход')
        self.new_product = QPushButton('Добавить товар')
        self.supplier = QLabel()
        self.lv = QListView()
        self.table = QTableView()
        self.sti = QStandardItemModel(parent=self.lv)
        self.t = QStandardItemModel(parent=self.table)
        self.grid = QGridLayout()
        self.split = QSplitter(Qt.Horizontal)
        self.hbox1 = QHBoxLayout()
        self.hbox2 = QHBoxLayout()
        self.vbox = QVBoxLayout()

    def closeEvent(self, event):
        self.sti.clear()
        self.t.clear()
        if event:
            event.accept()
        else:
            self.close()

    def on_click(self, s):
        self.t.clear()
        self.t = getProduct(self.s, s.indexes()[0].data())
        self.t.setHorizontalHeaderLabels(['Название', 'Описание', 'Цена'])
        self.table.setModel(self.t)

    def view_add_window(self):
        self.add_window.view()

    def view(self, supplier):
        self.setGeometry(400, 100, 635, 300)
        self.setWindowIcon(QIcon('./Icon/edit1'))
        self.setWindowTitle('Постовляемый товар')
        self.setWindowModality(Qt.ApplicationModal)

        self.s = supplier
        self.supplier.setText('Поставщик ' + supplier)
        self.supplier.setFont(QFont('Calibry', 12))
        self.supplier.setAlignment(QtCore.Qt.AlignHCenter)

        self.new_product.clicked.connect(self.add_window.view)
        self.exit.clicked.connect(self.close)

        arr = getCategory()

        if arr != '':
            self.sti = arr

        self.split.addWidget(self.lv)
        self.split.addWidget(self.table)

        self.grid.addWidget(self.supplier, 0, 1)
        self.grid.addWidget(self.split, 1, 0, 1, 3)
        self.grid.addWidget(self.new_product, 2, 0)
        self.grid.addWidget(self.exit, 2, 2)

        self.t.setHorizontalHeaderLabels(['Название', 'Описание', 'Цена'])

        self.table.setModel(self.t)
        self.lv.setModel(self.sti)
        self.lv.selectionModel().selectionChanged.connect(self.on_click)
        self.setLayout(self.grid)
        self.show()


class AddProduct(QWidget):
    def __init__(self, w):
        super().__init__()
        self.window = w
        self.add = QPushButton('Добавить')
        self.add_category = QPushButton('Добавить категорию')
        self.cancel = QPushButton('Отмена')
        self.name = QLabel('Название')
        self.name_text = QLineEdit()
        self.characteristic = QLabel('Характеристика')
        self.characteristic_text = QTextEdit()
        self.price = QLabel('Цена (руб.)')
        self.price_text = QLineEdit()
        self.category = QLabel('Категории')
        self.category_list = QComboBox()
        self.category_item_model = QStandardItemModel(parent=self.category_list)
        self.grid = QGridLayout()

    def closeEvent(self, event):
        if event:
            self.clearFields()
            event.accept()
        else:
            self.clearFields()
            self.close()

    def addCategory(self):
        category, ok = QInputDialog.getText(self, 'Добавление категории',
                                            'Введите категорию')
        if ok:
            self.category_list.addItem(category)
            addCategory(category)
            item = QStandardItem(category)
            self.window.sti.appendRow(item)

    def clearFields(self):
        self.category_list.clear()
        self.name_text.clear()
        self.characteristic_text.clear()
        self.price_text.clear()

    def addProduct(self):
        ok = QMessageBox.question(self, 'Внимание!', 'Вы уверены, что хотите добавить данный товар?',
                                     QMessageBox.No | QMessageBox.Yes)
        if ok == QMessageBox.Yes:
            p = ProductStruct(self.category_list.currentText(), self.window.s, self.name_text.text(),
                              self.characteristic_text.toPlainText(), self.price_text.text())
            print(self.category_list.currentText())
            addProduct(p)
            self.closeEvent()

    def view(self):
        self.setGeometry(400, 100, 500, 300)
        self.setWindowIcon(QIcon('./Icon/edit1'))
        self.setWindowTitle('Новый товар')
        self.setWindowModality(Qt.ApplicationModal)

        self.add_category.clicked.connect(self.addCategory)
        self.add.clicked.connect(self.addProduct)
        self.cancel.clicked.connect(self.closeEvent)

        arr = getCategory()

        if arr != '':
            self.category_item_model = arr

        self.grid.addWidget(self.category, 0, 0)
        self.grid.addWidget(self.category_list, 0, 1)
        self.grid.addWidget(self.add_category, 0, 2)
        self.grid.addWidget(self.name, 2, 0)
        self.grid.addWidget(self.name_text, 2, 1, 1, 2)
        self.grid.addWidget(self.characteristic, 3, 0)
        self.grid.addWidget(self.characteristic_text, 3, 1, 3, 2)
        self.grid.addWidget(self.price, 6, 0)
        self.grid.addWidget(self.price_text, 6, 1, 1, 2)
        self.grid.addWidget(self.add, 8, 0)
        self.grid.addWidget(self.cancel, 8, 2)

        self.category_list.setModel(self.category_item_model)

        self.setLayout(self.grid)
        self.show()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andy_U, 2017-03-19
@LVitA

Well, then the answer is:
Move all code (except self.show()) from ViewWindow.view_window() to ViewWindow.__init()___.
Every time you open a window, you bind the signal from the button again and again to the on_click () method. This procedure is called several times.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question