如何使用Python Qt4绘制形状然后播放声音?

时间:2016-08-17 15:18:19

标签: python qt4

使用Python QT4,我想在小部件上画一条线,然后开始播放声音。我不明白为什么反之亦然,即当我运行程序时,我首先听到声音,然后绘制一条线。

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import gtk
import pygame


class DrawLine(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(300, 300, 350, 350)
        self.setWindowTitle('Draw a line and then play a song')

    def paintEvent(self, event):
        paint = QPainter()
        paint.begin(self)
        paint.setRenderHint(QPainter.Antialiasing)
        paint.setBrush(Qt.white)
        pen = QPen(Qt.black, 2, Qt.SolidLine)
        paint.setPen(pen) 
        paint.drawLine(20, 80, 250, 80)
        paint.end()
        play()

    global play  
    def play():
        file_name = "/home/train_ipa/ipa_b.wav"
        pygame.mixer.init()
        pygame.mixer.music.load(file_name)
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy() == True:
            continue

app = QApplication([])
line = DrawLine()
line.show()
app.exec_()

0 个答案:

没有答案