在tkinter上,如何确定点击了哪些按钮?

时间:2017-08-18 10:00:06

标签: python python-3.x tkinter tic-tac-toe

我正在做一个大型的井字游戏。每当按下一个按钮时,我希望Gameplay类检查一行中是否有n个相同的标记(= win)。如果在类Box中激活方法move(),我无法弄清楚如何告诉类Gameplay。

对不起,我只是一个先发制人。这是我的代码。

from tkinter import *

# GRID MAXSIZE = 30*30


def game():
    n = [0]
    marks = ["X", "O"]
    points = [0,0]
    boxes = []


    class Box:
        def __init__(self, window, x, y):
            self.__x = x
            self.__y = y
            self.__state = ""
            self.__button = Button(window, text = self.__state, command = self.move, height = 1, width = 2)
        def move(self):
            if self.__state == "":
                self.__state = marks[n[0]]
                n[0] = (n[0]+1)%2
                self.__button.config(text = self.__state)

        def grid(self, **kwargs):
            self.__button.grid(**kwargs)
        def state(self):
            return self.__state

    class Gameplay:
        def __init__(self, size, towin):
            self.__mainwindow = Tk()
            self.__mainwindow.resizable(width=False, height=False)
            self.__mainlabel = Label(self.__mainwindow, text = "Se, joka saa " + str(towin) + " peräkkäin ensimmäisenä, voittaa!")
            self.__mainlabel.grid(row = 0, column = 0, columnspan = size)
            boxes = []
            for y in range(size):
                boxes.append([])
                for x in range(size):
                    boxes[y].append(Box(self.__mainwindow, x, y))
                    boxes[y][x].grid(row=y+1, column=x)
            self.__mainwindow.mainloop()





    playgame = Gameplay(20, 4)

game()

0 个答案:

没有答案