These are the entry fields and the buttons
I want to enter "X" and "O" in these different entry fields with the 2 buttons but I am only able to enter in the 1st box by calling a function. How do I do the same with other boxes? Like if i have my cursor in 5th box i want to enter the sign from the button in 5th box.
Code:
from tkinter import *
root=Tk()
e1=Entry(root,font=("arial",50),width=2,borderwidth=5)
e2=Entry(root,font=("arial",50),width=2,borderwidth=5)
e3=Entry(root,font=("arial",50),width=2,borderwidth=5)
e4=Entry(root,font=("arial",50),width=2,borderwidth=5)
e5=Entry(root,font=("arial",50),width=2,borderwidth=5)
e6=Entry(root,font=("arial",50),width=2,borderwidth=5)
e7=Entry(root,font=("arial",50),width=2,borderwidth=5)
e8=Entry(root,font=("arial",50),width=2,borderwidth=5)
e9=Entry(root,font=("arial",50),width=2,borderwidth=5)
def enterX(sign):
e1.delete(0,END)
e1.insert(0,sign)
def enterO(sign):
e1.delete(0,END)
e1.insert(0,sign)
b1=Button(root,text=("X"),command=lambda: enterX("X"),fg="blue")
b2=Button(root,text=("O"),command=lambda: enterO("O"),fg="red")
b3=Button(root,text="Exit Program",command=root.quit)
e1.grid(row=0 , column=0,padx=5,pady=20)
e2.grid(row=0 , column=1,padx=5,pady=20)
e3.grid(row=0 , column=2,padx=5,pady=20)
e4.grid(row=1 , column=0,padx=5,pady=20)
e5.grid(row=1 , column=1,padx=5,pady=20)
e6.grid(row=1 , column=2,padx=5,pady=20)
e7.grid(row=2 , column=0,padx=5,pady=20)
e8.grid(row=2 , column=1,padx=5,pady=20)
e9.grid(row=2 , column=2,padx=5,pady=20)
b1.grid(row=3,column=0)
b3.grid(row=4,column=1)
b2.grid(row=3,column=2)
root.mainloop()
Your button can only have one command. I havent really tried that, but you can maybe set the command to something else as soon as you press it. So an option for you may be to track where your cursor is, then have a loop checking constantly at what position your cursor is at and when you then click a button it gets the current position of the cursor, depending on that it changes the command of the button in order to place the X or O where your cursor is at.
If that is too complicated you could also just create 9 buttons and have a variable called active_player or player_turn or something like that. If that is equal to 1 player 1 is on the move and if its 2 its the move of player two. When any of the players then clicks one of the buttons you just check whose move it is and then place either an X or an O and change the active_player or player_turn method so its the other players move.