0

I'm a beginner programmer and I wanted to learn by a game. I'm trying to make a battleship game with the user clicking on buttons(made using Tkinter) to place battle ships around the board. When I click on buttons I get an error saying the below. How to get the right button int the dictionary choises = {}? The error says:

Traceback (most recent call last): File "C:\Python Codes\Battleship.py", line 29, in choice() TypeError: choice() takes exactly 2 arguments (0 given)

Here is the code I used:

from Tkinter import *
import Tkinter as tk
screen = tk.Tk(className = "Battle Ship Game" )
screen.geometry("300x300")
screen["bg"] = "white"

line1= list()

def choice(x,y) :
    global choises
    choises = {}

    choises[x] = y
    print choises

def buildaboard1(screen) :
    x = 20
    for n in range(0,10) :
        y = 20
        for i in range(0,10) :
            line1.append(tk.Button(screen ))
            line1[-1]["command"] = (lambda n : choice (x , y))
            line1[-1].place( x = x , y = y+20 , height = 20 , width = 20 )
            y = y+20
        x = x +20


buildaboard1(screen)
choice()
screen.mainloop()
6
  • The error is on the second to last line. You're calling choice(), but choice expects two arguments, x and y. Commented Jun 30, 2014 at 22:48
  • if I pass choice(x , y) on the last line I get another error :Traceback (most recent call last): File "C:\Python Codes\Battleship.py", line 29, in <module> choice(x , y) NameError: name 'x' is not defined what do I do? :( Commented Jun 30, 2014 at 22:50
  • Why are you calling this function? Commented Jun 30, 2014 at 22:51
  • You should go read up on functions and how arguments work :) the choices argument is expecting 2 variables, x and y. If you do not intend sending it those, remove them from the function definition - but then be sure to create them inside the function otherwise you will get that error again.. haha Commented Jun 30, 2014 at 22:52
  • x and y are the names you gave to the arguments of choice. You can't call choice(x, y) unless x and y have already been defined, like x = y = 2. Commented Jun 30, 2014 at 22:54

1 Answer 1

1

This way Tkinter assign values from x and y to a and b at once.
And choice() will can use it when you press button.

line1[-1]["command"] = (lambda a=x, b=y: choice (a , b))

This way Button want to get values from x and y when you click it but then x and y don't exist.

line1[-1]["command"] = (lambda: choice (x , y))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.