从pyqt5应用程序中的另一个模块运行时,线程无法正常运行

时间:2019-07-08 08:36:52

标签: python pyqt pyqt5 python-multithreading

您好,我四天前问this问题。下面的问题与我遇到的问题类似,我简化了问题,因此可以轻松地解决问题。

所以我用qt5 designer设计了两个接口。第一个是简单的main.py,中间只有一个按钮,下面是代码:

from PyQt5 import QtCore, QtGui, QtWidgets
from callBack import CallBack

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(538, 453)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(230, 170, 89, 25))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

    self.pushButton.clicked.connect(self.startFunction)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.pushButton.setText(_translate("MainWindow", "click"))

def startFunction(self):
    obj = CallBack()
    obj.show()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

这是callBack_ui.py模块的代码:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(458, 422)
        MainWindow.setStyleSheet("background:white")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setSpacing(30)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
        self.label.setSizePolicy(sizePolicy)
        self.label.setMinimumSize(QtCore.QSize(0, 150))
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())
        self.label_3.setSizePolicy(sizePolicy)
        self.label_3.setMinimumSize(QtCore.QSize(0, 0))
        self.label_3.setStyleSheet("QLabel{\n"
"    border-right:none;\n"
"    border-bottom:1px solid black;\n"
"    border-left:none;\n"
"    border-top:none;\n"
"}")
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setObjectName("label_3")
        self.verticalLayout.addWidget(self.label_3)
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth())
        self.label_2.setSizePolicy(sizePolicy)
        self.label_2.setMinimumSize(QtCore.QSize(0, 40))
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.verticalLayout.addWidget(self.label_2)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
        self.pushButton.setSizePolicy(sizePolicy)
        self.pushButton.setMinimumSize(QtCore.QSize(120, 120))
        self.pushButton.setMaximumSize(QtCore.QSize(120, 120))
        self.pushButton.setStyleSheet("QPushButton{\n"
"    border:0px;\n"
"    background-color:red;\n"
"    border-radius:60px;\n"
"}\n"
"\n"
"QPushButton:pressed{\n"
"    background-color:darkred\n"
"}\n"
"\n"
"")
        self.pushButton.setText("")
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton_2.sizePolicy().hasHeightForWidth())
        self.pushButton_2.setSizePolicy(sizePolicy)
        self.pushButton_2.setMinimumSize(QtCore.QSize(120, 120))
        self.pushButton_2.setMaximumSize(QtCore.QSize(120, 120))
        self.pushButton_2.setStyleSheet("QPushButton{\n"
"    border:0px;\n"
"    background-color:green;\n"
"    border-radius:60px;\n"
"}\n"
"\n"
"QPushButton:pressed{\n"
"    background-color:darkgreen;\n"
"}\n"
"")
        self.pushButton_2.setText("")
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout.addWidget(self.pushButton_2)
        self.verticalLayout_2.addLayout(self.horizontalLayout)
        self.verticalLayout_3.addLayout(self.verticalLayout_2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "contact profile picture"))
        self.label_3.setText(_translate("MainWindow", "contact name   is calling"))
        self.label_2.setText(_translate("MainWindow", "phone number"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

这是callBack.py模块的代码:

import sys
from PyQt5 import QtWidgets,QtGui,QtCore
from PyQt5.QtWidgets import QApplication
from callBack_ui import Ui_MainWindow
from threading import Thread
from playsound import playsound

class CallBack(QtWidgets.QMainWindow,Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        self.pushButton.setIcon(QtGui.QIcon('icons/telephone.png'))
        self.pushButton.setIconSize(QtCore.QSize(75,75))

        self.pushButton_2.setIcon(QtGui.QIcon('icons/call-answer.png'))
        self.pushButton_2.setIconSize(QtCore.QSize(75,75))

        self.label.setPixmap(QtGui.QPixmap('icons/user.png'))

        self.pushButton.clicked.connect(self.greenButtonFunction)
        self.pushButton_2.clicked.connect(self.redButtonFunction)

        self.ringTone = True
        self.t1 = Thread(target = self.playRingingTone)
        self.t1.start()

    def playRingingTone(self):
        while self.ringTone:
            playsound("ring_tone.wav")

    def greenButtonFunction(self):
        print("green button pressed")
        self.ringTone=not self.ringTone
        if self.ringTone:
            self.t1 = Thread(target = self.playRingingTone)
            self.t1.start()

    def redButtonFunction(self):
        print("red buttonn pressed")
        self.ringTone=not self.ringTone
        if self.ringTone:
            self.t1 = Thread(target = self.playRingingTone)
            self.t1.start()




if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = CallBack()
    ex.show()
    sys.exit(app.exec_())

当我单独运行callBack.py模块时,它会按预期运行。在启动时,它会播放音乐,并通过单击绿色或红色按钮中的任何一个来停止播放,并再次单击它会再次开始播放,例如暂停和播放按钮。但是,当我运行main.py应用程序时,它无法正常运行,仅通过单击一下即可关闭CallBack窗口,而不会引发任何错误。如何修复我的代码?

我想我可以用足够的信息来问适当的问题

0 个答案:

没有答案