OADINC
OADINC

Reputation: 35

Trouble with passing variables in functions

So I'm making a music player in python but I'm having trouble when passing variables into my function and I get this error message:

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

I've searched on google for an anwser but their programmes and solutions were to different for me to understand how it works and why it works. Anyway here is my code:

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_())

Upvotes: 0

Views: 56

Answers (1)

kabanus
kabanus

Reputation: 25895

This is where a call is defined:

btnNext.clicked.connect(self.next)

So when the button is clicked you run self.next(mouse_event) This is the function signature:

def next(self, CurrentSong, Song):

So when a button is clicked you send two parameters: self, from the self.next, and an event which has nothing to do with songs, it has info on the click. You were expecting 3 parameters self,CurrentSong,Song, and not an event - so two mistakes. A workaround is to not accept parameters besides self to next, and save the current song in the class:

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

etc... If you want to forego the event you can lambda hide it:

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

and change next accordingly.

Upvotes: 1

Related Questions