mardi 8 février 2022

TypeError: can only concatenate str (not "int") to str in python [closed]

I am making a Tic Tac Toe game using Tkinter in Python, if you understand the game you will understand what is my error. I am trying to get any empty buttons which where not filled with X and the code chooses a random button of those and fill them with O for the computer to play. I did that but it did not work. Here I need to check my buttons list 1 by one and sort which one is empty I mean text is not filled.

Related code:


# Importing the necessary libraries
import tkinter
import random


# Tkinter
window = tkinter.Tk()
window.title("Tic Tac Toe")
window.geometry("400x400")


# Game PC function
def game_pc():
    # create 9 tkinter buttons
    b1 = tkinter.Button(window, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda: btn_click(b1))
    b1.grid(row=1, column=0)
    b2 = tkinter.Button(window, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda: btn_click(b2))
    b2.grid(row=1, column=1)
    b3 = tkinter.Button(window, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda: btn_click(b3))
    b3.grid(row=1, column=2)
    b4 = tkinter.Button(window, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda: btn_click(b4))
    b4.grid(row=2, column=0)
    b5 = tkinter.Button(window, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda: btn_click(b5))
    b5.grid(row=2, column=1)
    b6 = tkinter.Button(window, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda: btn_click(b6))
    b6.grid(row=2, column=2)
    b7 = tkinter.Button(window, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda: btn_click(b7))
    b7.grid(row=3, column=0)
    b8 = tkinter.Button(window, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda: btn_click(b8))
    b8.grid(row=3, column=1)
    b9 = tkinter.Button(window, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda: btn_click(b9))
    b9.grid(row=3, column=2)
    
    
    # create a list to store the buttons
    buttons = [b1, b2, b3, b4, b5, b6, b7, b8, b9]
    # create a list to store the values of the buttons
    values = []
    # when button clicked, it puts X in the button
    def btn_click(buttons):
        # fill any empty random button with O
        # create a list of empty buttons
        empty_buttons = []
        # check if button is empty
        for button in buttons:
            if button['text'] == " ":
                empty_buttons.append(button)
        # choose a random button from the list of empty buttons
        r = random.choice(empty_buttons)
        # put O in the random button
        r.config(text="O")

        
    window.mainloop()
        






game_pc()

Error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\osedk\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:\Users\osedk\Desktop\tictactoe\game.py", line 33, in <lambda>
    b5 = tkinter.Button(window, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda: btn_click(b5))
  File "c:\Users\osedk\Desktop\tictactoe\game.py", line 117, in btn_click
    for button in buttons:
  File "C:\Users\osedk\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1681, in cget
    return self.tk.call(self._w, 'cget', '-' + key)
TypeError: can only concatenate str (not "int") to str

Full code




Aucun commentaire:

Enregistrer un commentaire