Answer the question
In order to leave comments, you need to log in
How to open a second window via a button in PyQt5?
Hello, I can not deal with what would seem to be banal.
There are files window_catalogue.py and add_product_to_catalogue.py, you need to open add_product_to_catalogue in the window window through the conditional button "Open a new window".
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTableWidgetItem
import add_product_to_catalogue
# Абсолютный путь временное решевние
sys.path.append('C:/Python project/test')
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1000, 800)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.catalogue_table = QtWidgets.QTableWidget(self.centralwidget)
self.catalogue_table.setObjectName("catalogue_table")
self.catalogue_table.setColumnCount(5)
self.catalogue_table.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
item.setText("Штрих код")
font = QtGui.QFont()
font.setBold(False)
font.setItalic(False)
font.setUnderline(False)
font.setWeight(50)
font.setStrikeOut(False)
font.setKerning(True)
font.setStyleStrategy(QtGui.QFont.NoAntialias)
item.setFont(font)
self.catalogue_table.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.catalogue_table.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.catalogue_table.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.catalogue_table.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.catalogue_table.setHorizontalHeaderItem(4, item)
self.catalogue_table.horizontalHeader().setCascadingSectionResizes(False)
self.verticalLayout.addWidget(self.catalogue_table)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.button_create = QtWidgets.QPushButton(self.centralwidget)
self.button_create.setObjectName("button_create")
self.horizontalLayout.addWidget(self.button_create)
self.button_edit = QtWidgets.QPushButton(self.centralwidget)
self.button_edit.setObjectName("button_edit")
self.horizontalLayout.addWidget(self.button_edit)
self.button_delete = QtWidgets.QPushButton(self.centralwidget)
self.button_delete.setObjectName("button_delete")
self.horizontalLayout.addWidget(self.button_delete)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.verticalLayout.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Каталог товаров"))
item = self.catalogue_table.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Тип товара"))
item = self.catalogue_table.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Название товара"))
item = self.catalogue_table.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Цена закупки"))
item = self.catalogue_table.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "Описание"))
self.button_create.setText(_translate("MainWindow", "Открыть другое окно"))
self.button_edit.setText(_translate("MainWindow", "Редактировать"))
self.button_delete.setText(_translate("MainWindow", "Удалить"))
class Widget(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setupUi(self)
self.output_data_in_table()
self.catalogue_table.resizeColumnsToContents()
# Блокируем возможность редактирования таблицы
self.catalogue_table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.button_delete.clicked.connect(self.delete_row)
self.button_create.clicked.connect(self.open_window)
def output_data_in_table(self):
pass
def delete_row(self):
""" Удаление выделеной строки """
index_row = self.catalogue_table.currentIndex().row()
bar_code = self.catalogue_table.item(index_row, 0).text()
self.catalogue_table.removeRow(index_row)
print('строка удалена')
def open_window(self):
""" Открыть 2 окно """
new_window = add_product_to_catalogue()
new_window.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets, QtCore
class Ui_window_add_to_catalogue(object):
def setupUi(self, window_add_to_catalogue):
""" Этот участок кода удалил, т.к. превышаю количество допустимых символов в форме сайта """
class Window(QtWidgets.QWidget, Ui_window_add_to_catalogue):
""" Наследю класс созданый через qt designer """
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setupUi(self)
self.doublespibox_purchase_price.setButtonSymbols(QtWidgets.QAbstractSpinBox.NoButtons)
self.lineedit_barcode.setInputMask("9999999999")
self.lineedit_product_name.setMaxLength(40)
self.button_create.clicked.connect(self.create)
self.button_clear.clicked.connect(self.clear)
item = ['1', '2', '3', '4', '5', '6', '7', '8']
self.combobox_product_tupe.addItems(item)
def create(self):
barcode = self.lineedit_barcode.text()
name = self.lineedit_product_name.text()
price = self.doublespibox_purchase_price.text()
notes = self.textedit_description.toPlainText()
product_type = self.combobox_product_tupe.currentText()
data = [barcode, product_type, name, price, notes]
print(data)
def clear(self):
""" Востанавливает окно к дефолтному состоянию, сбрасывает все данные введеные пользовательом """
self.lineedit_barcode.clear()
self.lineedit_product_name.clear()
self.doublespibox_purchase_price.setValue(0.00)
self.textedit_description.clear()
self.combobox_product_tupe.setCurrentIndex(0)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
Process finished with exit code -1073740791 (0xC0000409)
Answer the question
In order to leave comments, you need to log in
What technologies are best suited for such a service?The ones you know how to use.
It turned out to be a trite simple mistake, I forgot to specify self in the open_window function!
def open_window(self):
""" Открыть 2 окно """
new_window = add_product_to_catalogue()
new_window.show()
def open_window(self):
""" Открыть 2 окно """
self.new_window = add_product_to_catalogue.Widget()
self.new_window.show()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question