Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中面临在Qt中更新progressbar的问题_Python_Qt_Qml_Progress Bar_Pyside2 - Fatal编程技术网

在python中面临在Qt中更新progressbar的问题

在python中面临在Qt中更新progressbar的问题,python,qt,qml,progress-bar,pyside2,Python,Qt,Qml,Progress Bar,Pyside2,写播放器,然后出现问题,当我启动歌曲时,我想progressbar在goes music、Make cycle、投入线程时被更新,更新时的值通过qml中的信号传输,但在比问题中,这些值只有在我点击此按钮时才被传输,但不是实时的 Main.py progressMusicSignal = Signal(float, arguments=['progressMusic']) @Slot('float') def setValue(self, flagTrue): global thread

写播放器,然后出现问题,当我启动歌曲时,我想progressbar在goes music、Make cycle、投入线程时被更新,更新时的值通过qml中的信号传输,但在比问题中,这些值只有在我点击此按钮时才被传输,但不是实时的

Main.py

progressMusicSignal = Signal(float, arguments=['progressMusic'])

@Slot('float')
def setValue(self, flagTrue):
    global thread, que
    if flagTrue == 1:
        que = queue.Queue()
        thread = Thread(target=lambda ques, arg1: ques.put(progressBarMusic(arg1)), args=(que, flagTrue),
                                daemon=True)
        thread.start()
        result = que.get()
        self.progressMusicSignal.emit(result)
    elif flagTrue == 2:
        thread.join()

def playMusic(flagMusic=0):
    if flagMusic == 1:
        pygame.mixer.music.load(PATHLESS + MUSICFILEWAV)
        pygame.mixer.music.play()
    if flagMusic == 2:
        pygame.mixer.music.pause()
    if flagMusic == 3:
        pygame.mixer.music.unpause()

def progressBarMusic(flagTrue):
    if flagTrue == 1:
        while True:
            song = pygame.mixer.Sound(PATHLESS + MUSICFILEWAV)
            getLengthMusic = pygame.mixer.Sound.get_length(song)
            milSec = pygame.mixer.music.get_pos()
            operationLength = getLengthMusic // 10
            print(operationLength)
            sec = milSec // 1000
            secRes = milSec // 100
            print(secRes)
            operationSecPercent = (secRes / operationLength) / 100
            print(operationSecPercent)
            if sec != getLengthMusic:
                return operationSecPercent      
Main.qml

RoundButton {
    id: plauPauseBtn
    x: 370
    y: 15
    width: 50
    height: 50
    text: "\u25b7"
    enabled: true
    opacity: 1.0
    font.weight: Font.ExtraBold
    font.capitalization: Font.MixedCase
    font.strikeout: false
    font.underline: false
    font.italic: false
    display: AbstractButton.TextBesideIcon
    font.bold: false
    font.pointSize: 14
    font.family: "Tahoma"
    onClicked: {
        plauPauseBtn.opacity = 0.0;
        plauPauseBtn.enabled = false;
        stopPauseBtn.opacity = 1.0;
        stopPauseBtn.enabled = true;
        con.playMusicInt(1)
        con.setValue(1)
    }
}

RoundButton {
    id: stopPauseBtn
    x: 370
    y: 15
    width: 50
    height: 50
    text: "||"
    enabled: false
    opacity: 0.0
    bottomPadding: 13
    font.weight: Font.ExtraBold
    font.capitalization: Font.MixedCase
    font.strikeout: false
    font.underline: false
    font.italic: false
    display: AbstractButton.TextBesideIcon
    font.bold: false
    font.pointSize: 7
    font.family: "Tahoma"
    onClicked: {
        con.playMusicInt(2)
        con.setValue(2)
        stopPauseBtn.opacity = 0.0;
        stopPauseBtn.enabled = false;
        playAgainBtn.opacity = 1.0;
        playAgainBtn.enabled = true;
    }
}

    RoundButton {
        id: playAgainBtn
        x: 370
        y: 15
        width: 50
        height: 50
        text: "\u25b7"
        enabled: false
        opacity: 0.0
        bottomPadding: 13
        font.weight: Font.ExtraBold
        font.capitalization: Font.MixedCase
        font.strikeout: false
        font.underline: false
        font.italic: false
        display: AbstractButton.TextBesideIcon
        font.bold: false
        font.pointSize: 14
        font.family: "Tahoma"
        onClicked: {
            con.playMusicInt(3)
            con.setValue(1)
            playAgainBtn.opacity = 0.0;
            playAgainBtn.enabled = false;
            stopPauseBtn.opacity = 1.0;
            stopPauseBtn.enabled = true;
        }
    }

    ProgressBar {
        id: musicProgressBar
        x: 0
        y: 0
        width: 800
        height: 5
        indeterminate: false
        value: 0.0
    }
        Connections {
            target: con
            onProgressMusicSignal: {
                musicProgressBar.value = progressMusic
        }
    }

