在类内的画布中创建图像

2024-10-03 04:31:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用OOP创建一个国际象棋程序。所以我创建了一个超类Pieces,一个子类Bishop,和一个UI类GameUI。我在GameUI类中创建了一个画布。我想,当我在类GameUI中实例化一个对象bishop时,它会在画布上显示一个bishop的图像

问题是,当我实例化Bishop时,我没有看到任何图像。因此,我尝试对文本执行相同的操作:我没有使用来自类Canvas的方法create_image,而是使用了create_text方法,它起了作用:我在画布上看到了一个文本。这意味着,问题来自方法create_image,我不理解它

如果我直接在类GameUi中创建一个图像,它就可以工作了!但那不是我想要的

所以我没有任何错误消息。我看到画布(蓝色背景),但上面没有图像

代码如下:

from tkinter import PhotoImage, Tk, Canvas


class Pieces:

    def __init__(self, can, color, x_position, y_position):
        self.color = color
        self.x_position = x_position
        self.y_position = y_position


class Bishop(Pieces):

    def __init__(self, can, color, x_position, y_position):

        super().__init__(can, color, x_position, y_position)

        if color == "black":
            icon_path = 'black_bishop.png'
        elif color == "white":
            icon_path = 'white_bishop.png'

        icon = PhotoImage(file=icon_path)  # doesn't see the image
        can.create_image(x, y, image=icon)


class GameUI:

    def __init__(self):

        self.windows = Tk()
        self.windows.title("My chess game")
        self.windows.geometry("1080x720")
        self.windows.minsize(300, 420)

        self.can = Canvas(self.windows, width=1000, height=600, bg='skyblue')
        
        icon = PhotoImage(file=icon_path)  # here I create the image in this class, and
        can.create_image(x, y, image=icon)  # we can see it very well

        self.bishop = Bishop(self.can, "black", 50, 50)
        self.can.pack()
        self.windows.mainloop()


app = GameUI()

Tags: 图像imageselfinitwindows画布createposition
1条回答
网友
1楼 · 发布于 2024-10-03 04:31:58

为了使代码正常工作,我决定基于this answer重写它。它现在可以工作了,但实际上您需要添加的唯一内容是self.icon,而不是iconicon获取垃圾回收,因为没有对它的进一步引用,而self.icon仍然存在。而且,它和你的不完全一样,所以可能也需要重写一下

from tkinter import *
from random import randint

class Piece:
    def __init__(self, canvas, x1, y1):
        self.x1 = x1
        self.y1 = y1
        self.canvas = canvas

class Bishop(Piece):
    def __init__(self, canvas, x1, y1, color):
        super().__init__(canvas, x1, y1)

        if color == "black":
            icon_path = 'black_bishop.png'
        elif color == "white":
            icon_path = 'white_bishop.png'

        self.icon = PhotoImage(file=icon_path)
        self.ball = canvas.create_image(self.x1, self.y1, image=self.icon)

    def move_piece(self):
        deltax = randint(0,5)
        deltay = randint(0,5)
        self.canvas.move(self.ball, deltax, deltay)
        self.canvas.after(50, self.move_piece)

class GameUI:
    def __init__(self):
        # initialize root Window and canvas
        root = Tk()
        root.title("Chess")
        root.resizable(False,False)
        canvas = Canvas(root, width = 300, height = 300)
        canvas.pack()

        # create two ball objects and animate them
        bishop1 = Bishop(canvas, 10, 10, 'white')
        bishop2 = Bishop(canvas, 60, 60, 'black')

        bishop1.move_piece()
        bishop2.move_piece()

        root.mainloop()

app = GameUI()

相关问题 更多 >