如何多次打开同一个对话框?

时间:2019-08-07 01:46:00

标签: python pyqt5

现在我要显示几组数据,但是我只有一个主窗口和一个对话框。这意味着我想多次打开同一对话框来显示这些数据,而每个对话框每次只显示一组数据。最后,我想看到几个对话框打开,每个对话框都显示一组数据。在我的代码中,您可以看到该对话框打开了几次,每次新对话框都打开,最后一个对话框关闭,最后只有一个对话框打开。如何重新更正我的代码?

main.py

from PyQt5 import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
import time
from alert import Show_Text

class Ui_Control_system(object):



    def setupUi(self, Control_system):

        Control_system.setObjectName("Control_system")
        Control_system.resize(1004, 722)
        Control_system.setFixedSize(1004, 722)

        Control_system.setWindowIcon(QIcon('images/icon.png'))
        Control_system.setStyleSheet("QMainWindow{border-image:url(images/background.jpg)}")

        self.centralwidget = QWidget(Control_system)
        self.centralwidget.setObjectName("centralwidget")

        #表格显示
        self.tableWidget = QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QRect(130, 170, 752, 245))
        self.tableWidget.setRowCount(8)
        self.tableWidget.setColumnCount(6)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.horizontalHeader().setVisible(False)
        self.tableWidget.horizontalHeader().setCascadingSectionResizes(False)
        self.tableWidget.horizontalHeader().setDefaultSectionSize(125)
        self.tableWidget.horizontalHeader().setHighlightSections(True)
        self.tableWidget.horizontalHeader().setSortIndicatorShown(False)
        self.tableWidget.verticalHeader().setVisible(False)
        Control_system.setCentralWidget(self.centralwidget)
        self.tableWidget.setAutoFillBackground(True)

        self.tableWidget.setItem(0,0,QTableWidgetItem("机器编号"))
        self.tableWidget.setItem(0,1,QTableWidgetItem("烟雾值"))
        self.tableWidget.setItem(0,2,QTableWidgetItem("火焰值"))
        self.tableWidget.setItem(0,3, QTableWidgetItem("温度值"))
        self.tableWidget.setItem(0,4, QTableWidgetItem("电流值"))
        self.tableWidget.setItem(0,5, QTableWidgetItem("电压值"))
        font = QFont()
        font.setPointSize(15)
        font.setBold(True)
        self.tableWidget.item(0,0).setFont(font)
        self.tableWidget.item(0,1).setFont(font)
        self.tableWidget.item(0,2).setFont(font)
        self.tableWidget.item(0,3).setFont(font)
        self.tableWidget.item(0,4).setFont(font)
        self.tableWidget.item(0,5).setFont(font)

        self.label = QLabel(self.centralwidget)
        self.label.setGeometry(QRect(360, 70, 300, 50))
        self.label.setObjectName("label")
        self.label.setAutoFillBackground(True)
        self.label.setAlignment(Qt.AlignCenter)   
        self.label.setStyleSheet("border-image:url(images/title.png)")

        self.pushButton = QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QRect(560, 480, 121, 41))
        self.pushButton.setObjectName("pushButton")

        self.pushButton_2 = QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QRect(730, 480, 121, 41))
        self.pushButton_2.setObjectName("pushButton_2")

        self.pushButton_3 = QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QRect(70, 60, 121, 41))
        self.pushButton_3.setObjectName("pushButton_3")

        Control_system.setCentralWidget(self.centralwidget)
        self.statusbar = QStatusBar(Control_system)
        self.statusbar.setObjectName("statusbar")
        Control_system.setStatusBar(self.statusbar)


        self.retranslateUi(Control_system)
        QMetaObject.connectSlotsByName(Control_system)


    def retranslateUi(self, Control_system):
        _translate = QCoreApplication.translate
        Control_system.setWindowTitle(_translate("Control_system", "实时监控系统"))
        self.pushButton.setText(_translate("Control_system", "开始运行"))
        self.pushButton_2.setText(_translate("Control_system", "停止"))
        self.pushButton_3.setText(_translate("Control_system", "跳转"))