OP提供的代码是可以理解的,因此我将避免分析它,因此我将从头开始提出解决方案

在本例中,我在pygame.mixer.music上创建了一个包装器,它公开了源、卷、当前状态的属性,并通过pyqtSlot公开了方法,该类不处理应用程序的逻辑,而只是一个资源

应用程序的逻辑必须在QML中处理按钮的状态,在这种情况下,不需要创建多个按钮,因为只有一个按钮可以更改文本

考虑到上述情况,解决方案是:

main.py

导入操作系统
输入数学
导入pygame
从PyQt5导入QtCore、QtGui、QtQml
PyGameSound类(QtCore.QObject):
sourceChanged=QtCore.pyqtSignal()
volumeChanged=QtCore.pyqtSignal()
stateChanged=QtCore.pyqtSignal()
notifyIntervalChanged=QtCore.pyqtSignal()
progressChanged=QtCore.pyqtSignal()
error=QtCore.pyqtSignal(str,参数=[“消息”])
类别状态:
播放状态、暂停状态、停止状态=范围(3)
QtCore.Q_枚举(状态)
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.destrocted.connect(self.on_destrocted)
pygame.mixer.init()
self._source=“”
自身时间间隔=1000
自身进度=0.0
自身体积=1.0
self.\u notify\u timer=QtCore.QTimer(self,timeout=self.on\u notify\u回调)
self.\u state=PyGameSound.state.StoppedState
@pyqtProperty(状态,notify=stateChanged)
def状态(自身):
返回自我状态
定义更新状态(自身、状态):
自我状态=状态
self.stateChanged.emit()
def on_notify_回调(自):
如果self.source:
尝试:
song=pygame.mixer.Sound(self.source)
总计=歌曲。获取长度()
pos=pygame.mixer.music.get\u pos()
如果位置>=0:
百分比=位置/(总计*1000.0)
如果math.isclose(
百分比,1.0,绝对值=self.0/1000.0
):
百分比=1.0
self.progress=百分比
除了pygame.error作为消息:
self.error.emit(str(message))
@pyqtProperty(str,notify=sourceChanged)
def源(自身):
返回自我。\u源
@source.setter
def源(自身,源):
尝试:
pygame.mixer.music.load(源代码)
除了pygame.error作为消息:
self.error.emit(str(message))
source=“”
如果是自我。_来源!=资料来源:
self.\u source=源
self.sourceChanged.emit()
@pyqtProperty(float,notify=volumeChanged)
def体积(自身):
返回pygame.mixer.music.get_volume()
@音量调节器
def体积(自身,体积):
pygame.mixer.music.set_音量(音量)
self.volumeChanged.emit()
@pyqtProperty(int,notify=notifyIntervalChanged)
def通知间隔(自):
返回自我。\u
@notifyInterval.setter
def notifyInterval(自我,间隔):
如果是自组。_notifyInterval!=间隔时间:
self.\u notifyInterval=间隔
is\u active=self.\u notify\u timer.isActive()
如果您处于活动状态:
self.\u notify\u timer.stop()
self.\u notify\u timer.setInterval(self.\u notifyInterval)
如果您处于活动状态:
self.\u notify\u timer.start()
@pyqtProperty(float,notify=progressChanged)
def进度(自我):
回归自我
@进度设定器
def进度(自我、进度):
自我进步=进步
self.progressChanged.emit()
@QtCore.pyqtSlot()
def播放(自我):
尝试:
pygame.mixer.music.play()
self.\u notify\u timer.start()
除了pygame.error作为消息:
self.error.emit(str(message))
返回
自我更新状态(PyGameSound.state.PlayingState)
@QtCore.pyqtSlot()
def取消暂停(自我):
pygame.mixer.music.unpuse()
self.\u notify\u timer.start()
自我更新状态(PyGameSound.state.PlayingState)
@QtCore.pyqtSlot()
def暂停(自我):
pygame.mixer.music.pause()
self.\u notify\u timer.stop()
自我更新状态(PyGameSound.state.PausedState)
@QtCore.pyqtSlot()
def停止(自):
pygame.mixer.music.stop()
self.\u notify\u timer.stop()
自我更新状态(PyGameSound.state.StoppedState)
def on_已销毁(自):
pygame.mixer.quit()
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
current_dir=os.path.dirname(os.path.realpath(uu文件_uu))
QtQml.qmlRegisterType(PyGameSound,“PyGame”,1,0,“PyGameSound”)
app=QtGui.qgui应用程序(sys.argv)
engine=QtQml.QQmlApplicationEngine()
filename=os.path.join(当前目录,“main.qml”)
engine.load(QtCore.QUrl.fromLocalFile(文件名))
如果不是engine.rootObjects():
系统出口(-1)
sys.exit(app.exec_())
main.qml

