-3

this is a class for designing a toplevel window with Tkinter the issue comes from the method which tries to get an info from the member self.lbox but it is not recognized as a member. the error is : NameError: name 'self' is not defined

additional question : I believe I can remove all 'self' prefixes in the member declaration. Am I correct ?

class Fen_Radioweb(Toplevel):
  "Fenêtre satellite contenant des contrôles de redimensionnement"
  def __init__(self,  **Arguments):
      Toplevel.__init__(self,  **Arguments)
      global window_height
      global window_width
      global x_cordinate 
      global y_cordinate 
      self.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
      self.content1 = ttk.Frame(self)
      self.content1.grid(column=0, row=0)
      self.led_1=ttk.Button(self.content1, text='play',command=lambda:player.play())
      self.led_1.grid(column=1, row=0)
      self.led_2=ttk.Button(self.content1, text='stop',command=lambda:player.stop())
      self.led_2.grid(column=1, row=1)
      self.lbox=Listbox(self.content1, height=5,listvariable=cnames)
      self.lbox.bind('<Double-1>', self.change_channel)
      self.lbox.grid(column=2, row=2)
      
  def change_channel(*args):
      idxs = self.lbox.curselection()
      idx = int(idxs[0])
      global channel_ini
      channel_ini=idx
      url=liste_url[idx]
      media=instance.media_new(url)
      player.set_media(media)
      player.play()

I tried to remove self in idxs = self.lbox.... I tried to replace (*args) with (self) unsuccessfully

3
  • You need to def change_channel(self, *args):. Note the self as first argument. And: no, you can't just remove all self prefixes. You will get an error.
    – Friedrich
    Commented Jul 2 at 8:48
  • "I believe I can remove all 'self' prefixes in the member declaration" - you can do this if you have change_channel as a @staticmethod, but in that case it won't be able to reference self within the method. If you want to reference self in the method then you have to declare it in the method declaration. Commented Jul 2 at 8:49
  • please start using some linters, maybe several together: mypy, pylint, pylance, flake8. They'll point to many errors even before you know it. Likely they are easily integrating in your editor, unless you're using a notepad. Commented Jul 2 at 9:07

1 Answer 1

1

You're missing the self argument in the function's declaration:

def change_channel(self, *args):
    # Here---------^
1
  • Great. Thk you all for you precise and useful answers. Commented Jul 2 at 9:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.