I am trying to make 3 turn to "o" after pressing 1 or 2. However, I encounter some obsticles, no matter how i try, it just wont work and i just cant firgure out wheres the problem. I would be very thankful if anyone can help me out with it.
After 1 or 2 is pressed, it will turn into "x". Then, the computer should choose the number 3and turn it to "o", but there is no changes.
import tkinter as tk
from tkinter import Button, ttk
import random
win = tk.Tk()
win.title("Python GUI")
list=["1","2","3"]
ai=[1,2,3]
start = 0
def X1():
a1["text"]="x"
global start
start+=1
global list
list[0] = "x"
global order
order=(list.count("x"))%2
global ai
ai.remove(1)
print(order)
def X2():
a2["text"]="x"
global start
start+=1
global list
list[1] = "x"
global order
order=(list.count("x"))%2
global ai
ai.remove(2)
print(order)
def X3():
a3["text"]="x"
global start
start+=1
global list
list[2] = "x"
global order
order=(list.count("x"))%2
global ai
ai.remove(3)
print(order)
def O1():
ao1["text"]="O"
global order
order=(list.count("x"))%2
def O2():
ao2["text"]="O"
global order
order=(list.count("x"))%2
def O3():
ao3["text"]="O"
global order
order=(list.count("x"))%2
aii=3
game=0
order=0
win.resizable(False, False)
list=["1","2","3"]
if order!=0 or start==0:
a1 = ttk.Button(text="1", width=2,command=X1)
a1.grid(column=0, row=0)
if order!=0 and aii==1:
ao1 = ttk.Button(text="1", width=2,command=O1)
ao1.grid(column=0, row=0)
ao1.invoke()
if order==0 or start==0:
a2 = ttk.Button(text="2", width=2,command=X2)
a2.grid(column=1, row=0)
if order!=0 and aii==2 :
ao2 = ttk.Button(text="2", width=2,command= O2)
ao2.grid(column=1, row=0)
ao2.invoke()
if order==0 or start==0:
a3 = ttk.Button(text="3", width=2,command=X3)
a3.grid(column=2, row=1)
if order!=0 and aii==3:
ao3 = ttk.Button(text="4", width=2,command=O3)
ao3.grid(column=2, row=0)
ao3.invoke()
win.mainloop()
It was a good effort in the original question.
However, I can achieve what you need in a much cleaner way.
firstly, the use of the global
keyword is poor practice and usually indicates that a class is needed. Which is a neater way of passing variables.
Secondly, I have thrown in a bit of typehinting to make the code clearer. For example, the list is actually a List
of Button
.
Finally, the code is set to turn the text on the button into an "o" for every third click.
You can tkinker and tweek this as you like.
Here is my version of the same code:
import tkinter as tk
from typing import List
from tkinter import Button
class ClickCounterApp:
def __init__(self, master: tk.Tk):
self.master = master
self.clicker = 0
self.buttons: List[Button] = []
# Set the title of the window
self.master.title("Button Click Example")
# Set the width of the window to accommodate the title
self.master.geometry("300x200")
# Create three button widgets
for _ in range(3):
button = tk.Button(self.master, text="x")
button.config(command=lambda b=button: self.button_click(b))
self.buttons.append(button)
# Pack the button widgets into the window
for button in self.buttons:
button.pack()
# Function to execute when any of the buttons is clicked
def button_click(self, button: Button):
self.clicker += 1
if self.clicker % 3 == 0:
button.config(text="o")
else:
button.config(text="Button is clicked ({})".format(self.clicker))
# Create a new Tkinter window
window = tk.Tk()
# Create an instance of the ClickCounterApp class
app = ClickCounterApp(window)
# Start the Tkinter event loop
window.mainloop()
My output screen looks like this: