-1
I’m reading Head First Programming in Python and presented the following error in the execution of a proposed program:
Traceback (most recent call last): File "E:\Desktop\python\teste20_Classe_tkinter.py", line 12, in <module> panel = SoundPanel(app,mixer,"50459_M_RED_Nephlimizer.wav").pack() TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given
The code is this:
from tkinter import*
from SoundPanel import*
import pygame.mixer
app = Tk()
app.title("Mix de Sons")
mixer = pygame.mixer
mixer.init()
panel = SoundPanel(app,mixer,"50459_M_RED_Nephlimizer.wav").pack()
panel = SoundPanel(app,mixer,"49119_M_RED_HardBouncer.wav").pack()
def shutdown():
mixer.stop()
app.destroy()
app.protocol("WM_DELETE_WINDOW",shutdown)
app.mainloop()
Class code:
from tkinter import*
import pygame.mixer
class SoundPanel(Frame):
def _init_(self,app,mixer,arq_som):
Frame._init_(self,app)
self.trilha = mixer.Sound(arq_som)
self.tocar_trilha = IntVar()
botão_trilha = Checkbutton(self,variable = self.tocar_trilha,command = self.Play_Stop,text = arq_som)
botão_trilha.pack(side = LEFT)
self.volume = DoubleVar()
self.volume.set(trilha.get_volume())
volume_scale = Scale(self,variable = self.volume,from_ = 0.0,to = 1.0,resolution = 0.1,command = self.muda_volume,label = "Volume",orient = HORIZONTAL)
volume_scale.pack(side = RIGHT)
def Play_Stop(self):
if self.tocar_trilha.get() == 1:
self.trilha.play(loops = -1)
else:
self.trilha.stop()
def muda_volume(self,v):
self.trilha.set_volume(self.volume.get())
It seems that your
_init_
is written with only 1 underline, try replacing with__init__
and check if it works.– ThRnk
Yes, your
__init__
is not created as a constructor because of the error in the syntax. Perhaps swapping solves the problem.– ThRnk
In fact there is a difference in the number of arguments of the Class and panel = Soundpanel(app,mixer, arq_som)
– Luciana Faria Costa