Doubt in Python code using Tkinter

Asked

Viewed 41 times

-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.

  • Yes, your __init__ is not created as a constructor because of the error in the syntax. Perhaps swapping solves the problem.

  • In fact there is a difference in the number of arguments of the Class and panel = Soundpanel(app,mixer, arq_som)

1 answer

0


There were several minor errors in code and indentation

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(self.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())  

The code is this:

from tkinter 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")
panel.pack()
panel = SoundPanel(app,mixer,"49119_M_RED_HardBouncer.wav")
panel.pack()

def shutdown():
    mixer.stop()
    app.destroy()

app.protocol("WM_DELETE_WINDOW",shutdown)

app.mainloop()

I used Anaconda so the classes were in the same document, you will need to import if use in separate files.

I modified of :

panel = SoundPanel(app,mixer,"50459_M_RED_Nephlimizer.wav").pack()
panel = SoundPanel(app,mixer,"49119_M_RED_HardBouncer.wav").pack())

for:

panel = SoundPanel(app,mixer,"50459_M_RED_Nephlimizer.wav")
panel.pack()
panel = SoundPanel(app,mixer,"49119_M_RED_HardBouncer.wav")
panel.pack()

of:

def _init_(self,app,mixer,arq_som):

    Frame._init_(self,app)

for:

    def __init__(self,app,mixer,arq_som):

        Frame.__init__(self,app)

of:

self.volume.set(trilha.get_volume())

for:

self.volume.set(self.trilha.get_volume())

Upshot:

resultado

  • Imonferrari, it worked! Thank you very much. I would just like to know the reason why I detected an error in the first execution regarding the number of arguments in def__init__ (4 args) and the call in the Prog.main panel = Soundpanel(app,mixer,"50459_M_RED_Nephizer.wav") with 3 args. :)

  • The init is the builder and you had mispronounced, I believe he was not recognizing an argument you passed. If the above answer has solved your question, consider marking the answer as valid. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.