Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.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 函数中传递变量的问题_Python_Function_Variables - Fatal编程技术网

Python 函数中传递变量的问题

Python 函数中传递变量的问题,python,function,variables,Python,Function,Variables,因此,我正在用python制作一个音乐播放器,但在将变量传递到函数中时遇到了问题,我收到了以下错误消息: TypeError: next() takes exactly 3 arguments (2 given) 我在谷歌上搜索过anwser,但他们的程序和解决方案与我不同,我无法理解它是如何工作的以及为什么工作的。无论如何,这是我的代码: import sys import os import pygame from PyQt4 import QtGui, QtCore from time

因此,我正在用python制作一个音乐播放器,但在将变量传递到函数中时遇到了问题,我收到了以下错误消息:

TypeError: next() takes exactly 3 arguments (2 given)
我在谷歌上搜索过anwser,但他们的程序和解决方案与我不同,我无法理解它是如何工作的以及为什么工作的。无论如何,这是我的代码:

import sys
import os
import pygame
from PyQt4 import QtGui, QtCore
from time import sleep
class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("Music Player Alpha")
        AutoPlay = True
        Play = True
        SongQueue = []
        Song = os.listdir('/home/pi/Desktop/Muziek/' )
        Song = sorted(Song)
        CurrentSong = 0
        pygame.mixer.init()
        pygame.mixer.music.load('/home/pi/Desktop/Muziek/' + Song[0])
        pygame.mixer.music.play()
        self.home()

    def home(self):
        btnQuit = QtGui.QPushButton("Quit", self)
        btnQuit.clicked.connect(self.close)
        btnPlay = QtGui.QPushButton("Play", self)
        btnPlay.clicked.connect(self.play)
        btnPlay.move(100, 100)
        btnPause = QtGui.QPushButton("Pause", self)
        btnPause.clicked.connect(self.pause)
        btnPause.move(200, 100)
        btnNext = QtGui.QPushButton("Next", self)
        btnNext.clicked.connect(self.next)
        btnNext.move(300, 100)
        btnPrevious = QtGui.QPushButton("Previous", self)
        btnPrevious.clicked.connect(self.previous)
        btnPrevious.move(0, 100)
        self.show()

    def close(self):
        print("Closing application")
        sys.exit()

    def play(self, Play):
        pygame.mixer.music.unpause()
        Play = True

    def pause(self, Play):
        pygame.mixer.music.pause()
        play = False

    def next(self, CurrentSong, Song):
        print("1")
        CurrentSong = CurrentSong + 1
        if CurrentSong > len(Song) + 1:
            CurrentSong = 0
            pygame.mixer.music.load('/home/pi/Desktop/Muziek/' + Song[CurrentSong])
            pygame.mixer.music.play()
        else:
            pygame.mixer.music.load('/home/pi/Desktop/Muziek/' + Song[CurrentSong])
            pygame.mixer.music.play()

    def previous(self, CurrentSong, Song):
        CurrentSong = CurrentSong - 1
        if CurrentSong < 0:
            CurrentSong = len(Song) -1
            pygame.mixer.music.load('/home/pi/Desktop/Muziek/' + Song[CurrentSong])
            pygame.mixer.music.play()
        else:
            pygame.mixer.music.load('/home/pi/Desktop/Muziek/' + Song[CurrentSong])
            pygame.mixer.music.play()


app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())

这是定义呼叫的地方:

btnNext.clicked.connect(self.next)
因此,当单击按钮时,运行self.nextmouse_事件,这是函数签名:

def next(self, CurrentSong, Song):
因此,当单击按钮时,您会发送两个参数:self,来自self.next,以及一个与歌曲无关的事件,它在单击时会显示信息。您预期的是3个参数self、CurrentSong、Song,而不是一个事件-因此有两个错误。解决方法是不接受除self到next之外的参数,并将当前歌曲保存在类中:

def next(self,event):
    currentSong = self.currentSong
等等。。。如果要放弃该事件,可以将其隐藏:

btnNext.clicked.connect(lambda e:self.next)

并相应地更改下一步。

显示函数调用您的btnNext.clicked事件处理程序正在调用self.next,但没有参数。