I'm creating a simple TicTacToe GUI. Here is what I got so far:
import tkinter as tk
def initTable(master, rows, columns):
for i in range(0, columns):
button = tk.Button(master, text="", height=1, width=2)
button.grid(row=i)
for j in range(0, rows):
button2 = tk.Button(master, text="", height=1, width=2)
button2.grid(row=i, column=j)
if "__main__" == __name__:
print("Welcome to the TicTacToe game!")
print("Recommended size: 10x10")
correctinput = False
while True:
if correctinput:
print("The entered size is too big, please enter a smaller value")
height = int(input("Please enter the height (Max. 70)!"))
width = int(input("Please enter the width (Max. 70)!"))
correctinput = True
if height <= 70 and width <= 70: # This check is needed because it seems that you can't use any bigger
# table than this, it will freeze the computer...
break
master = tk.Tk()
master.resizable(width=False, height=False)
initTable(master, height, width)
master.mainloop()
So this creates the GUI with the width and height specified by the user. However, now I want to manage these buttons which is created seperately. For example if you press the left mouse button on a button created in the GUI it should display an X in that label. I found these to display it:
def leftclick(event):
print("leftclick")
def rightclick(event):
print("rightclick")
button.bind('<Button-1>', leftclick)
button.bind('<Button-3>', rightclick)
However I don't know how could I use this, because the buttons don't have unique names, etc... maybe with winfo_child()?