Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- from tkinter import Tk, Button, Label, Frame, Menu, StringVar, messagebox
- from functools import partial
- except ImportError:
- from Tkinter import Tk, Button, Label, Frame, Menu, StringVar, messagebox
- from fuctools import partial
- # For menubar to quit
- def quit_application():
- quit()
- # Check if three X or O in a row
- def is_won():
- if (label_list[0].cget('text') == 'X') & (label_list[1].cget('text') == 'X') & (label_list[2].cget('text') == 'X'):
- label_list[0].config(bg='green')
- label_list[1].config(bg='green')
- label_list[2].config(bg='green')
- show_winner('X')
- elif (label_list[3].cget('text') == 'X') & (label_list[4].cget('text') == 'X') & (label_list[5].cget('text') == 'X'):
- label_list[3].config(bg='green')
- label_list[4].config(bg='green')
- label_list[5].config(bg='green')
- show_winner('X')
- elif (label_list[6].cget('text') == 'X') & (label_list[7].cget('text') == 'X') & (label_list[8].cget('text') == 'X'):
- label_list[6].config(bg='green')
- label_list[7].config(bg='green')
- label_list[8].config(bg='green')
- show_winner('X')
- elif (label_list[0].cget('text') == 'X') & (label_list[3].cget('text') == 'X') & (label_list[6].cget('text') == 'X'):
- label_list[0].config(bg='green')
- label_list[3].config(bg='green')
- label_list[6].config(bg='green')
- show_winner('X')
- elif (label_list[1].cget('text') == 'X') & (label_list[4].cget('text') == 'X') & (label_list[7].cget('text') == 'X'):
- label_list[1].config(bg='green')
- label_list[4].config(bg='green')
- label_list[7].config(bg='green')
- show_winner('X')
- elif (label_list[2].cget('text') == 'X') & (label_list[5].cget('text') == 'X') & (label_list[8].cget('text') == 'X'):
- label_list[2].config(bg='green')
- label_list[5].config(bg='green')
- label_list[8].config(bg='green')
- show_winner('X')
- elif (label_list[0].cget('text') == 'X') & (label_list[4].cget('text') == 'X') & (label_list[8].cget('text') == 'X'):
- label_list[0].config(bg='green')
- label_list[4].config(bg='green')
- label_list[8].config(bg='green')
- show_winner('X')
- elif (label_list[2].cget('text') == 'X') & (label_list[4].cget('text') == 'X') & (label_list[6].cget('text') == 'X'):
- label_list[2].config(bg='green')
- label_list[4].config(bg='green')
- label_list[6].config(bg='green')
- show_winner('X')
- elif (label_list[0].cget('text') == 'O') & (label_list[1].cget('text') == 'O') & (label_list[2].cget('text') == 'O'):
- label_list[0].config(bg='green')
- label_list[1].config(bg='green')
- label_list[2].config(bg='green')
- show_winner('O')
- elif (label_list[3].cget('text') == 'O') & (label_list[4].cget('text') == 'O') & (label_list[5].cget('text') == 'O'):
- label_list[3].config(bg='green')
- label_list[4].config(bg='green')
- label_list[5].config(bg='green')
- show_winner('O')
- elif (label_list[6].cget('text') == 'O') & (label_list[7].cget('text') == 'O') & (label_list[8].cget('text') == 'O'):
- label_list[6].config(bg='green')
- label_list[7].config(bg='green')
- label_list[8].config(bg='green')
- show_winner('O')
- elif (label_list[0].cget('text') == 'O') & (label_list[3].cget('text') == 'O') & (label_list[6].cget('text') == 'O'):
- label_list[0].config(bg='green')
- label_list[3].config(bg='green')
- label_list[6].config(bg='green')
- show_winner('O')
- elif (label_list[1].cget('text') == 'O') & (label_list[4].cget('text') == 'O') & (label_list[7].cget('text') == 'O'):
- label_list[1].config(bg='green')
- label_list[4].config(bg='green')
- label_list[7].config(bg='green')
- show_winner('O')
- elif (label_list[2].cget('text') == 'O') & (label_list[5].cget('text') == 'O') & (label_list[8].cget('text') == 'O'):
- label_list[2].config(bg='green')
- label_list[5].config(bg='green')
- label_list[8].config(bg='green')
- show_winner('O')
- elif (label_list[0].cget('text') == 'O') & (label_list[4].cget('text') == 'O') & (label_list[8].cget('text') == 'O'):
- label_list[0].config(bg='green')
- label_list[4].config(bg='green')
- label_list[8].config(bg='green')
- show_winner('O')
- elif (label_list[2].cget('text') == 'O') & (label_list[4].cget('text') == 'O') & (label_list[6].cget('text') == 'O'):
- label_list[2].config(bg='green')
- label_list[4].config(bg='green')
- label_list[6].config(bg='green')
- show_winner('O')
- elif (
- ((label_list[0].cget('text') == 'O') or (label_list[0].cget('text') == 'X')) and
- ((label_list[1].cget('text') == 'O') or (label_list[1].cget('text') == 'X')) and
- ((label_list[2].cget('text') == 'O') or (label_list[2].cget('text') == 'X')) and
- ((label_list[3].cget('text') == 'O') or (label_list[3].cget('text') == 'X')) and
- ((label_list[4].cget('text') == 'O') or (label_list[4].cget('text') == 'X')) and
- ((label_list[5].cget('text') == 'O') or (label_list[5].cget('text') == 'X')) and
- ((label_list[6].cget('text') == 'O') or (label_list[6].cget('text') == 'X')) and
- ((label_list[7].cget('text') == 'O') or (label_list[7].cget('text') == 'X')) and
- ((label_list[8].cget('text') == 'O') or (label_list[8].cget('text') == 'X'))
- ):
- show_winner('No one')
- # Show winner window
- def show_winner(winner):
- messagebox.showinfo(title='Game is finished', message=f'{winner} has won the game.')
- root.destroy()
- # If pressed, change label to X and make buttons disappear
- def hide_button_x(n, label,):
- button_list_x[n].pack_forget()
- button_list_o[n].pack_forget()
- for m in range(len(label_list)):
- if label == label_list[m]:
- label_text_list[m].set('X')
- button_list_x[m]['state'] = 'disabled'
- button_list_o[m]['state'] = 'normal'
- is_won()
- # If pressed, change label to O and make buttons disappear
- def hide_button_o(n, label):
- button_list_x[n].pack_forget()
- button_list_o[n].pack_forget()
- for m in range(len(label_list)):
- if label == label_list[m]:
- label_text_list[m].set('O')
- button_list_x[m]['state'] = 'normal'
- button_list_o[m]['state'] = 'disabled'
- is_won()
- root = Tk()
- # Menubar to Quit the game
- menuBar = Menu(root)
- root.config(menu=menuBar)
- subMenuFile = Menu(menuBar, tearoff=0)
- menuBar.add_cascade(label='File', menu=subMenuFile)
- subMenuFile.add_command(label='Quit', command=quit_application)
- # Creating a 3x3 grid
- frame_list = []
- for i in range(9):
- frame_list.append(Frame(root))
- i = 0
- for j in range(3):
- for k in range(3):
- frame_list[i].grid(row=j, column=k)
- i += 1
- pass
- pass
- # Label text to display 'X' or 'O'
- label_text_list = []
- for i in range(9):
- label_text_list.append(StringVar())
- label_text_list[i].set('?')
- # Pack label to grid, above buttons
- label_list = []
- for i in range(9):
- label_list.append(Label(frame_list[i], textvariable=label_text_list[i]))
- for j in range(0, 2):
- for k in range(0, 2):
- label_list[i].pack(side='top')
- button_list_x = []
- button_list_o = []
- # Buttons to change label to X in grid
- for i in range(9):
- button_list_x.append(
- Button(
- frame_list[i],
- text='X',
- command=partial(hide_button_x, i, label_list[i]),
- state='normal'
- )
- )
- button_list_x[i].pack(side='left')
- # Buttons to chance label to O in grid
- for i in range(9):
- button_list_o.append(
- Button(
- frame_list[i],
- text='O',
- command=partial(hide_button_o, i, label_list[i]),
- state='disabled'
- )
- )
- button_list_o[i].pack(side='right')
- # Create the window
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment