Как объединить 4 диалоговых окна в 1?
У меня есть 4 диалоговых окна, которые просят пользователя ввести данные и этими данными заполняется новая строка в таблице. Вопрос: Как мне их объединить в 1 единое диалоговое окно ?
def add_new_row(self):
window = QWidget()
rowPosition = self.tableWidget.rowCount()
self.tableWidget.insertRow(rowPosition)
text, done = QtWidgets.QInputDialog.getText(window, 'Input Dialog', 'ip :')
text1, done1 = QtWidgets.QInputDialog.getText(window, 'Input Dialog', 'Организация :')
text2, done2 = QtWidgets.QInputDialog.getText(window, 'Input Dialog', 'Улица :')
text3, done3 = QtWidgets.QInputDialog.getText(window, 'Input Dialog', 'Город :')
# text, text1, text2, text3, done = QtWidgets.QInputDialog.getText(window, 'Input Dialog', 'ip :', 'Место :', 'Улица :', 'Город :')
if done and done1 and done2 and done3:
self.tableWidget.setItem(rowPosition , 0, QtWidgets.QTableWidgetItem(text))
self.tableWidget.setItem(rowPosition , 1, QtWidgets.QTableWidgetItem(text1))
self.tableWidget.setItem(rowPosition , 2, QtWidgets.QTableWidgetItem(text2))
self.tableWidget.setItem(rowPosition , 3, QtWidgets.QTableWidgetItem(text3))
else: pass
Ui_Form вызывалась кнопкой, попытался вывести, но из за ошибки - QLayout: Attempting to add QLayout "" to MyWin "Form", which alredy has a layout вернул в изначалный вид.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'setings_ip.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
QInputDialog, QApplication)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import QHeaderView
import sqlite3
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(606, 335)
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.tableWidget = QtWidgets.QTableWidget(Form)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
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.gridLayout.addWidget(self.tableWidget, 1, 0, 1, 3)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout.addWidget(self.pushButton_3, 0, 2, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Добавить"))
self.pushButton.setStyleSheet("QPushButton {\n"
" background-color: #54e346;\n"
"}\n"
"QPushButton:hover {\n"
" background-color: white;\n"
"}\n"
"QPushButton:pressed {\n"
" color: #626AB0;\n"
" background-color: #D5D4D4;\n"
"}")
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("Form", "ip"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("Form", "Место"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("Form", "Улица"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("Form", "Город"))
self.pushButton_2.setText(_translate("Form", "Изменить"))
self.pushButton_2.setStyleSheet("QPushButton {\n"
" background-color: #00b8ef;\n"
"}\n"
"QPushButton:hover {\n"
" background-color: white;\n"
"}\n"
"QPushButton:pressed {\n"
" color: #626AB0;\n"
" background-color: #D5D4D4;\n"
"}")
self.pushButton_3.setText(_translate("Form", "Удалить"))
self.pushButton_3.setStyleSheet("QPushButton {\n"
" background-color: #fa7f72;\n"
"}\n"
"QPushButton:hover {\n"
" background-color: white;\n"
"}\n"
"QPushButton:pressed {\n"
" color: #626AB0;\n"
" background-color: #D5D4D4;\n"
"}")
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
self.tableWidget.horizontalHeader().setMinimumSectionSize(0)
self.row_count = 1
self.table_index = 0
sqlite_connection = sqlite3.connect("New.db")
cursor = sqlite_connection.cursor()
sqlite_select_query = """SELECT ip, Location, Yi, Gor FROM ips"""
cursor.execute(sqlite_select_query)
records = cursor.fetchall()
for row in records:
self.tableWidget.setRowCount(self.row_count)
self.tableWidget.setItem(self.table_index, 0, QtWidgets.QTableWidgetItem(str(row[0])))
self.tableWidget.setItem(self.table_index, 1, QtWidgets.QTableWidgetItem(str(row[1])))
self.tableWidget.setItem(self.table_index, 2, QtWidgets.QTableWidgetItem(str(row[2])))
self.tableWidget.setItem(self.table_index, 3, QtWidgets.QTableWidgetItem(str(row[3])))
self.table_index += 1
self.row_count += 1
cursor.close()
sqlite_connection.close()
self.pushButton.clicked.connect(self.add_new_row)
self.pushButton_2.clicked.connect(self.clear_item)
self.pushButton_3.clicked.connect(self.edit_item)
def add_new_row(self):
window = QWidget()
rowPosition = self.tableWidget.rowCount()
self.tableWidget.insertRow(rowPosition)
text, done = QtWidgets.QInputDialog.getText(window, 'Input Dialog', 'ip :')
text1, done1 = QtWidgets.QInputDialog.getText(window, 'Input Dialog', 'Организация :')
text2, done2 = QtWidgets.QInputDialog.getText(window, 'Input Dialog', 'Улица :')
text3, done3 = QtWidgets.QInputDialog.getText(window, 'Input Dialog', 'Город :')
# text, text1, text2, text3, done = QtWidgets.QInputDialog.getText(window, 'Input Dialog', 'ip :', 'Место :', 'Улица :', 'Город :')
if done and done1 and done2 and done3:
self.tableWidget.setItem(rowPosition , 0, QtWidgets.QTableWidgetItem(text))
self.tableWidget.setItem(rowPosition , 1, QtWidgets.QTableWidgetItem(text1))
self.tableWidget.setItem(rowPosition , 2, QtWidgets.QTableWidgetItem(text2))
self.tableWidget.setItem(rowPosition , 3, QtWidgets.QTableWidgetItem(text3))
else: pass
def clear_item(self):
pass
def edit_item(self):
pass
Вызов
def setings_ip_window(self):
window = setings_ip_window(self)
window.show()
class setings_ip_window(QtWidgets.QWidget):
def __init__(self, parent=MyWin):
super(setings_ip_window, self).__init__(parent, QtCore.Qt.Window)
self.setings_ip = Ui_Form()
self.setings_ip.setupUi(self)
self.setWindowModality(2)
Ответы (2 шт):
Вам нужно сделать свой диалог от QDialog
Пример:
from PyQt5.QtWidgets import QApplication, QDialog, QWidget, QFormLayout, QVBoxLayout, QDialogButtonBox, QLineEdit
class InputCorporateDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Input Dialog')
self.line_edit_ip = QLineEdit()
self.line_edit_corporate = QLineEdit()
self.line_edit_street = QLineEdit()
self.line_edit_city = QLineEdit()
form_layout = QFormLayout()
form_layout.addRow('IP:', self.line_edit_ip)
form_layout.addRow('Организация:', self.line_edit_corporate)
form_layout.addRow('Улица:', self.line_edit_street)
form_layout.addRow('Город:', self.line_edit_city)
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)
main_layout = QVBoxLayout()
main_layout.addLayout(form_layout)
main_layout.addWidget(button_box)
self.setLayout(main_layout)
app = QApplication([])
window = QWidget()
dialog = InputCorporateDialog(window)
if dialog.exec() == QDialog.Accepted:
print(dialog.line_edit_ip.text())
print(dialog.line_edit_corporate.text())
print(dialog.line_edit_street.text())
print(dialog.line_edit_city.text())
Результат:
1123
Рога и Копыта
Кукуева
Т
PS. Вместо QLineEdit можно использовать другие виджеты, это просто пример.
НИКОГДА НЕ ИЗМЕНЯЙТЕ код, сгенерированный Qt Designer - НИКОГДА . Создайте другой класс, который наследуется от соответствующего виджета, и используйте созданный класс для его заполнения.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(606, 335)
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.tableWidget = QtWidgets.QTableWidget(Form)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
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.gridLayout.addWidget(self.tableWidget, 1, 0, 1, 3)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout.addWidget(self.pushButton_3, 0, 2, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Добавить"))
self.pushButton.setStyleSheet("QPushButton {\n"
" background-color: #54e346;\n"
"}\n"
"QPushButton:hover {\n"
" background-color: white;\n"
"}\n"
"QPushButton:pressed {\n"
" color: #626AB0;\n"
" background-color: #D5D4D4;\n"
"}")
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("Form", "ip"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("Form", "Место"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("Form", "Улица"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("Form", "Город"))
self.pushButton_2.setText(_translate("Form", "Изменить"))
self.pushButton_2.setStyleSheet("QPushButton {\n"
" background-color: #00b8ef;\n"
"}\n"
"QPushButton:hover {\n"
" background-color: white;\n"
"}\n"
"QPushButton:pressed {\n"
" color: #626AB0;\n"
" background-color: #D5D4D4;\n"
"}")
self.pushButton_3.setText(_translate("Form", "Удалить"))
self.pushButton_3.setStyleSheet("QPushButton {\n"
" background-color: #fa7f72;\n"
"}\n"
"QPushButton:hover {\n"
" background-color: white;\n"
"}\n"
"QPushButton:pressed {\n"
" color: #626AB0;\n"
" background-color: #D5D4D4;\n"
"}")
class InputCorporateDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle('Input Dialog')
self.line_edit_ip = QLineEdit()
self.line_edit_corporate = QLineEdit()
self.line_edit_street = QLineEdit()
self.line_edit_city = QLineEdit()
form_layout = QFormLayout()
form_layout.addRow('IP:', self.line_edit_ip)
form_layout.addRow('Организация:', self.line_edit_corporate)
form_layout.addRow('Улица:', self.line_edit_street)
form_layout.addRow('Город:', self.line_edit_city)
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)
main_layout = QVBoxLayout()
main_layout.addLayout(form_layout)
main_layout.addWidget(button_box)
self.setLayout(main_layout)
class Widget(QtWidgets.QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
self.tableWidget.horizontalHeader().setMinimumSectionSize(0)
self.pushButton.clicked.connect(self.add_new_row)
self.pushButton_2.clicked.connect(self.clear_item)
self.pushButton_3.clicked.connect(self.edit_item)
# Здесь ваша логика
def add_new_row(self):
inputDialog = InputCorporateDialog()
rez = inputDialog.exec()
if not rez:
msg = QMessageBox.information(self, 'Внимание', 'Диалог сброшен.')
return
text = inputDialog.line_edit_ip.text()
text1 = inputDialog.line_edit_corporate.text()
text2 = inputDialog.line_edit_street.text()
text3 = inputDialog.line_edit_city.text()
rowPosition = self.tableWidget.rowCount()
if text and text1 and text2 and text3:
self.tableWidget.insertRow(rowPosition)
self.tableWidget.setItem(rowPosition , 0, QtWidgets.QTableWidgetItem(text))
self.tableWidget.setItem(rowPosition , 1, QtWidgets.QTableWidgetItem(text1))
self.tableWidget.setItem(rowPosition , 2, QtWidgets.QTableWidgetItem(text2))
self.tableWidget.setItem(rowPosition , 3, QtWidgets.QTableWidgetItem(text3))
else:
msg = QMessageBox.information(self, 'Внимание', 'Заполните все поля.')
def clear_item(self):
pass
def edit_item(self):
pass
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.resize(450, 200)
w.show()
sys.exit(app.exec_())

