在函数中传递变量有问题

时间:2017-05-04 10:39:23

标签: python function variables

所以我在python中创建了一个音乐播放器,但是在将变量传递给我的函数时遇到了麻烦,我收到了这条错误消息:

TypeError: next() takes exactly 3 arguments (2 given)

我在google上搜索了一个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_())

1 个答案:

答案 0 :(得分:1)

这是定义呼叫的地方:

btnNext.clicked.connect(self.next)

因此,当单击该按钮时,您运行self.next(mouse_event)这是函数签名:

def next(self, CurrentSong, Song):

因此,当点击按钮时,您会发送两个参数:self来自self.next,而event与歌曲无关,它有关于点击的信息。你期待3个参数self,CurrentSong,Song,而不是event - 所以有两个错误。解决方法是不接受除了self到next的参数,并将当前歌曲保存在类中:

def next(self,event):
    currentSong = self.currentSong

等......如果你想放弃这个事件,你可以将它隐藏起来:

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

并相应地更改next