Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 拖放兵_Python_Tkinter_Canvas_Bind - Fatal编程技术网

Python 拖放兵

Python 拖放兵,python,tkinter,canvas,bind,Python,Tkinter,Canvas,Bind,我有一个棋盘。我会有兴趣在不同的情况下拖拽每一个棋子,但我不知道如何才能做到这一点。我曾想过使用我的_canvas.bind、move,但我不确定如何在上面的代码中使用它。现在,我只能看到不同的白色和黑色,无法移动它们 如何在棋盘上拖放棋子?您所要做的就是从您在问题的早期版本中链接到的问题中投票率最高的答案中复制一些代码,然后调整绑定以处理标记块而不是令牌 首先,将函数drag_start、drag_stop和drag_stop从代码中复制到CanvasChecker类中。在复制的代码中将sel

我有一个棋盘。我会有兴趣在不同的情况下拖拽每一个棋子,但我不知道如何才能做到这一点。我曾想过使用我的_canvas.bind、move,但我不确定如何在上面的代码中使用它。现在,我只能看到不同的白色和黑色,无法移动它们


如何在棋盘上拖放棋子?

您所要做的就是从您在问题的早期版本中链接到的问题中投票率最高的答案中复制一些代码,然后调整绑定以处理标记块而不是令牌

首先,将函数drag_start、drag_stop和drag_stop从代码中复制到CanvasChecker类中。在复制的代码中将self.canvas更改为self

接下来,在_init__函数中的某个位置添加此行:

from tkinter import Tk, Canvas, Label, NSEW

class CanvasChecker(Canvas):

    def __init__(self, parent, n_pixels_per_case=60):

        self.checker = {(7, 0): 'o', (7, 2): 'o', (7, 4): 'o', (7, 6): 'o', (6, 1): 'o', 
                (6, 3): 'o', (6, 5): 'o', (6, 7): 'o', (5, 0): 'o', (5, 2): 'o', (5, 4): 'o',
                (5, 6): 'o', (2, 1): 'x', (2, 3): 'x', (2, 5): 'x', (2, 7): 'x', (1, 0): 'x',
                (1, 2): 'x', (1, 4): 'x', (1, 6): 'x', (0, 1): 'x', (0, 3): 'x', (0, 5): 'x',
                (0, 7): 'x'}

        self.n_pixels_per_case = n_pixels_per_case

        width = 8 * n_pixels_per_case
        height = 8 * n_pixels_per_case

        super().__init__(parent, width=width, height=height)

        self.bind('<Configure>', self.redimension)

    def draw_cases(self):
        for i in range(8):
            for j in range(8):
                beg_line = i * self.n_pixels_per_case
                end_line = beg_line + self.n_pixels_per_case
                beg_column = j * self.n_pixels_per_case
                end_column = beg_column + self.n_pixels_per_case

                if (i + j) % 2 == 0:
                    color = '#E88515'
                color = '#DDDDFF'

                self.create_rectangle(beg_column, beg_line, end_column, end_line, fill=color, tags='case')

    def draw_pieces(self):
        for position in self.checker:
            coordinate_y = position[0] * self.n_pixels_per_case + self.n_pixels_per_case // 2
            coordinate_x = position[1] * self.n_pixels_per_case + self.n_pixels_per_case // 2

            piece = self.checker[position]

            if piece == 'o':
                icon = "\u26C0"
            elif piece == 'O':
                icon = "\u26C1"
            elif piece == 'x':
                icon = "\u26C2"
            else:
                icon = "\u26C3"

            character_police = ('Already Seen', self.n_pixels_per_case//2)
            self.create_text(coordinate_x, coordinate_y, text=icon, font=character_police, tags='piece')

    def redimension(self, event):

        new_size = min(event.width, event.height)
        self.n_pixels_per_case = new_size // 8
        self.actualise()

    def actualise(self):
        self.delete('case')
        self.draw_cases()
        self.delete('piece')
        self.draw_pieces()


class Game(Tk):

    def __init__(self):
        super().__init__()

        self.canvas_checker = CanvasChecker(self, 60)
        self.canvas_checker.grid(sticky=NSEW)

        self.messages = Label(self)
        self.messages.grid()

        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)


if __name__ == '__main__':
    window = Game()
    window.mainloop()
最后,在_init__;中添加以下绑定:

您需要添加逻辑来确定移动是否合法,但这回答了如何使用鼠标移动工件的问题


