Reputation: 424
Once button clicked, need to display in QTextEdit() in the main window : "Added"
Try to import from the second window the first and : QTextEdit().append("added"). But the first window is already imported in the second then I get an error message (Second window importing itself...)
That is the Main window :
from PyQt5.QtWidgets import *
import sys
from App.A_Client import NewClient
class Crm(QWidget):
def __init__(self, parent=None):
super(Crm, self).__init__()
self.setGeometry(50, 50, 400, 100)
self.setWindowTitle("CRM TEST")
self.grid = QGridLayout()
self.grid.setSpacing(10)
self.setLayout(self.grid)
self.home()
def home(self):
self.message_box = QTextEdit()
self.message_box.setReadOnly(True)
self.grid.addWidget(self.message_box, 3, 0, 5, 3)
# NEED TO APPEND THE "ADDED" IN SELF.MESSAGE_BOX FROM CLICK IN THE SECOND WINDOW
self.dialogs = list()
self.show()
def btn1_on(self):
dialog = NewClient(self)
self.dialogs.append(dialog)
dialog.show()
if __name__ == "__main__":
app = QApplication([])
launch = Crm()
app.exec_()
There is the second :
from PyQt5.QtWidgets import *
from App.App_main import Crm
class NewClient(QWidget):
def __init__(self, parent = None):
super(NewClient, self).__init__()
self.setWindowTitle("ADD CLIENT")
self.grid = QGridLayout()
self.setLayout(self.grid)
self.home()
def home(self):
self.btn_client_add = QPushButton("Add", self)
self.grid.addWidget(self.btn_client_add, 12, 0, 12, 3)
self.btn_client_add.clicked.connect(self.btn_add_on)
def btn_add_on(self):
if self.check_client_option_news_letter.isChecked():
news_letter = "True"
else:
news_letter = "False"
if self.check_client_option_other.isChecked():
option = "True"
else:
option = "False"
choice = QMessageBox.question(self, "Execute", "Do you want to add this client ?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if choice == QMessageBox.Yes:
Crm.message_box.append("Added") # THERE IS WHEN I WOULD LIKE TO APPEND THE "ADDED"
else:
pass
Upvotes: 1
Views: 67
Reputation: 244132
You have a circular import, since file A imports B and B imports A. One way to avoid this is to use the signals next to the Single responsibility principle
: each class must have a set of tasks, in this case emit through a signal the text to the other window.
App_main.py
# ...
def btn1_on(self):
dialog = NewClient(self)
dialog.sendTextSignal.connect(self.message_box.append) # connect signal to slot
self.dialogs.append(dialog)
dialog.show()
# ...
A_Client.py
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSignal
# It is not necessary to import Crm
# from App.App_main import Crm
class NewClient(QWidget):
sendTextSignal = pyqtSignal(str) # create signal
# ...
def btn_add_on(self):
# ...
choice = QMessageBox.question(
self,
"Execute",
"Do you want to add this client ?",
QMessageBox.Yes | QMessageBox.No,
QMessageBox.Yes,
)
if choice == QMessageBox.Yes:
self.sendTextSignal.emit("Added") # emit signal
else:
pass
# ...
As you see in the class NewClient does not know Crm, it only emits the signal, and then in Crm the signal is connected to the append slot of the QTextEdit, in this case it is necessary that Crm knows NewClient.
Upvotes: 1