小部件Tkinter上方的小部件

时间:2019-07-12 13:07:42

标签: python-3.x tkinter

我正在尝试创建一个tictactoe GUI Tkinter游戏,我具有棋盘图像,但是很难进行编程,因此我没有添加棋盘图像,而是对所有必需的东西进行了编程,因此游戏运行良好,但是我现在想添加电路板,问题仍然更大,我在XO中使用了图像文件,因此它仍然变得很复杂,如果您希望我提供源代码,任何人都可以帮助我有点大。

import tkinter as tk
import tkinter.messagebox as messageBox
import time

grids = ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']

def checkWinner(grids):
    try:
        if ((grids[0] == 'x' and grids[1] == 'x' and grids[2] == 'x') or 
            (grids[3] == 'x' and grids[4] == 'x' and grids[5] == 'x') or 
            (grids[6] == 'x' and grids[7] == 'x' and grids[8] == 'x') or
            (grids[0] == 'x' and grids[3] == 'x' and grids[6] == 'x') or
            (grids[1] == 'x' and grids[4] == 'x' and grids[7] == 'x') or
            (grids[2] == 'x' and grids[5] == 'x' and grids[8] == 'x') or
            (grids[0] == 'x' and grids[4] == 'x' and grids[8] == 'x') or
            (grids[2] == 'x' and grids[4] == 'x' and grids[6] == 'x')):
            window.protocol('WM_DELETE_WINDOW', closeWindow('X'))
        elif ((grids[0] == 'o' and grids[1] == 'o' and grids[2] == 'o') or 
            (grids[3] == 'o' and grids[4] == 'o' and grids[5] == 'o') or 
            (grids[6] == 'o' and grids[7] == 'o' and grids[8] == 'o') or
            (grids[0] == 'o' and grids[3] == 'o' and grids[6] == 'o') or
            (grids[1] == 'o' and grids[4] == 'o' and grids[7] == 'o') or
            (grids[2] == 'o' and grids[5] == 'o' and grids[8] == 'o') or
            (grids[0] == 'o' and grids[4] == 'o' and grids[8] == 'o') or
            (grids[2] == 'o' and grids[4] == 'o' and grids[6] == 'o')):
            window.protocol('WM_DELETE_WINDOW', closeWindow('O'))
        elif counter == 9:
            window.protocol('WM_DELETE_WINDOW', closeWindow('Nobody'))
    except:
        pass

def clickOne(event):
    global frame_1
    global counter
    global statusBar
    if counter % 2 == 1:
        canves_1 = tk.Canvas(frame_1, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theX)
        grids[0] = 'x'
        checkWinner(grids)
    elif counter % 2 == 0:
        canves_1 = tk.Canvas(frame_1, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theO)
        grids[0] = 'o'
        checkWinner(grids)
    counter += 1
    if counter % 2 == 0:
        statusBar['text'] = "It's O's turn."
    else:
        statusBar['text'] = "It's X's turn."

def clickTwo(event):
    global frame_2
    global counter
    global statusBar
    if counter % 2 == 1:
        canves_1 = tk.Canvas(frame_2, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theX)
        grids[1] = 'x'
        checkWinner(grids)
    elif counter % 2 == 0:
        canves_1 = tk.Canvas(frame_2, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theO)
        grids[1] = 'o'
        checkWinner(grids)
    counter += 1
    if counter % 2 == 0:
        statusBar['text'] = "It's O's turn."
    else:
        statusBar['text'] = "It's X's turn."

def clickThree(event):
    global frame_3
    global counter
    global statusBar
    if counter % 2 == 1:
        canves_1 = tk.Canvas(frame_3, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theX)
        grids[2] = 'x'
        checkWinner(grids)
    elif counter % 2 == 0:
        canves_1 = tk.Canvas(frame_3, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theO)
        grids[2] = 'o'
        checkWinner(grids)
    counter += 1
    if counter % 2 == 0:
        statusBar['text'] = "It's O's turn."
    else:
        statusBar['text'] = "It's X's turn."

def clickFour(event):
    global frame_4
    global counter
    global statusBar
    if counter % 2 == 1:
        canves_1 = tk.Canvas(frame_4, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theX)
        grids[3] = 'x'
        checkWinner(grids)
    elif counter % 2 == 0:
        canves_1 = tk.Canvas(frame_4, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theO)
        grids[3] = 'o'
        checkWinner(grids)
    counter += 1
    if counter % 2 == 0:
        statusBar['text'] = "It's O's turn."
    else:
        statusBar['text'] = "It's X's turn."

def clickFive(event):
    global frame_5
    global counter
    global statusBar
    if counter % 2 == 1:
        canves_1 = tk.Canvas(frame_5, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theX)
        grids[4] = 'x'
        checkWinner(grids)
    elif counter % 2 == 0:
        canves_1 = tk.Canvas(frame_5, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theO)
        grids[4] = 'o'
        checkWinner(grids)
    counter += 1
    if counter % 2 == 0:
        statusBar['text'] = "It's O's turn."
    else:
        statusBar['text'] = "It's X's turn."

