CodingGuyTutorials

Tic-Tac-Toe | Tkinter | Python

Feb 1st, 2020
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.85 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox
  3.  
  4. def btnClicked(button):
  5.     global pturn, turns, turn
  6.     if button["text"] == " " and turns % 2 == 1:
  7.         turns += 1
  8.         button["text"] = "X"
  9.         pturn["text"] = "Waiting on: " + p2.get()
  10.         turn["text"] = "Turn: " + str(turns)
  11.     elif button["text"] == " " and turns % 2 == 0:
  12.         turns += 1
  13.         button["text"] = "O"
  14.         pturn["text"] = "Waiting on: " + p1.get()
  15.         turn["text"] = "Turn: " + str(turns)
  16.     else:
  17.         messagebox.showerror("Error", "That tile has already been chosen!")
  18.     if checkWin == False:
  19.         win()
  20.  
  21. def win():
  22.     global b1; global b2;  global b3; global b4; global b5; global b6; global b7; global b8; global b9;
  23.     global checkWin, turns
  24.     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":
  25.         messagebox.showinfo("Win", p1.get() + " has won!")
  26.         checkWin = True
  27.         turns += 1
  28.     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":
  29.         messagebox.showinfo("Win", p2.get() + " has won!")
  30.         checkWin = True
  31.         turns += 1
  32.     elif turns == 10:
  33.         turns = 9
  34.         turn["text"] = "Turn: " + str(turns)
  35.         messagebox.showinfo("Draw", "It's a draw!")
  36.         checkWin = True
  37.     if checkWin:
  38.         b1["state"] = "disabled"
  39.         b2["state"] = "disabled"
  40.         b3["state"] = "disabled"
  41.         b4["state"] = "disabled"
  42.         b5["state"] = "disabled"
  43.         b6["state"] = "disabled"
  44.         b7["state"] = "disabled"
  45.         b8["state"] = "disabled"
  46.         b9["state"] = "disabled"
  47.        
  48. def main():
  49.     global root
  50.     root = tk.Tk()
  51.     root.title("Tic-Tac-Toe")
  52.     tk.Label(text = "Tic-Tac-Toe", bg = "grey47", width = "30", height = "2", font = ("Calibri", 13)).pack()
  53.     tk.Label(text = "").pack()
  54.     tk.Button(text = "Player vs Player", height = "2", width = "30", command = names).pack()
  55.     tk.Label(text = "").pack()
  56.     tk.Button(text = "Player vs Computer",height = "2", width = "30").pack()
  57.     tk.Label(text = "").pack()
  58.     root.mainloop()
  59.  
  60. def names():
  61.     global names
  62.     names = tk.Toplevel(root)
  63.     names.title("Player Names")
  64.     global p1
  65.     global p2
  66.     p1 = tk.StringVar()
  67.     p2 = tk.StringVar()
  68.     global p1_entry
  69.     global p2_entry
  70.     tk.Label(names, text = "Player 1:", font = ("Calibri", 20)).grid(row = 1, column = 1)
  71.     tk.Entry(names, textvariable = p1, font = ("Calibri", 20)).grid(row = 1, column = 2)
  72.     tk.Label(names, text = "Player 2:", font = ("Calibri", 20)).grid(row = 2, column = 1)
  73.     tk.Entry(names, textvariable = p2, font = ("Calibri", 20)).grid(row = 2, column = 2)
  74.     tk.Button(names, text = "Submit", font = ("Calibri", 13), command = verifynames).grid(row = 3, column = 2, sticky = tk.E)
  75.  
  76. def verifynames():
  77.     if p1.get() == "" or p2.get() == "":
  78.         messagebox.showerror("Error", "Don't leave any spaces!")
  79.     else:
  80.         pvp()
  81.  
  82. def pvp():
  83.     names.destroy()
  84.     w = 8
  85.     h = 4
  86.     pvp = tk.Toplevel(root)
  87.     pvp.title("Tic-Tac-Toe")
  88.     global pturn, turns, turn, checkWin
  89.     pturn = tk.Label(pvp, text = "Waiting on: " + p1.get(), font = ("Calibri", 28))
  90.     turns = 1
  91.     turn = tk.Label(pvp, text = "Turn: " + str(turns), font = ("Calibri", 28))
  92.     checkWin = False
  93.     pturn.grid(row = 1, column = 1, columnspan = 2, sticky = tk.W)
  94.     turn.grid(row = 1, column = 3, sticky = tk.E)
  95.     global b1; global b2;  global b3; global b4; global b5; global b6; global b7; global b8; global b9;
  96.     b1 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b1))
  97.     b1.grid(row = 2, column = 1)
  98.     b2 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b2))
  99.     b2.grid(row = 2, column = 2)
  100.     b3 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b3))
  101.     b3.grid(row = 2, column = 3)
  102.     b4 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b4))
  103.     b4.grid(row = 3, column = 1)
  104.     b5 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b5))
  105.     b5.grid(row = 3, column = 2)
  106.     b6 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b6))
  107.     b6.grid(row = 3, column = 3)
  108.     b7 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b7))
  109.     b7.grid(row = 4, column = 1)
  110.     b8 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b8))
  111.     b8.grid(row = 4, column = 2)
  112.     b9 = tk.Button(pvp, text = " ", font = ("Calibri", 32), bg = "grey47", width = w, height = h, command = lambda: btnClicked(b9))
  113.     b9.grid(row = 4, column = 3)
  114.  
  115. main()
Advertisement
Add Comment
Please, Sign In to add comment