Pyqt5 How to Close and Open again Textbrowser when there is new string for append?
-
wrote on 17 Jul 2020, 03:49 last edited by
I'm finding a way of closing previous TextBrowser window and opening new TextBrowser window when it comes new string to display
Below is part of my code
Here is my expectation
if len(result_display) != 0 then pop-up dialog (Ui_Dialog) window called by display_message method in main.py
Keep the opened dialog (Ui_Dialog) window , until new pop-up dialog (Ui_Dialog) window called by display_message
Finally, Previous dialog (Ui_Dialog) window have been closed and kept new dialog (Ui_Dialog) window
But when I run the script, New dialog (Ui_Dialog) window have been pop-up, not closed previous dialog (Ui_Dialog) window.import os import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox from PyQt5.QtCore import pyqtSlot, pyqtSignal import threading from threading import Thread from warning_window import Ui_Dialog class SleepingCellInfo(QWidget): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(700, 450) self.horizontalLayout = QtWidgets.QHBoxLayout(Dialog) self.horizontalLayout.setObjectName("horizontalLayout") self.tb = QtWidgets.QTextBrowser(Dialog) font = QtGui.QFont() font.setFamily("HY궁서") font.setPointSize(9) self.tb.setFont(font) self.tb.setMouseTracking(True) self.tb.setTabletTracking(True) self.tb.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded) self.tb.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByMouse | QtCore.Qt.TextSelectableByMouse) self.tb.setOpenExternalLinks(False) self.tb.setOpenLinks(False) self.tb.setObjectName("textBrowser") self.horizontalLayout.addWidget(self.tb) self.retranslateUi(Dialog) self.tb.anchorClicked['QUrl'].connect(self.draw_chart) QtCore.QMetaObject.connectSlotsByName(Dialog) def display_message(self): global result_display Dialog = QtWidgets.QDialog() ui = Ui_Dialog(result_display) ui.setupUi(Dialog) Dialog.exec_() def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "SLEEPING CELL INFO V5")) def discover_sleeping_cell(self): global order global result_display sql_command_display = "SELECT * From SLEEPING_CELL_DEMO where DATE==" + list_date + " and ROP=='" + list_rop + "'" + " and STATE != 'NORM'" print(sql_command_display) cursor.execute(sql_command_display) result_display = cursor.fetchall() if (len(result_display) != 0): p1 = Thread(target=self.display_message) p1.start() for var in result_display: bla bla bla elif(len(result_display) == 0): bla bla bla cursor.close() conn.close() fp_sleeping_cell.close() # os.remove(target_filename) #else: # pass order = order + 1 if order == 32: sys.exit(app.exec_()) self.wait_10min() def wait_10min(self): global order t = threading.Timer(1.0,self.discover_sleeping_cell) t.start() if __name__ == '__main__': global order app = QtWidgets.QApplication(sys.argv) Dialog = QtWidgets.QDialog() ex = SleepingCellInfo() ex.setupUi(Dialog) Dialog.show() file_list = ['suspect_sleeping_20200704.1200.txt', 'suspect_sleeping_20200704.1215.txt', 'suspect_sleeping_20200704.1230.txt', 'suspect_sleeping_20200704.1245.txt', 'suspect_sleeping_20200704.1300.txt', 'suspect_sleeping_20200704.1315.txt', 'suspect_sleeping_20200704.1330.txt', 'suspect_sleeping_20200704.1345.txt', 'suspect_sleeping_20200705.1200.txt', 'suspect_sleeping_20200705.1215.txt', 'suspect_sleeping_20200705.1230.txt', 'suspect_sleeping_20200705.1245.txt', 'suspect_sleeping_20200705.1300.txt', 'suspect_sleeping_20200705.1315.txt', 'suspect_sleeping_20200705.1330.txt', 'suspect_sleeping_20200705.1345.txt', 'suspect_sleeping_20200706.1200.txt', 'suspect_sleeping_20200706.1215.txt', 'suspect_sleeping_20200706.1230.txt', 'suspect_sleeping_20200706.1245.txt', 'suspect_sleeping_20200706.1300.txt', 'suspect_sleeping_20200706.1315.txt', 'suspect_sleeping_20200706.1330.txt', 'suspect_sleeping_20200706.1345.txt', 'suspect_sleeping_20200707.1200.txt', 'suspect_sleeping_20200707.1215.txt', 'suspect_sleeping_20200707.1230.txt', 'suspect_sleeping_20200707.1245.txt', 'suspect_sleeping_20200707.1300.txt', 'suspect_sleeping_20200707.1315.txt', 'suspect_sleeping_20200707.1330.txt', 'suspect_sleeping_20200707.1345.txt'] order = 0 ex.wait_10min() sys.exit(app.exec_())
warning_window.py
from PyQt5 import QtCore, QtGui, QtWidgets import sys class Ui_Dialog(object): def __init__(self,msg): global list_of_sleeping_cell list_of_sleeping_cell = msg print(list_of_sleeping_cell) def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(768, 794) self.horizontalLayout = QtWidgets.QHBoxLayout(Dialog) self.horizontalLayout.setObjectName("horizontalLayout") self.warn_sleeping = QtWidgets.QTextBrowser(Dialog) self.warn_sleeping.setFrameShape(QtWidgets.QFrame.StyledPanel) self.warn_sleeping.setFrameShadow(QtWidgets.QFrame.Plain) self.warn_sleeping.setLineWidth(3) self.warn_sleeping.setMidLineWidth(1) self.warn_sleeping.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop) self.warn_sleeping.setObjectName("warn_sleeping") self.horizontalLayout.addWidget(self.warn_sleeping) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) ################################################ lenth_sleeping_cell_list = len(list_of_sleeping_cell) self.warn_sleeping.setText("bla~~~~~~~~~") for info in list_of_sleeping_cell: date = info[0] time = info[1] eNB = info[2] cell = info[3] status = info[10] self.warn_sleeping.append("\n") string_date = "DATE :" + str(date) self.warn_sleeping.append(string_date) self.warn_sleeping.append("TIME :" + str(time)) self.warn_sleeping.append("eNB :" + str(eNB)) self.warn_sleeping.append("DATE :" + str(cell)) self.warn_sleeping.append("STATUS :" + str(status)) self.warn_sleeping.append("\n") print (list_of_sleeping_cell) ################################################ def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.warn_sleeping.setText(_translate("Dialog", "TextLabel")) 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_())
-
I'm finding a way of closing previous TextBrowser window and opening new TextBrowser window when it comes new string to display
Below is part of my code
Here is my expectation
if len(result_display) != 0 then pop-up dialog (Ui_Dialog) window called by display_message method in main.py
Keep the opened dialog (Ui_Dialog) window , until new pop-up dialog (Ui_Dialog) window called by display_message
Finally, Previous dialog (Ui_Dialog) window have been closed and kept new dialog (Ui_Dialog) window
But when I run the script, New dialog (Ui_Dialog) window have been pop-up, not closed previous dialog (Ui_Dialog) window.import os import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox from PyQt5.QtCore import pyqtSlot, pyqtSignal import threading from threading import Thread from warning_window import Ui_Dialog class SleepingCellInfo(QWidget): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(700, 450) self.horizontalLayout = QtWidgets.QHBoxLayout(Dialog) self.horizontalLayout.setObjectName("horizontalLayout") self.tb = QtWidgets.QTextBrowser(Dialog) font = QtGui.QFont() font.setFamily("HY궁서") font.setPointSize(9) self.tb.setFont(font) self.tb.setMouseTracking(True) self.tb.setTabletTracking(True) self.tb.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded) self.tb.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByMouse | QtCore.Qt.TextSelectableByMouse) self.tb.setOpenExternalLinks(False) self.tb.setOpenLinks(False) self.tb.setObjectName("textBrowser") self.horizontalLayout.addWidget(self.tb) self.retranslateUi(Dialog) self.tb.anchorClicked['QUrl'].connect(self.draw_chart) QtCore.QMetaObject.connectSlotsByName(Dialog) def display_message(self): global result_display Dialog = QtWidgets.QDialog() ui = Ui_Dialog(result_display) ui.setupUi(Dialog) Dialog.exec_() def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "SLEEPING CELL INFO V5")) def discover_sleeping_cell(self): global order global result_display sql_command_display = "SELECT * From SLEEPING_CELL_DEMO where DATE==" + list_date + " and ROP=='" + list_rop + "'" + " and STATE != 'NORM'" print(sql_command_display) cursor.execute(sql_command_display) result_display = cursor.fetchall() if (len(result_display) != 0): p1 = Thread(target=self.display_message) p1.start() for var in result_display: bla bla bla elif(len(result_display) == 0): bla bla bla cursor.close() conn.close() fp_sleeping_cell.close() # os.remove(target_filename) #else: # pass order = order + 1 if order == 32: sys.exit(app.exec_()) self.wait_10min() def wait_10min(self): global order t = threading.Timer(1.0,self.discover_sleeping_cell) t.start() if __name__ == '__main__': global order app = QtWidgets.QApplication(sys.argv) Dialog = QtWidgets.QDialog() ex = SleepingCellInfo() ex.setupUi(Dialog) Dialog.show() file_list = ['suspect_sleeping_20200704.1200.txt', 'suspect_sleeping_20200704.1215.txt', 'suspect_sleeping_20200704.1230.txt', 'suspect_sleeping_20200704.1245.txt', 'suspect_sleeping_20200704.1300.txt', 'suspect_sleeping_20200704.1315.txt', 'suspect_sleeping_20200704.1330.txt', 'suspect_sleeping_20200704.1345.txt', 'suspect_sleeping_20200705.1200.txt', 'suspect_sleeping_20200705.1215.txt', 'suspect_sleeping_20200705.1230.txt', 'suspect_sleeping_20200705.1245.txt', 'suspect_sleeping_20200705.1300.txt', 'suspect_sleeping_20200705.1315.txt', 'suspect_sleeping_20200705.1330.txt', 'suspect_sleeping_20200705.1345.txt', 'suspect_sleeping_20200706.1200.txt', 'suspect_sleeping_20200706.1215.txt', 'suspect_sleeping_20200706.1230.txt', 'suspect_sleeping_20200706.1245.txt', 'suspect_sleeping_20200706.1300.txt', 'suspect_sleeping_20200706.1315.txt', 'suspect_sleeping_20200706.1330.txt', 'suspect_sleeping_20200706.1345.txt', 'suspect_sleeping_20200707.1200.txt', 'suspect_sleeping_20200707.1215.txt', 'suspect_sleeping_20200707.1230.txt', 'suspect_sleeping_20200707.1245.txt', 'suspect_sleeping_20200707.1300.txt', 'suspect_sleeping_20200707.1315.txt', 'suspect_sleeping_20200707.1330.txt', 'suspect_sleeping_20200707.1345.txt'] order = 0 ex.wait_10min() sys.exit(app.exec_())
warning_window.py
from PyQt5 import QtCore, QtGui, QtWidgets import sys class Ui_Dialog(object): def __init__(self,msg): global list_of_sleeping_cell list_of_sleeping_cell = msg print(list_of_sleeping_cell) def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(768, 794) self.horizontalLayout = QtWidgets.QHBoxLayout(Dialog) self.horizontalLayout.setObjectName("horizontalLayout") self.warn_sleeping = QtWidgets.QTextBrowser(Dialog) self.warn_sleeping.setFrameShape(QtWidgets.QFrame.StyledPanel) self.warn_sleeping.setFrameShadow(QtWidgets.QFrame.Plain) self.warn_sleeping.setLineWidth(3) self.warn_sleeping.setMidLineWidth(1) self.warn_sleeping.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop) self.warn_sleeping.setObjectName("warn_sleeping") self.horizontalLayout.addWidget(self.warn_sleeping) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) ################################################ lenth_sleeping_cell_list = len(list_of_sleeping_cell) self.warn_sleeping.setText("bla~~~~~~~~~") for info in list_of_sleeping_cell: date = info[0] time = info[1] eNB = info[2] cell = info[3] status = info[10] self.warn_sleeping.append("\n") string_date = "DATE :" + str(date) self.warn_sleeping.append(string_date) self.warn_sleeping.append("TIME :" + str(time)) self.warn_sleeping.append("eNB :" + str(eNB)) self.warn_sleeping.append("DATE :" + str(cell)) self.warn_sleeping.append("STATUS :" + str(status)) self.warn_sleeping.append("\n") print (list_of_sleeping_cell) ################################################ def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.warn_sleeping.setText(_translate("Dialog", "TextLabel")) 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_())
@Junhi said in Pyqt5 How to Close and Open again Textbrowser when there is new string for append?:
Dialog = QtWidgets.QDialog()
You should make Dialog a class member. Then you can close it before starting new one.
if self.Dialog: self.Dialog.close() self.Dialog = QtWidgets.QDialog()
-
@Junhi said in Pyqt5 How to Close and Open again Textbrowser when there is new string for append?:
Dialog = QtWidgets.QDialog()
You should make Dialog a class member. Then you can close it before starting new one.
if self.Dialog: self.Dialog.close() self.Dialog = QtWidgets.QDialog()
wrote on 17 Jul 2020, 05:33 last edited by@jsulm Thank you for your interest. But I cannot understand what should I do.
As with your reply and my decision, I edit warning_window.py as belowdef setupUi(self, Dialog):
if self.Dialog: self.Dialog.close()
==> Add these 2 lines in setupUi
def Dialog(self): self.Dialog = QtWidgets.QDialog()
==> make Dialog as a member of Ui_Dialog
When I run this , error occurs below
Exception in thread Thread-42:
Traceback (most recent call last):
File "C:\Users\think\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\think\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/think/PycharmProjects/network_monitor/crte_sleepingcell_db_5.py", line 45, in display_message
ui.setupUi(Dialog)
File "C:\Users\think\PycharmProjects\network_monitor\warning_window.py", line 25, in setupUi
self.Dialog.close()
AttributeError: 'function' object has no attribute 'close' -
@jsulm Thank you for your interest. But I cannot understand what should I do.
As with your reply and my decision, I edit warning_window.py as belowdef setupUi(self, Dialog):
if self.Dialog: self.Dialog.close()
==> Add these 2 lines in setupUi
def Dialog(self): self.Dialog = QtWidgets.QDialog()
==> make Dialog as a member of Ui_Dialog
When I run this , error occurs below
Exception in thread Thread-42:
Traceback (most recent call last):
File "C:\Users\think\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\think\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/think/PycharmProjects/network_monitor/crte_sleepingcell_db_5.py", line 45, in display_message
ui.setupUi(Dialog)
File "C:\Users\think\PycharmProjects\network_monitor\warning_window.py", line 25, in setupUi
self.Dialog.close()
AttributeError: 'function' object has no attribute 'close'@Junhi You have name clash, just rename the variable from self.Dialog to, for example, self.__dialog.
By the way upper case variable names like indef setupUi(self, Dialog):
are bad idea as usually classes are named like this.
-
@Junhi You have name clash, just rename the variable from self.Dialog to, for example, self.__dialog.
By the way upper case variable names like indef setupUi(self, Dialog):
are bad idea as usually classes are named like this.
wrote on 17 Jul 2020, 07:49 last edited by@jsulm Sorry for bothering, but result is same as before. (AttributeError: 'function' object has no attribute 'close')
Following is my job history
- Edit setupUi in warning_window.py like :
def setupUi(self, dialog): if self._dialog: self._dialog.close()
- Define _dialog method in the Ui_Dialog class
def _dialog(self): self._dialog = QtWidgets.QDialog()
-
@jsulm Sorry for bothering, but result is same as before. (AttributeError: 'function' object has no attribute 'close')
Following is my job history
- Edit setupUi in warning_window.py like :
def setupUi(self, dialog): if self._dialog: self._dialog.close()
- Define _dialog method in the Ui_Dialog class
def _dialog(self): self._dialog = QtWidgets.QDialog()
@Junhi said in Pyqt5 How to Close and Open again Textbrowser when there is new string for append?:
Define _dialog method in the Ui_Dialog class
def _dialog(self): self._dialog = QtWidgets.QDialog()
Sorry, but what is this?!
Now you define a method called _dialog and try to assign to it.
I was talking about this:def display_message(self): global result_display if self.__dialog: self.__dialog.close() self.__dialog = QtWidgets.QDialog() ui = Ui_Dialog(result_display) ui.setupUi(self.__dialog) self.__dialog.exec_()
-
@Junhi said in Pyqt5 How to Close and Open again Textbrowser when there is new string for append?:
Define _dialog method in the Ui_Dialog class
def _dialog(self): self._dialog = QtWidgets.QDialog()
Sorry, but what is this?!
Now you define a method called _dialog and try to assign to it.
I was talking about this:def display_message(self): global result_display if self.__dialog: self.__dialog.close() self.__dialog = QtWidgets.QDialog() ui = Ui_Dialog(result_display) ui.setupUi(self.__dialog) self.__dialog.exec_()
wrote on 17 Jul 2020, 08:36 last edited by@jsulm The dialog for close is from warning_window.py
your code is for closing dialog in the main.py(SleepingCellInfo) class.
Your code makes errors
Exception in thread Thread-42:
Traceback (most recent call last):
File "C:\Users\think\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\think\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/think/PycharmProjects/network_monitor/crte_sleepingcell_db_5.py", line 43, in display_message
if self.__dialog:
AttributeError: 'SleepingCellInfo' object has no attribute '_SleepingCellInfo__dialog'I want my code to act
- When if (len(result_display) != 0): is true,
pop-up dialog window called from warning_window.py (Ui_Dialog class) - if (len(result_display) != 0): is true again,
Close previous dialog window and pop-up new one.
Because display_message(self): method is part of main.py, self.__dialog in your code became SleepingCellInfo__dialog.
My wish is to open or close Ui_Dialog.__dialog.
- When if (len(result_display) != 0): is true,
1/7