def clickSix(event):
    global frame_6
    global counter
    global statusBar
    if counter % 2 == 1:
        canves_1 = tk.Canvas(frame_6, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theX)
        grids[5] = 'x'
        checkWinner(grids)
    elif counter % 2 == 0:
        canves_1 = tk.Canvas(frame_6, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theO)
        grids[5] = 'o'
        checkWinner(grids)
    counter += 1
    if counter % 2 == 0:
        statusBar['text'] = "It's O's turn."
    else:
        statusBar['text'] = "It's X's turn."

def clickSeven(event):
    global frame_7
    global counter
    global statusBar
    if counter % 2 == 1:
        canves_1 = tk.Canvas(frame_7, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theX)
        grids[6] = 'x'
        checkWinner(grids)
    elif counter % 2 == 0:
        canves_1 = tk.Canvas(frame_7, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theO)
        grids[6] = 'o'
        checkWinner(grids)
    counter += 1
    if counter % 2 == 0:
        statusBar['text'] = "It's O's turn."
    else:
        statusBar['text'] = "It's X's turn."

def clickEight(event):
    global frame_8
    global counter
    global statusBar
    if counter % 2 == 1:
        canves_1 = tk.Canvas(frame_8, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theX)
        grids[7] = 'x'
        checkWinner(grids)
    elif counter % 2 == 0:
        canves_1 = tk.Canvas(frame_8, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theO)
        grids[7] = 'o'
        checkWinner(grids)
    counter += 1
    if counter % 2 == 0:
        statusBar['text'] = "It's O's turn."
    else:
        statusBar['text'] = "It's X's turn."

def clickNine(event):
    global frame_9
    global counter
    global statusBar
    if counter % 2 == 1:
        canves_1 = tk.Canvas(frame_9, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theX)
        grids[8] = 'x'
        checkWinner(grids)
    elif counter % 2 == 0:
        canves_1 = tk.Canvas(frame_9, width=83, height=83)
        canves_1.pack()
        canves_1.create_image(0, 0, anchor= tk.NW, image=theO)
        grids[8] = 'o'
        checkWinner(grids)
    counter += 1
    if counter % 2 == 0:
        statusBar['text'] = "It's O's turn."
    else:
        statusBar['text'] = "It's X's turn."

def closeWindow(winners):
    messageBox.askokcancel('Quit', f'{winners} won the game!')
    window.destroy()

window = tk.Tk()
window.resizable(False, False)
window.title('TicTacToe')
theBoard = tk.PhotoImage(file='E:\\KAMALESH\\Python_projects\\visual_studio_code\\TIC_TAC_TOE\\theBoard.gif') # The board image
theX = tk.PhotoImage(file='E:\\KAMALESH\\Python_projects\\visual_studio_code\\TIC_TAC_TOE\\X.gif') # The X image
theO = tk.PhotoImage(file='E:\\KAMALESH\\Python_projects\\visual_studio_code\\TIC_TAC_TOE\\O.gif') # The O image

counter = 1
# Frames
frame_1 = tk.Frame(window, height=83, width=83)
frame_1.bind("<Button-1>", clickOne)
frame_1.grid(row=0, column=0, sticky=tk.NW)

frame_2 = tk.Frame(window, height=83, width=83)
frame_2.bind("<Button-1>", clickTwo)
frame_2.grid(row=0, column=1, sticky=tk.N)

frame_3 = tk.Frame(window, height=83, width=83)
frame_3.bind("<Button-1>", clickThree)
frame_3.grid(row=0, column=2, sticky=tk.NE)

frame_4 = tk.Frame(window, height=83, width=83)
frame_4.bind("<Button-1>", clickFour)
frame_4.grid(row=1, column=0, sticky=tk.W)

frame_5 = tk.Frame(window, height=83, width=83)
frame_5.bind("<Button-1>", clickFive)
frame_5.grid(row=1, column=1)

frame_6 = tk.Frame(window, height=83, width=83)
frame_6.bind("<Button-1>", clickSix)
frame_6.grid(row=1, column=2, sticky=tk.E)

frame_7 = tk.Frame(window, height=83, width=83)
frame_7.bind("<Button-1>", clickSeven)
frame_7.grid(row=2, column=0, sticky=tk.SW)

frame_8 = tk.Frame(window, height=83, width=83)
frame_8.bind("<Button-1>", clickEight)
frame_8.grid(row=2, column=1, sticky=tk.S)

frame_9 = tk.Frame(window, height=83, width=83)
frame_9.bind("<Button-1>", clickNine)
frame_9.grid(row=2, column=2, sticky=tk.SE)

statusBar_frame = tk.Frame(window)
statusBar = tk.Label(statusBar_frame, text="TicTacToe game by Kamalesh Pathy V A", relief=tk.SUNKEN, anchor=tk.W)
statusBar.pack(fill="both")
statusBar_frame.grid(row=3, column=0, columnspan=3, sticky="ew")
tk.mainloop()
# print(grids)
# print(counter)

如果您不觉得无聊,请看一下。请不要将其标记为重复项,因为我需要对此进行自定义的答案。 我正在使用python 3.x,VS Code和Windows8

0 个答案:

没有答案