import QtQuick 2.13
import QtQuick.Controls 2.5

import PyGame 1.0

ApplicationWindow{
    visible: true
    width: 640
    height: 480

    PyGameSound{
        id: sound
        notifyInterval: 10
        source: "/path/of/music.wav"
        volume: 1.0
        onError: console.log(message)
    }

    RoundButton {
        id: play_pause_button
        x: 370
        y: 15
        width: 50
        height: 50
        text: "\u25b7"
        display: AbstractButton.TextBesideIcon

        font {
            weight: Font.ExtraBold
            capitalization: Font.MixedCase
            strikeout: false
            pointSize: 14
            family: "Tahoma"
            bold: false
            underline: false
            italic: false
        }
        onClicked: {
            if(sound.state == PyGameSound.StoppedState){
                sound.play()
                play_pause_button.text = "||"
            }
            else if(sound.state == PyGameSound.PlayingState){
                sound.pause()
                play_pause_button.text = "\u25b7"
            }
            else if(sound.state == PyGameSound.PausedState){
                sound.unpause()
                play_pause_button.text = "||"
            }
        }
    }

    ProgressBar {
        id: musicProgressBar
        width: parent.width
        height: 5
        indeterminate: false
        value: sound.progress
    }
}

尽管最简单的解决方案是使用音频模块:

从PyQt5导入QtCore、QtGui、QtQml
如果名称=“\uuuuu main\uuuuuuuu”:
导入操作系统
导入系统
current_dir=os.path.dirname(os.path.realpath(uu文件_uu))
app=QtGui.qgui应用程序(sys.argv)
engine=QtQml.QQmlApplicationEngine()
filename=os.path.join(当前目录,“main.qml”)
import QtQuick 2.13
import QtQuick.Controls 2.5
import QtMultimedia 5.13

ApplicationWindow{
    visible: true
    width: 640
    height: 480
    Audio{
        id: sound
        notifyInterval: 10
        source: "/path/of/music.wav"
    }

    RoundButton {
        id: play_pause_button
        x: 370
        y: 15
        width: 50
        height: 50
        text: "\u25b7"
        display: AbstractButton.TextBesideIcon

        font {
            weight: Font.ExtraBold
            capitalization: Font.MixedCase
            strikeout: false
            pointSize: 14
            family: "Tahoma"
            bold: false
            underline: false
            italic: false
        }
        onClicked: {
            if(sound.playbackState == Audio.StoppedState){
                sound.play()
                play_pause_button.text = "||"
            }
            else if(sound.playbackState == Audio.PlayingState){
                sound.pause()
                play_pause_button.text = "\u25b7"
            }
            else if(sound.playbackState == Audio.PausedState){
                sound.play()
                play_pause_button.text = "||"
            }
        }
    }

    ProgressBar {
        id: musicProgressBar
        width: parent.width
        height: 5
        indeterminate: false
        value: sound.position/sound.duration
    }
}