内容简介:注意:player必须在QApplication实例化后创建,否则无报错不播放文章首发:
from PyQt5.QtCore import * from PyQt5.QtMultimedia import * from PyQt5.QtWidgets import * app = QApplication([]) player = QMediaPlayer() player.setMedia(QMediaContent(QUrl.fromLocalFile('/mnt/d/music/test/金娃娃-彩虹糖的梦.wma'))) player.play() app.exec()
注意:player必须在QApplication实例化后创建,否则无报错不播放
PyQt5多线程
import sys import time from PyQt5.QtCore import * from PyQt5.QtWidgets import * class BigWork(QThread): signal = pyqtSignal(type(None)) def run(self) -> None: print("Big Working...") time.sleep(3) print("Big done.") self.signal.emit(None) def terminate(self) -> None: print("Terminating...") super().terminate() print("Terminated.") class Window(QWidget): root_layout: QVBoxLayout button1: QPushButton button2: QPushButton button3: QPushButton work: QThread def __init__(self) -> None: super().__init__() self.setup_layout() self.work = BigWork() self.work.signal.connect(lambda x: print("Signal received:", x)) def setup_layout(self): self.button1 = QPushButton("Start", parent=self) self.button2 = QPushButton("Stop", parent=self) self.button3 = QPushButton("About Qt", parent=self) self.root_layout = QVBoxLayout() self.root_layout.addWidget(self.button1) self.root_layout.addWidget(self.button2) self.root_layout.addWidget(self.button3) self.button1.clicked.connect(lambda: self.work.start()) self.button2.clicked.connect(lambda: self.work.terminate()) self.button3.clicked.connect(lambda: QMessageBox.aboutQt(self)) self.resize(600, 400) self.setLayout(self.root_layout) def main(): app = QApplication(sys.argv) window = Window() window.show() app.exec() if __name__ == '__main__': main()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。