Python Forum
[Tkinter] Button won't execute function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Button won't execute function
#1
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()
Reply


Messages In This Thread
Button won't execute function - by TheLegendOfPanda - Jul-05-2019, 07:24 PM
RE: Button won't execute function - by Yoriz - Jul-05-2019, 07:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a function interrupt button tkinter AnotherSam 2 5,573 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,052 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  Stop import from executing function, but allow the function to execute from tk btn. MrBitPythoner 4 2,721 Dec-08-2020, 10:00 PM
Last Post: MrBitPythoner
  Class function does not create command button Heyjoe 2 2,294 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  Tkinter:Unable to bind and unbind function with a button shallanq 2 5,063 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,042 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] How make a button perform a function after the user inputs numbers Zephyrforce 1 2,457 May-22-2019, 05:43 PM
Last Post: woooee
  [Kivy] Chagne a button's function after its first pressed TheStraying11 2 5,099 Feb-17-2019, 06:16 PM
Last Post: Yoriz
  [Tkinter] assigning two function for a single button prawesh05 3 5,081 Jan-16-2019, 10:39 PM
Last Post: Larz60+
  Unable to return value from callback function of a button in Python Tkinter nilaybnrj 4 20,840 Aug-05-2018, 11:01 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020