Takes 2 arguments? I gave one???!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 360monkey
    New Member
    • Feb 2010
    • 17

    Takes 2 arguments? I gave one???!!!

    Code:
    from Tkinter import *
    import random
    
    class App:
    	def __init__(self, master):
    		frame = Frame(master)
    		frame.pack()
    		self.label = LabelFrame(frame, text='Battler III')
    		self.label.pack()
    		self.arena = Button(frame, text='Arena Mode', relief=RAISED, command=self.arena)
    		self.arena.grid(row=4)
    		self.arena.pack()
    		self.adven = Button(frame,text='Adventure Mode', relief=RAISED, command=self.adventure)
    		self.adven.grid(row=6)
    		self.adven.pack()
    		
    	def arena(self, secondary):
    		top = Toplevel()
    		sframe = Frame(secondary)
    		sframe.pack()
    		self.label = LabelFrame(sframe, text='Battler III')
    		self.label.pack()
    		
    		print('goodbye world')
    	
    	def adventure(self):
    		print('hello world')
    		
    root = Tk()		
    app = App(None)
    root.mainloop()

    What i am trying to do, is create a Tkinter application frame and apply a frame to it called frame (called sframe) and i get this error when i run it (on Python 2.6.4)

    line 1410 in __call__
    return self.(*args)
    TypeError: arena takes exactly two arguments (1 given)

    can you help me out?
    Last edited by bvdet; Feb 6 '10, 04:47 PM. Reason: Add code tags
  • vintello
    New Member
    • Feb 2010
    • 5

    #2
    in "def arena(self, secondary)" you take 2 param - "self" and "secondary"

    in
    "self.arena = Button(frame, text='Arena Mode', relief=RAISED, command=self.ar ena)"
    you called "self.arena " with one parametr "self".
    Where second parametr ?

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Please use code tags when posting code. Please read the posting guidelines here.

      You are defining an instance attribute "self.arena = Button(frame, t.....". Later in your code, you are defining a method "def arena(self, secondary):". Should not these objects have different names?

      Note that Button keyword "command" expects a function object that takes no arguments (the "event" is automatically passed to the function). You can accomplish this by defining a closure and using a default argument.

      Example (untested):
      Code:
              def handler(event, parent=frame):
                  return self.arenaMethod(event, parent)
      
              self.arenaAttr = Button(frame, text='Arena Mode', relief=RAISED, command=handler)
       
          def arenaMethod(self, parent):
              def f():
                  top = Toplevel()
                  sframe = Frame(parent)
                  sframe.pack()
                  self.label = LabelFrame(sframe, text='Battler III')
                  self.label.pack()
              return f
      BV - Moderator
      Last edited by bvdet; Feb 6 '10, 06:04 PM. Reason: Fix code

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        The example I previously posted can be advantageous when you are creating a list of buttons as in:
        Code:
                    # ....snippet....
                    self.columns = 5
                    self.buttonList = []
                    j=-1
                    for i, t in enumerate(self.btnList):
                        btn = Tkinter.Button(self.buttonFrame.widget,
                                             text=t,
                                             anchor='center',
                                             bd=3,
                                             bg='#ffffff000',
                                             fg="#000000fff",
                                             activebackground = "#000000fff",
                                             activeforeground = "#ffffff000",
                                             font=textFont3,
                                             padx='1.0m',
                                             pady='1.0m',
                                             relief='raised',
                                             state='normal',
                                             width=self.btnWidth)
                        self.buttonList.append((btn, t))
                        if not i%self.columns:
                            j += 1
                        # Arrange buttons into columns and rows
                        btn.grid(row=j, column=i%self.columns)
                        def handler(event, i=i):
                            return self.__buttonHandler(event, i)
                        btn.bind(sequence="<ButtonRelease-1>", func=handler)
                
                def __buttonHandler(self, event, btnNumber):
                    setattr(self, 'value', self.buttonList[btnNumber][1])
                    # After user makes a choice, exit the dialog box
                    self.dlg1.Quit()
        In your case, you can do it this way as well:
        Code:
                self.arenaAttr = Button(frame, text='Arena Mode', relief=RAISED, command=self.arenaMethod(frame))
         
            def arenaMethod(self, parent):
                def f():
                    top = Toplevel()
                    sframe = Frame(parent)
                    sframe.pack()
                    self.label = LabelFrame(sframe, text='Battler III')
                    self.label.pack()
                return f

        Comment

        • 360monkey
          New Member
          • Feb 2010
          • 17

          #5
          thank you! my code works now! i will read the posting regulations and return if i ned help again.
          ^.^

          Comment

          Working...