После закрытия окна PyQt5 музыка не перестает играть
import sys, os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtCore import QTimer, QUrl
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent, QMediaPlaylist
from openpyxl import load_workbook
class Ui_cityquestion1(object):
def setupUi(self, cityquestion1):
cityquestion1.setObjectName("cityquestion1")
cityquestion1.resize(1223, 800)
cityquestion1.setStyleSheet("background-color: rgb(255, 255, 255);")
self.task = QtWidgets.QLabel(cityquestion1)
self.task.setGeometry(QtCore.QRect(0, 0, 1231, 81))
self.task.setStyleSheet("color: #000;\n"
"font: 24pt \"MS Shell Dlg 2\";\n"
"border: none;\n"
"color: white;\n"
"background-color: rgb(214, 146, 255);")
self.task.setAlignment(QtCore.Qt.AlignCenter)
self.task.setObjectName("task")
self.answer1 = QtWidgets.QPushButton(cityquestion1)
self.answer1.setGeometry(QtCore.QRect(240, 520, 291, 81))
font = QtGui.QFont()
font.setFamily("MS Shell Dlg 2")
font.setPointSize(24)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.answer1.setFont(font)
self.answer1.setStyleSheet("QPushButton{\n"
"border: none;\n"
"color: rgb(0, 0, 0);\n"
"font: 24pt \"MS Shell Dlg 2\";\n"
"border: none;\n"
"color: white;\n"
"background-color: rgb(112, 107, 255);\n"
"border-radius: 10px\n"
"}\n"
"\n"
"QPushButton:hover{\n"
"border: none;\n"
"color: rgb(0, 0, 0);\n"
"font: 24pt \"MS Shell Dlg 2\";\n"
"border: none;\n"
"color: white;\n"
"background-color: #00FFFF;\n"
"border-radius: 10px\n"
"}\n")
self.answer1.setObjectName("answer1")
self.answer2 = QtWidgets.QPushButton(cityquestion1)
self.answer2.setGeometry(QtCore.QRect(690, 520, 291, 81))
font = QtGui.QFont()
font.setFamily("MS Shell Dlg 2")
font.setPointSize(24)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.answer2.setFont(font)
self.answer2.setStyleSheet("QPushButton{\n"
"border: none;\n"
"color: rgb(0, 0, 0);\n"
"font: 24pt \"MS Shell Dlg 2\";\n"
"border: none;\n"
"color: white;\n"
"background-color: rgb(112, 107, 255);\n"
"border-radius: 10px\n"
"}\n"
"\n"
"QPushButton:hover{\n"
"border: none;\n"
"color: rgb(0, 0, 0);\n"
"font: 24pt \"MS Shell Dlg 2\";\n"
"border: none;\n"
"color: white;\n"
"background-color: #00FFFF;\n"
"border-radius: 10px\n"
"}\n")
self.answer2.setObjectName("answer2")
self.answer3 = QtWidgets.QPushButton(cityquestion1)
self.answer3.setGeometry(QtCore.QRect(240, 650, 291, 81))
font = QtGui.QFont()
font.setFamily("MS Shell Dlg 2")
font.setPointSize(24)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.answer3.setFont(font)
self.answer3.setStyleSheet("QPushButton{\n"
"border: none;\n"
"color: rgb(0, 0, 0);\n"
"font: 24pt \"MS Shell Dlg 2\";\n"
"border: none;\n"
"color: white;\n"
"background-color: rgb(112, 107, 255);\n"
"border-radius: 10px\n"
"}\n"
"\n"
"QPushButton:hover{\n"
"border: none;\n"
"color: rgb(0, 0, 0);\n"
"font: 24pt \"MS Shell Dlg 2\";\n"
"border: none;\n"
"color: white;\n"
"background-color: #00FFFF;\n"
"border-radius: 10px\n"
"}\n")
self.answer3.setObjectName("answer3")
self.answer4 = QtWidgets.QPushButton(cityquestion1)
self.answer4.setGeometry(QtCore.QRect(690, 650, 291, 81))
font = QtGui.QFont()
font.setFamily("MS Shell Dlg 2")
font.setPointSize(24)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.answer4.setFont(font)
self.answer4.setStyleSheet("QPushButton{\n"
"border: none;\n"
"color: rgb(0, 0, 0);\n"
"font: 24pt \"MS Shell Dlg 2\";\n"
"border: none;\n"
"color: white;\n"
"background-color: rgb(112, 107, 255);\n"
"border-radius: 10px\n"
"}\n"
"\n"
"QPushButton:hover{\n"
"border: none;\n"
"color: rgb(0, 0, 0);\n"
"font: 24pt \"MS Shell Dlg 2\";\n"
"border: none;\n"
"color: white;\n"
"background-color: #00FFFF;\n"
"border-radius: 10px\n"
"}\n")
self.answer4.setObjectName("answer4")
self.label = QtWidgets.QLabel(cityquestion1)
self.label.setGeometry(QtCore.QRect(240, 100, 741, 381))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("questionsimages/city/cityquestion1.jpg"))
self.label.setObjectName("label")
self.retranslateUi(cityquestion1)
QtCore.QMetaObject.connectSlotsByName(cityquestion1)
def retranslateUi(self, cityquestion1):
_translate = QtCore.QCoreApplication.translate
cityquestion1.setWindowTitle(_translate("cityquestion1", "CityQuestion_1"))
self.task.setText(_translate("cityquestion1", "Toto mesto, hlavné mesto ktorej krajiny ?"))
self.answer1.setText(_translate("cityquestion1", "Rusko"))
self.answer2.setText(_translate("cityquestion1", "Bielorusko"))
self.answer3.setText(_translate("cityquestion1", "Francúzsko"))
self.answer4.setText(_translate("cityquestion1", "Fínsko"))
class MainWindow1(QtWidgets.QWidget, Ui_cityquestion1):
def __init__(self):
super().__init__()
self.setupUi(self)
self.playlist = QMediaPlaylist()
self.url = QUrl.fromLocalFile("audio/clock.mp3")
self.playlist.addMedia(QMediaContent(self.url))
self.playlist.setPlaybackMode(QMediaPlaylist.Loop)
self.clock = QMediaPlayer()
self.clock.setPlaylist(self.playlist)
self.clock.setVolume(20)
self.clock.play()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('icons/city.svg'))
wd = MainWindow1()
wd.show()
sys.exit(app.exec_())
Этот код я вызываю из другого окна вот так:
self.window = MainWindow1()
self.window.show()
Источник: Stack Overflow на русском