from tkinter import *
import numpy as np
window = Tk()
window.title("3 X 3 Game")
btarray = np.full((3, 3), "red", dtype='str')
arfinal = []
def clickbutton(arvalue):
print (arvalue)
if btarray[arvalue] == "red":
btarray[arvalue] = "green"
else:
btarray[arvalue] = "red"
btn = Button(window, bg=btarray[0, 0], activebackground="blue", height=5, width=10, command= clickbutton("0, 0"))
the print returns 0, 0 which is the correct value for the first entry in the array, but I can't get it to accept this and change that array value, I get the error:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
I'm sure it has something to do with formatting and data types of the 0, 0 value in arvalue, but I have no idea how to fix it.
Is this what you want
from tkinter import *
import numpy as np
window = Tk()
window.title("3 X 3 Game")
btarray = np.full((3, 3), "red")
arfinal = []
def clickbutton(arvalue):
a,b=arvalue.split(',')
a,b=int(a),int(b)
if btarray[a,b] == "red":
btarray[a,b] = "green"
else:
btarray[a,b] = "red"
print(btarray)
btn = Button(window, activebackground="blue", height=5, width=10, command= lambda: clickbutton("0, 0"))
btn.pack()
window.mainloop()
Using clickbutton(0,0) or clickbutton([0,0]) and then in the function use btarray[arvalue[0],arvalue[1]] will work?
Because right now you are passing a string as index not an Array of 2 values.
To solve your second issue about the string "Red" to "green" check this Weird behaviour initializing a numpy array of string data
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.