@布莱恩·奥克利这是一个糟糕的答案,因为你只是在指导OP做什么,而没有详细说明。@Space:为什么?如果您让我知道,也许我可以把答案做得更好。@Space:精化在复制代码的内联注释中。OP知道存在一个更好的答案,但在将代码应用到他们的代码时有某种心理障碍。从他们对评论的回答中似乎很清楚,他们永远也不会明白。我知道OP已经链接到他们删除的答案,你不必解释游戏的机制,而是解释他们的代码失败的原因和地方。@Space:他们的代码中没有失败的地方。即使在我询问之后,他们也拒绝显示试图移动对象的代码。
self._drag_data = {"x": 0, "y": 0, "item": None}
self.tag_bind("piece", "<ButtonPress-1>", self.drag_start)
self.tag_bind("piece", "<ButtonRelease-1>", self.drag_stop)
self.tag_bind("piece", "<B1-Motion>", self.drag)
from tkinter import Tk, Canvas, Label, NSEW

class CanvasChecker(Canvas):

    def __init__(self, parent, n_pixels_per_case=60):

        self.checker = {(7, 0): 'o', (7, 2): 'o', (7, 4): 'o', (7, 6): 'o', (6, 1): 'o',
                (6, 3): 'o', (6, 5): 'o', (6, 7): 'o', (5, 0): 'o', (5, 2): 'o', (5, 4): 'o',
                (5, 6): 'o', (2, 1): 'x', (2, 3): 'x', (2, 5): 'x', (2, 7): 'x', (1, 0): 'x',
                (1, 2): 'x', (1, 4): 'x', (1, 6): 'x', (0, 1): 'x', (0, 3): 'x', (0, 5): 'x',
                (0, 7): 'x'}

        self.n_pixels_per_case = n_pixels_per_case

        width = 8 * n_pixels_per_case
        height = 8 * n_pixels_per_case

        super().__init__(parent, width=width, height=height)

        self.bind('<Configure>', self.redimension)

        self._drag_data = {"x": 0, "y": 0, "item": None}
        self.tag_bind("piece", "<ButtonPress-1>", self.drag_start)
        self.tag_bind("piece", "<ButtonRelease-1>", self.drag_stop)
        self.tag_bind("piece", "<B1-Motion>", self.drag)


    def draw_cases(self):
        for i in range(8):
            for j in range(8):
                beg_line = i * self.n_pixels_per_case
                end_line = beg_line + self.n_pixels_per_case
                beg_column = j * self.n_pixels_per_case
                end_column = beg_column + self.n_pixels_per_case

                if (i + j) % 2 == 0:
                    color = '#E88515'
                color = '#DDDDFF'

                self.create_rectangle(beg_column, beg_line, end_column, end_line, fill=color, tags='case')

    def draw_pieces(self):
        for position in self.checker:
            coordinate_y = position[0] * self.n_pixels_per_case + self.n_pixels_per_case // 2
            coordinate_x = position[1] * self.n_pixels_per_case + self.n_pixels_per_case // 2

            piece = self.checker[position]

            if piece == 'o':
                icon = "\u26C0"
            elif piece == 'O':
                icon = "\u26C1"
            elif piece == 'x':
                icon = "\u26C2"
            else:
                icon = "\u26C3"

            character_police = ('Already Seen', self.n_pixels_per_case//2)
            self.create_text(coordinate_x, coordinate_y, text=icon, font=character_police, tags='piece')

    def redimension(self, event):

        new_size = min(event.width, event.height)
        self.n_pixels_per_case = new_size // 8
        self.actualise()

    def actualise(self):
        self.delete('case')
        self.draw_cases()
        self.delete('piece')
        self.draw_pieces()

    def drag_start(self, event):
        """Begining drag of an object"""
        # record the item and its location
        self._drag_data["item"] = self.find_closest(event.x, event.y)[0]
        self._drag_data["x"] = event.x
        self._drag_data["y"] = event.y

    def drag_stop(self, event):
        """End drag of an object"""
        # reset the drag information
        self._drag_data["item"] = None
        self._drag_data["x"] = 0
        self._drag_data["y"] = 0

    def drag(self, event):
        """Handle dragging of an object"""
        # compute how much the mouse has moved
        delta_x = event.x - self._drag_data["x"]
        delta_y = event.y - self._drag_data["y"]
        # move the object the appropriate amount
        self.move(self._drag_data["item"], delta_x, delta_y)
        # record the new position
        self._drag_data["x"] = event.x
        self._drag_data["y"] = event.y

class Game(Tk):

    def __init__(self):
        super().__init__()

        self.canvas_checker = CanvasChecker(self, 60)
        self.canvas_checker.grid(sticky=NSEW)

        self.messages = Label(self)
        self.messages.grid()

        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)


if __name__ == '__main__':
    window = Game()
    window.mainloop()