class Control_system(QMainWindow, Ui_Control_system):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.pushButton_3.clicked.connect(self.jump_to_alert)


    def goAlert(self):                                                  
        self.alert = Alert()
        self.alert.show()      


class Alert(Show_Text):

    def __init__(self):

        super().__init__()
        self.setupUi(self)


    def closeEvent(self, event):

        reply = QMessageBox.question(self,     '警告',
                                               "是否要退出警告?",
                                               QMessageBox.Yes | QMessageBox.No,
                                               QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


class PopupWindow(QThread): 

    popup = pyqtSignal()

    def __init__(self, parent=None):

        super(PopupWindow, self).__init__()
        self.index = 0

    def run(self):

        while self.index <= 3:
            self.index +=1
            self.popup.emit()
            time.sleep(1)

    def stop(self):  
        self.quit()
        self.wait()



if __name__ == '__main__':

    if not QApplication.instance():
        app = QApplication(sys.argv)
    else:
        app = QApplication.instance() 

    w = Control_system()

    w.show() 
    sys.exit(app.exec())

alert.py

from PyQt5 import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import threading
import time

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(557, 444)
        #Dialog.setStyleSheet("QDialog{border-image:url(images/background.jpg)}")
        Dialog.setWindowIcon(QIcon('images/icon.png'))
        self.textBrowser = QtWidgets.QTextBrowser(Dialog)
        self.textBrowser.setGeometry(QtCore.QRect(200, 80, 256, 41))
        self.textBrowser.setObjectName("textBrowser")
        self.textBrowser_2 = QtWidgets.QTextBrowser(Dialog)
        self.textBrowser_2.setGeometry(QtCore.QRect(200, 170, 256, 41))
        self.textBrowser_2.setObjectName("textBrowser_2")
        self.textBrowser_3 = QtWidgets.QTextBrowser(Dialog)
        self.textBrowser_3.setGeometry(QtCore.QRect(200, 260, 256, 41))
        self.textBrowser_3.setObjectName("textBrowser_3")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(60, 80, 111, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setStyleSheet("color: rgb(255, 0, 0);")
        self.label.setTextFormat(QtCore.Qt.AutoText)
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(60, 170, 111, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setStyleSheet("color: rgb(255, 0, 0);")
        self.label_2.setTextFormat(QtCore.Qt.AutoText)
        self.label_2.setObjectName("label_2")
        self.alert = QtWidgets.QLabel(Dialog)
        self.alert.setGeometry(QtCore.QRect(60, 260, 111, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.alert.setFont(font)
        self.alert.setStyleSheet("color: rgb(255, 0, 0);")
        self.alert.setTextFormat(QtCore.Qt.AutoText)
        self.alert.setObjectName("alert")

        self.retranslateUi(Dialog)
        #QtCore.QMetaObject.connectSlotsByName(Dialog)
        #self.recieveAlarm(Dialog)
        #timer = QtCore.QTimer(self, interval=1000, timeout=self.recieveAlarm)
        #timer.start()

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "警告"))
        self.label.setText(_translate("Dialog", "报警车间"))
        self.label_2.setText(_translate("Dialog", "报警机器"))
        self.alert.setText(_translate("Dialog", "报警项目"))




class Show_Text(QDialog, Ui_Dialog):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.show_text_thread = showtext_Thread()
        self.show_text_thread.trigger.connect(self.show_Data)
        self.show_text_thread.start()

    @pyqtSlot(str, str, str)
    def show_Data(self, room, machine, project):#数据更新    
        self.textBrowser.append(room)   
        self.textBrowser_2.append(machine)
        self.textBrowser_3.append(project)  

class showtext_Thread(QtCore.QThread):
    trigger = QtCore.pyqtSignal(str,str,str)

    def __init__(self, parent=None):
        super(showtext_Thread, self).__init__(parent)

    def run(self):

        self.trigger.emit("2","3","4")    
        time.sleep(1)

    def stop(self):
        self.quit()
        self.wait()

0 个答案:

没有答案