Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- flag = False
- winner = False
- def hod():
- global flag
- flag = not flag
- if flag:
- return 'x'
- else:
- return 'o'
- def f():
- xo = Tk()
- but = Button(xo, text=" ", width=10, height=5, font="Arial 16",
- bg="White", fg="Black", command=lambda: p(but))
- but2 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
- bg="White", fg="Black", command=lambda: p(but2))
- but3 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
- bg="White", fg="Black", command=lambda: p(but3))
- but4 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
- bg="White", fg="Black", command=lambda: p(but4))
- but5 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
- bg="White", fg="Black", command=lambda: p(but5))
- but6 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
- bg="White", fg="Black", command=lambda: p(but6))
- but7 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
- bg="White", fg="Black", command=lambda: p(but7))
- but8 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
- bg="White", fg="Black", command=lambda: p(but8))
- but9 = Button(xo, text=" ", width=10, height=5, font="Arial 16",
- bg="White", fg="Black", command=lambda: p(but9))
- but.grid(row=1, column=1)
- but2.grid(row=1, column=2)
- but3.grid(row=1, column=3)
- but4.grid(row=2, column=1)
- but5.grid(row=2, column=2)
- but6.grid(row=2, column=3)
- but7.grid(row=3, column=1)
- but8.grid(row=3, column=2)
- but9.grid(row=3, column=3)
- def p(button):
- if button["text"] == " " and not winner:
- button["text"] = hod()
- win()
- def win():
- global winner
- if but['text'] == but2['text'] == but3['text'] and but['text'] != ' ':
- but['bg'] = 'red'
- but2['bg'] = 'red'
- but3['bg'] = 'red'
- winner = True
- if but4['text'] == but5['text'] == but6['text'] and but4['text'] != ' ':
- but4['bg'] = 'red'
- but5['bg'] = 'red'
- but6['bg'] = 'red'
- winner = True
- if but7['text'] == but8['text'] == but9['text'] and but7['text'] != ' ':
- but7['bg'] = 'red'
- but8['bg'] = 'red'
- but9['bg'] = 'red'
- winner = True
- if but['text'] == but4['text'] == but7['text'] and but['text'] != ' ':
- but['bg'] = 'red'
- but4['bg'] = 'red'
- but7['bg'] = 'red'
- winner = True
- if but2['text'] == but5['text'] == but8['text'] and but2['text'] != ' ':
- but2['bg'] = 'red'
- but5['bg'] = 'red'
- but8['bg'] = 'red'
- winner = True
- if but3['text'] == but6['text'] == but9['text'] and but3['text'] != ' ':
- but3['bg'] = 'red'
- but6['bg'] = 'red'
- but9['bg'] = 'red'
- winner = True
- if but['text'] == but5['text'] == but9['text'] and but['text'] != ' ':
- but['bg'] = 'red'
- but5['bg'] = 'red'
- but9['bg'] = 'red'
- winner = True
- if but3['text'] == but5['text'] == but7['text'] and but3['text'] != ' ':
- but3['bg'] = 'red'
- but5['bg'] = 'red'
- but7['bg'] = 'red'
- winner = True
- xo.mainloop()
- f()
Add Comment
Please, Sign In to add comment