Jul-05-2019, 07:24 PM
I programming the game battleships and got stuck when it came to the user input for the fields you want to shoot on. For that I created a tkinter window, which should take the user input given when 'ok' is pressed. When pressed, the programm should also check how many of that sized ship are left etc. and than obviously change the labels on the window (the absolute amount of them is already given, the numbers are an example). When i press 'ok', nothing happens and I don't know why. Thank you in advance.
import tkinter as tk
root = tk.Tk()
board_size = 10
c5a = 1 # amount of ships size 5
b4a = 2 # -"- 4
c3a = 3 # -"- 3
d2a = 4 # -"- 2
index = 0
class ShipPlacementPlayer:
def placement():
ship_list = ('Carrier (5)', 'Battleship (4)', 'Cruiser (3)', 'Destroyer (2)')
global board_size, c5a, b4a, c3a, d2a, index
sa = [c5a, b4a, c3a, d2a]
sl = sa[index] # ships left
current_ship = ship_list[index]
if sl != 0:
ship = tk.Label(root, text=current_ship, font=(15), bg='white')
ship.grid(row=1, column=0)
left = tk.Label(root, text=str(sl)+' left', font=(15), bg='white')
left.grid(row=1, column=1)
if index == 0:
c5a -= 1
return c5a
elif index == 1:
b4a -= 1
return b4a
elif index == 2:
c3a -= 1
return c3a
elif index == 3:
d2a -= 1
return d2a
else:
index += 1
return index
ship.destroy()
left.destroy()
ship = tk.Label(root, text=current_ship, font=(15), bg='white')
ship.grid(row=1, column=0)
left = tk.Label(root, text=str(sl)+' left', font=(15), bg='white')
left.grid(row=1, column=1)
if index == 0:
c5a -= 1
return c5a
elif index == 1:
b4a -= 1
return b4a
elif index == 2:
c3a -= 1
return c3a
elif index == 3:
d2a -= 1
return d2a
def window(*args):
global board_size
root.title('Ship placement')
w = 215; h = 192
ws = root.winfo_screenwidth(); hs = root.winfo_screenheight()
x = (ws/2) - (600+w/2); y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.configure(background='white')
root.attributes("-topmost", True)
place_label = tk.Label(root, text=' Place your ships! ', font=(15), fg='white', bg='black')
place_label.grid(row=0, columnspan=2)
emptyplace = tk.Label(root, font=(15), bg='white')
emptyplace.grid(row=1, columnspan=2)
vh = tk.IntVar()
v = tk.Radiobutton(root, text='Vertical', variable=vh, value=1, font=(10), bg='alice blue')
v.grid(row=2, column=0)
h = tk.Radiobutton(root, text='Horizontal', variable=vh, value=2, font=(10), bg='alice blue')
h.grid(row=2, column=1)
abc_list = ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z')
abc_list_bs = abc_list[:board_size]
label_row = tk.Label(root, text='Row:', font=(15), bg='white')
label_row.grid(row=4, column=0, sticky='E')
label_column = tk.Label(root, text='Column:', font=(15), bg='white')
label_column.grid(row=5, column=0, sticky='E')
rowi = tk.Spinbox(root, values=sorted(abc_list_bs), font=(15), width=3)
rowi.grid(row=4,column=1)
columni = tk.Spinbox(root, from_=0, to=board_size, font=(15), width=3)
columni.grid(row=5,column=1)
okp = tk.Button(root,text='OK', font=(15), fg='white', bg='black', command=ShipPlacementPlayer.placement())
okp.grid(row=6,columnspan=2)
app = ShipPlacementPlayer.window()
root.mainloop()
