forked from shikaruki/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic_Tac_Toe_using_python_tkinter.py
267 lines (220 loc) · 10 KB
/
Tic_Tac_Toe_using_python_tkinter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Tic Tac Toe Game Using Python Tkinter
import tkinter.messagebox
from tkinter import *
# Window Creation.
win = Tk()
win.geometry("1150x800")
win.title("TIC TAC TOE")
win.configure(background="cyan")
# Inseting values of X and O in buttons
buttons = StringVar()
click = True
def butinsrt(buttons):
global click
if buttons["text"] == " " and click == True:
buttons["text"] = "X"
click = False
ScoreChecker("X", colorX)
elif buttons["text"] == " " and click == False:
buttons["text"] = "O"
click = True
ScoreChecker("O", colorO)
colorX = "light sky blue"
colorO = "salmon"
# Function to assign the winner
def ScoreChecker(var, colorXO):
if button1["text"] == var and button2["text"] == var and button3["text"] == var:
button1.configure(background=colorXO)
button2.configure(background=colorXO)
button3.configure(background=colorXO)
if var == "X":
s = float(XPlayer.get())
XPlayer.set(s + 1)
else:
s = float(OPlayer.get())
OPlayer.set(s + 1)
tkinter.messagebox.showinfo(f"Player {var} WINS.", f"Congratulations Player {var} for Winning The Round.")
Reset()
elif button4["text"] == var and button5["text"] == var and button6["text"] == var:
button4.configure(background=colorXO)
button5.configure(background=colorXO)
button6.configure(background=colorXO)
if var == "X":
s = float(XPlayer.get())
XPlayer.set(s + 1)
else:
s = float(OPlayer.get())
OPlayer.set(s + 1)
tkinter.messagebox.showinfo(f"Player {var} WINS.", f"Congratulations Player {var} for Winning The Round.")
Reset()
elif button7["text"] == var and button8["text"] == var and button9["text"] == var:
button7.configure(background=colorXO)
button8.configure(background=colorXO)
button9.configure(background=colorXO)
if var == "X":
s = float(XPlayer.get())
XPlayer.set(s + 1)
else:
s = float(OPlayer.get())
OPlayer.set(s + 1)
tkinter.messagebox.showinfo(f"Player {var} WINS.", f"Congratulations Player {var} for Winning The Round.")
Reset()
elif button1["text"] == var and button5["text"] == var and button9["text"] == var:
button1.configure(background=colorXO)
button5.configure(background=colorXO)
button9.configure(background=colorXO)
if var == "X":
s = float(XPlayer.get())
XPlayer.set(s + 1)
else:
s = float(OPlayer.get())
OPlayer.set(s + 1)
tkinter.messagebox.showinfo(f"Player {var} WINS.", f"Congratulations Player {var} for Winning The Round.")
Reset()
elif button3["text"] == var and button5["text"] == var and button7["text"] == var:
button3.configure(background=colorXO)
button5.configure(background=colorXO)
button7.configure(background=colorXO)
if var == "X":
s = float(XPlayer.get())
XPlayer.set(s + 1)
else:
s = float(OPlayer.get())
OPlayer.set(s + 1)
tkinter.messagebox.showinfo(f"Player {var} WINS.", f"Congratulations Player {var} for Winning The Round.")
Reset()
elif button1["text"] == var and button4["text"] == var and button7["text"] == var:
button1.configure(background=colorXO)
button4.configure(background=colorXO)
button7.configure(background=colorXO)
if var == "X":
s = float(XPlayer.get())
XPlayer.set(s + 1)
else:
s = float(OPlayer.get())
OPlayer.set(s + 1)
tkinter.messagebox.showinfo(f"Player {var} WINS.", f"Congratulations Player {var} for Winning The Round.")
Reset()
elif button2["text"] == var and button5["text"] == var and button8["text"] == var:
button2.configure(background=colorXO)
button5.configure(background=colorXO)
button8.configure(background=colorXO)
if var == "X":
s = float(XPlayer.get())
XPlayer.set(s + 1)
else:
s = float(OPlayer.get())
OPlayer.set(s + 1)
tkinter.messagebox.showinfo(f"Player {var} WINS.", f"Congratulations Player {var} for Winning The Round.")
Reset()
elif button3["text"] == var and button6["text"] == var and button9["text"] == var:
button3.configure(background=colorXO)
button6.configure(background=colorXO)
button9.configure(background=colorXO)
if var == "X":
s = float(XPlayer.get())
XPlayer.set(s + 1)
else:
s = float(OPlayer.get())
OPlayer.set(s + 1)
tkinter.messagebox.showinfo(f"Player {var} WINS.", f"Congratulations Player {var} for Winning The Round.")
Reset()
# Reset Function
def Reset():
button1["text"] = " "
button2["text"] = " "
button3["text"] = " "
button4["text"] = " "
button5["text"] = " "
button6["text"] = " "
button7["text"] = " "
button8["text"] = " "
button9["text"] = " "
button1.configure(background="lemon chiffon")
button2.configure(background="lemon chiffon")
button3.configure(background="lemon chiffon")
button4.configure(background="lemon chiffon")
button5.configure(background="lemon chiffon")
button6.configure(background="lemon chiffon")
button7.configure(background="lemon chiffon")
button8.configure(background="lemon chiffon")
button9.configure(background="lemon chiffon")
click = True
# New Game Function
def NewGame():
Reset()
XPlayer.set(0)
OPlayer.set(0)
# Frames
Top = Frame(win, bg="cyan", pady=2, width=1150, height=100, relief=RIDGE)
Top.grid(row=0, column=0)
Gtitle = Label(Top, font=("Times", 50, "bold"), text="Tic Tac Toe Game", bd=21, bg="cyan", fg="black", justify=CENTER)
Gtitle.grid(row=0, column=0)
MainFrame = Frame(win, bd=10, bg="green yellow", pady=2, width=1150, height=600, padx=2, relief=RIDGE)
MainFrame.grid(row=1, column=0)
LeftFrame = Frame(MainFrame, bd=10, width=650, height=500, pady=2, padx=2, bg="green yellow", relief=RIDGE)
LeftFrame.pack(side=LEFT)
RightFrame = Frame(MainFrame, bd=10, width=460, height=500, pady=2, padx=2, bg="green yellow", relief=RIDGE)
RightFrame.pack(side=RIGHT)
RightFrame1 = Frame(RightFrame, bd=10, width=460, height=300, pady=2, padx=2, bg="green yellow", relief=RIDGE)
RightFrame1.grid(row=0, column=0)
RightFrame2 = Frame(RightFrame, bd=10, width=460, height=100, pady=2, padx=2, bg="green yellow", relief=RIDGE)
RightFrame2.grid(row=1, column=0)
BottomFrame = Frame(win, bg="cyan", pady=2, width=1150, height=50, relief=RIDGE)
BottomFrame.grid(row=2, column=0)
Btitle = Label(BottomFrame, font=("Times", 20, "bold"), text="Created By: Tanmay Pandey", bd=21, bg="cyan", fg="black",
justify=CENTER)
Btitle.grid(row=0, column=0)
XPlayer = IntVar()
OPlayer = IntVar()
XPlayer.set(0)
OPlayer.set(0)
Score = Label(RightFrame1, font=("arial 40 bold"), text="Scores ", padx=2, pady=2, bg="green yellow", fg="violet red",
justify=CENTER)
Score.grid(row=0, column=0, sticky=W)
P1Title = Label(RightFrame1, font=("arial", 30, "bold"), text="Player X : ", padx=2, pady=2, bg="green yellow",
fg="orange red")
P1Title.grid(row=1, column=0, sticky=W)
TextPX = Entry(RightFrame1, font=("arial", 30, "bold"), bd=2, fg="black", textvariable=XPlayer, width=13, justify=LEFT)
TextPX.grid(row=1, column=1, sticky=W)
P2Title = Label(RightFrame1, font=("arial 30 bold"), text="Player O : ", padx=2, pady=2, bg="green yellow",
fg="orange red")
P2Title.grid(row=2, column=0, sticky=W)
TextPO = Entry(RightFrame1, font=("arial", 30, "bold"), bd=2, fg="black", textvariable=OPlayer, width=13, justify=LEFT)
TextPO.grid(row=2, column=1, sticky=W)
# Tic tac Toe Buttons
button1 = Button(LeftFrame, text=" ", font=("Times 26 bold"), height=3, width=8, bg="lemon chiffon",
command=lambda: butinsrt(button1))
button1.grid(row=1, column=0, sticky=S + N + E + W)
button2 = Button(LeftFrame, text=" ", font=("Times 26 bold"), height=3, width=8, bg="lemon chiffon",
command=lambda: butinsrt(button2))
button2.grid(row=1, column=1, sticky=S + N + E + W)
button3 = Button(LeftFrame, text=" ", font=("Times 26 bold"), height=3, width=8, bg="lemon chiffon",
command=lambda: butinsrt(button3))
button3.grid(row=1, column=2, sticky=S + N + E + W)
button4 = Button(LeftFrame, text=" ", font=("Times 26 bold"), height=3, width=8, bg="lemon chiffon",
command=lambda: butinsrt(button4))
button4.grid(row=2, column=0, sticky=S + N + E + W)
button5 = Button(LeftFrame, text=" ", font=("Times 26 bold"), height=3, width=8, bg="lemon chiffon",
command=lambda: butinsrt(button5))
button5.grid(row=2, column=1, sticky=S + N + E + W)
button6 = Button(LeftFrame, text=" ", font=("Times 26 bold"), height=3, width=8, bg="lemon chiffon",
command=lambda: butinsrt(button6))
button6.grid(row=2, column=2, sticky=S + N + E + W)
button7 = Button(LeftFrame, text=" ", font=("Times 26 bold"), height=3, width=8, bg="lemon chiffon",
command=lambda: butinsrt(button7))
button7.grid(row=3, column=0, sticky=S + N + E + W)
button8 = Button(LeftFrame, text=" ", font=("Times 26 bold"), height=3, width=8, bg="lemon chiffon",
command=lambda: butinsrt(button8))
button8.grid(row=3, column=1, sticky=S + N + E + W)
button9 = Button(LeftFrame, text=" ", font=("Times 26 bold"), height=3, width=8, bg="lemon chiffon",
command=lambda: butinsrt(button9))
button9.grid(row=3, column=2, sticky=S + N + E + W)
# New Game Button
button10 = Button(RightFrame2, text=" NEW GAME ", font=("Times 26 bold"), height=1, width=23, bg="lemon chiffon",
command=NewGame)
button10.grid(row=0, column=0)
button10 = Button(RightFrame2, text=" RESET ", font=("Times 26 bold"), height=1, width=23, bg="lemon chiffon",
command=Reset)
button10.grid(row=1, column=0)
win.mainloop()