0

Hello I am trying to create a game where you click on a button and it tells you if it is treasure or a bandit. It if is treasure then you get points if it is a bandit you loose your coins. I have created the following code but cannot find a way of finding out if I have managed to set the buttons to the values stored in the array. How do you use the data in the array and when a button is clicked it tells you what the value is. Am I doing the array correctly any help would be appreciated I am going round in circles and cannot think straight now.

from tkinter import *  
import tkinter.messagebox  
import random

#count=0

root = Tk()  
TreasureMap = Frame(root)  
TreasureMap.grid()  

root.title("TreasureMap")  
text_box = Entry(TreasureMap, justify=RIGHT)

text_box.grid(row = 0, column = 0, columnspan = 8,)  
text_box.insert(0, "0")  

amount_to_add='B'  
my_array = [[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],    
[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]]  
my_array[random.randrange(len(my_array))].append(amount_to_add)  
my_array[random.randrange(len(my_array))].append(amount_to_add)  
my_array[random.randrange(len(my_array))].append(amount_to_add)  
my_array[random.randrange(len(my_array))].append(amount_to_add)  

Treasure='T'
my_array[random.randrange(len(my_array))].append(Treasure)  
my_array[random.randrange(len(my_array))].append(Treasure)  
my_array[random.randrange(len(my_array))].append(Treasure)  
my_array[random.randrange(len(my_array))].append(Treasure)    
my_array[random.randrange(len(my_array))].append(Treasure)  
my_array[random.randrange(len(my_array))].append(Treasure)  
my_array[random.randrange(len(my_array))].append(Treasure) 
my_array[random.randrange(len(my_array))].append(Treasure)  
my_array[random.randrange(len(my_array))].append(Treasure)  
my_array[random.randrange(len(my_array))].append(Treasure)  


print(my_array)  


def changelabel():  `   
        tkinter.messagebox.showinfo('Hellow this button works') `` 
        print('hello this button works') ` ``  
        print(my_array)  ``  ``   
        return  



i = 0  
bttn = []  
for j in range(1,9):`     
     for k in range(8):  ``    
     bttn.append(Button(TreasureMap, text = ' ', value=random.shuffle      
     (my_array),command=changelabel))       
     bttn[i].grid(row = j, column = k)           
     i += 1  ``     


 TreasureMap.mainloop()  
Ann.S
  • 3
  • 2

1 Answers1

0

This may be what you're wanting to do.
Borrowing info from this answer, I put a callback in the Button command to pass the row and column numbers.
Also, 'B' and 'T' are being appended to each array of 0's, instead of getting assigned to locations within the array, which is what I think you want.
I made a few other changes, including fixing the for loops to start from 0 (the first row), etc.

# previous code

amount_to_add='B'  
my_array = [[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],    
[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]]  

# ... all code up to here unchanged ...

for _ in range(4):
    my_array[random.randrange(len(my_array))][random.randrange(len(my_array))] = amount_to_add   

Treasure='T'
for _ in range(4):
    my_array[random.randrange(len(my_array))][random.randrange(len(my_array))] = Treasure  

# print my_array # uncomment to check array values before shuffle
random.shuffle(my_array)
# print my_array # and after shuffle to confirm button is getting the correct values

def changelabel(j,k):
    tkinter.messagebox.showinfo('Button %d,%d' % (j,k), 'Button %d,%d contains a %s' % (j,k, my_array[j][k]))
        print('hello this button works')
        # return # not needed  

i = 0  
bttn = []

for j in range(0,8):   
    for k in range(8):
        bttn.append(Button(TreasureMap, text = ' ', command= lambda row = j, column = k: changelabel(row, column)))
        bttn[i].grid(row = j, column = k)           
        i += 1

Feel free to comment or ask questions if this doesn't do what you were asking for. Otherwise, hope it helps.

Community
  • 1
  • 1
  • what does command= lambda row = j, column=k: changelabel(row,column) do in this code?? – Ann.S Feb 16 '17 at 22:34
  • command is the argument passed to the Button just as you had used it. However we also want to include parameters to `changelabel` for it to know which row/column the button was being called from. [The callback can be a function, bound method, or any other callable Python object](http://effbot.org/tkinterbook/button.htm#Tkinter.Button.config-method), here `changelabel` *is that function* and the [lambda](http://www.secnetix.de/olli/Python/lambda_functions.hawk) assigns `j` and `k` at runtime as parameters to pass to `changelabel` – chickity china chinese chicken Feb 16 '17 at 22:58