Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox
- def btnClicked(button):
- global pturn, turns, turn
- if button["text"] == " " and turns % 2 == 1:
- turns += 1
- button["text"] = "X"
- pturn["text"] = "Waiting on: " + p2.get()
- turn["text"] = "Turn: " + str(turns)
- elif button["text"] == " " and turns % 2 == 0:
- turns += 1
- button["text"] = "O"
- pturn["text"] = "Waiting on: " + p1.get()
- turn["text"] = "Turn: " + str(turns)
- else:
- messagebox.showerror("Error", "That tile has already been chosen!")
- if checkWin == False:
- win()
- def win():
- global b1; global b2; global b3; global b4; global b5; global b6; global b7; global b8; global b9;
- global checkWin, turns
- if b1["text"] == "X" and b2["text"] == "X" and b3["text"] == "X" or b4["text"] == "X" and b5["text"] == "X" and b6["text"] == "X" or b7["text"] == "X" and b8["text"] == "X" and b9["text"] == "X" or b1["text"] == "X" and b4["text"] == "X" and b7["text"] == "X" or b2["text"] == "X" and b5["text"] == "X" and b8["text"] == "X" or b3["text"] == "X" and b6["text"] == "X" and b9["text"] == "X" or b1["text"] == "X" and b5["text"] == "X" and b9["text"] == "X" or b3["text"] == "X" and b5["text"] == "X" and b7["text"] == "X":
- messagebox.showinfo("Win", p1.get() + " has won!")
- checkWin = True
- turns += 1
- elif b1["text"] == "O" and b2["text"] == "O" and b3["text"] == "O" or b4["text"] == "O" and b5["text"] == "O" and b6["text"] == "O" or b7["text"] == "O" and b8["text"] == "O" and b9["text"] == "O" or b1["text"] == "O" and b4["text"] == "O" and b7["text"] == "O" or b2["text"] == "O" and b5["text"] == "O" and b8["text"] == "O" or b3["text"] == "O" and b6["text"] == "O" and b9["text"] == "O" or b1["text"] == "O" and b5["text"] == "O" and b9["text"] == "O" or b3["text"] == "O" and b5["text"] == "O" and b7["text"] == "O":
- messagebox.showinfo("Win", p2.get() + " has won!")
- checkWin = True
- turns += 1
- elif turns == 10:
- turns = 9
- turn["text"] = "Turn: " + str(turns)
- messagebox.showinfo("Draw", "It's a draw!")
- checkWin = True
- if checkWin:
- b1["state"] = "disabled"
- b2["state"] = "disabled"
- b3["state"] = "disabled"
- b4["state"] = "disabled"
- b5["state"] = "disabled"
- b6["state"] = "disabled"
- b7["state"] = "disabled"
- b8["state"] = "disabled"
- b9["state"] = "disabled"
- def main():
- global root
- root = tk.Tk()
- root.title("Tic-Tac-Toe")
- tk.Label(text = "Tic-Tac-Toe", bg = "grey47", width = "30", height = "2", font = ("Calibri", 13)).pack()
- tk.Label(text = "").pack()
- tk.Button(text = "Player vs Player", height = "2", width = "30", command = names).pack()
- tk.Label(text = "").pack()
- tk.Button(text = "Player vs Computer",height = "2", width = "30").pack()
- tk.Label(text = "").pack()
- root.mainloop()
- def names():
- global names
- names = tk.Toplevel(root)
- names.title("Player Names")
- global p1
- global p2
- p1 = tk.StringVar()
- p2 = tk.StringVar()
- global p1_entry
- global p2_entry
- tk.Label(names, text = "Player 1:", font = ("Calibri", 20)).grid(row = 1, column = 1)
- tk.Entry(names, textvariable = p1, font = ("Calibri", 20)).grid(row = 1, column = 2)
- tk.Label(names, text = "Player 2:", font = ("Calibri", 20)).grid(row = 2, column = 1)
- tk.Entry(names, textvariable = p2, font = ("Calibri", 20)).grid(row = 2, column = 2)
- tk.Button(names, text = "Submit", font = ("Calibri", 13), command = verifynames).grid(row = 3, column = 2, sticky = tk.E)
- def verifynames():
- if p1.get() == "" or p2.get() == "":
- messagebox.showerror("Error", "Don't leave any spaces!")
- else:
- pvp()
- def pvp():
- names.destroy()
- w = 8
- h = 4
- pvp = tk.Toplevel(root)
- pvp.title("Tic-Tac-Toe")
- global pturn, turns, turn, checkWin
- pturn = tk.Label(pvp, text = "Waiting on: " + p1.get(), font = ("Calibri", 28))
- turns = 1
- turn = tk.Label(pvp, text = "Turn: " + str(turns), font = ("Calibri", 28))
- checkWin = False
- pturn.grid(row = 1, column = 1, columnspan = 2, sticky = tk.W)
- turn.grid(row = 1, column = 3, sticky = tk.E)
- global b1; global b2; global b3; global b4; global b5; global b6; global b7; global b8; global b9;
- b1 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b1))
- b1.grid(row = 2, column = 1)
- b2 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b2))
- b2.grid(row = 2, column = 2)
- b3 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b3))
- b3.grid(row = 2, column = 3)
- b4 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b4))
- b4.grid(row = 3, column = 1)
- b5 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b5))
- b5.grid(row = 3, column = 2)
- b6 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b6))
- b6.grid(row = 3, column = 3)
- b7 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b7))
- b7.grid(row = 4, column = 1)
- b8 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b8))
- b8.grid(row = 4, column = 2)
- b9 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b9))
- b9.grid(row = 4, column = 3)
- main()
Advertisement
Add Comment
Please, Sign In to add comment