Home > Net >  Tkinter Checkbutton Not Working Even After Clicked
Tkinter Checkbutton Not Working Even After Clicked

Time:12-29

I am writing a simple tic-tac-toe game that anyone can win. The player will have to put an X on the board, and check the "I confirm my move." check button. Once the check button is checked, the program jumps into a checking process. If the X is placed at the correct cell, a messagebox pops up saying that that won. However, if they place the X at the wrong cell, a messagebox will pop up to mock the player. The problem I am having now is that the messageboxes do not show up even after the check button is clicked on. What went wrong? Thank you.

import tkinter as tk
from tkinter import messagebox

window=tk.Tk()
window.title('Tic-Tac-Toe')

def button1_clicked():
    button1.configure(text='X')
    return True
def button2_clicked():
    button2.configure(text='X')
    return True
def button3_clicked():
    button3.configure(text='X')
    return True
def button4_clicked():
    button4.configure(text='X')
    return True
def checked():
    if button1_clicked==True:
        messagebox.showinfo('Good job!','You just won a tic-tac-toe game that anyone can win.')
    if button2_clicked==True or button3_clicked==True or button4_clicked==True:  
        messagebox.showinfo('BRUH!','The only best move was obvious.')

t=tk.IntVar()
label1=tk.Label(window,text='X')
label2=tk.Label(window,text='O')
label3=tk.Label(window,text='O')
label4=tk.Label(window,text='X')
label5=tk.Label(window,text='O')
button1=tk.Button(window,text=' ',command=button1_clicked)
button2=tk.Button(window,text=' ',command=button2_clicked)
button3=tk.Button(window,text=' ',command=button3_clicked)
button4=tk.Button(window,text=' ',command=button4_clicked)
check=tk.Checkbutton(window,text='I confirm my move.',variable=t,command=checked)

label1.grid(row=0,column=0)
label2.grid(row=0,column=1)
label3.grid(row=0,column=2)
label4.grid(row=1,column=0)
label5.grid(row=1,column=1)
button1.grid(row=2,column=0)
button2.grid(row=2,column=2)
button3.grid(row=1,column=2)
button4.grid(row=2,column=1)
check.grid(row=3,column=1)

messagebox.showinfo('Instruction','This is a tic-tac-toe game that anyone can win.')
messagebox.showinfo('Instruction','Click on an empty cell to put X.')

window.mainloop()

CodePudding user response:

Messageboxes do not show up even after the check button is clicked on, because your button1_clicked,button2_clicked,.. etc. these conditions are not true, and you should call these function in this way

def checked():
    if button1_clicked()==True:
        messagebox.showinfo('Good job!','You just won a tic-tac-toe game that anyone can win.')
    if button2_clicked()==True or button3_clicked()==True or button4_clicked()==True:  
        messagebox.showinfo('BRUH!','The only best move was obvious.')

There should be "()" after the fucntion name.

CodePudding user response:

Returning True from buttons clicks serves no purpose.

Your checked() function is called when checkbox is clicked. In that function you check if button1_clicked and button2_clicked are true. However you have never set those buttonx_clicked values. So they cannot be true (nor false).

You have to check if text in yout button is X, because that is the only thing you change when you click those buttons:

if button1['text'] == "X":
    messagebox.showinfo('Good job!','You just won a tic-tac-toe game that anyone can win.')
if button2['text'] == "X" or button3['text'] == "X" or button4['text'] == "X":
    messagebox.showinfo('BRUH!','The only best move was obvious.')
  • Related