如何在tkinter gui中添加图像?

时间:2018-01-31 21:25:26

标签: python python-3.x tkinter

我拿起这段代码并想弄乱它,我遇到的主要问题是无法在二维阵列上的设定位置向实际gui添加图像。我没有得到任何实际的错误,但也没有得到gui上的图像输出。请帮忙!谢谢。

import tkinter as tk
class GameBoard(tk.Frame):
    def __init__(self, parent, rows=9, columns=9, size=60, color1="light grey", color2="light grey"):
        '''size is the size of a square, in pixels'''

        self.rows = rows
        self.columns = columns
        self.size = size
        self.color1 = color1
        self.color2 = color2
        self.pieces = {}

        canvas_width = columns * size
        canvas_height = rows * size

        tk.Frame.__init__(self, parent)
        self.canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0,
                            width=canvas_width, height=canvas_height, background="white")
        self.canvas.pack(side="top", fill="both", expand=True, padx=2, pady=2)

        self.canvas.bind("<Configure>", self.refresh)

    def addpiece(self, name, image, row=1, column=1):
        bishop = tk.PhotoImage(file='C:\\Users\\Sharjeel Jan\\Desktop\\final shit man\\Pieces\\Bishop.gif')
        self.canvas.create_image(1,1, image=bishop, tags=(name, "Bishop"), anchor= "c")
        self.placepiece(name, row, column)

    def placepiece(self, name, row, column):
        '''Place a piece at the given row/column'''
        self.pieces[name] = (row, column)
        x0 = (column * self.size) + int(self.size/2)
        y0 = (row * self.size) + int(self.size/2)
        self.canvas.coords(name, x0, y0)

def placepiece(self,name,row,column):         '''在给定的行/列放置一块'''         self.pieces [name] =(row,column)         x0 =(列* self.size)+ int(self.size / 2)         y0 =(row * self.size)+ int(self.size / 2)         self.canvas.coords(name,x0,y0)

  def refresh(self, event):
        '''Redraw the board, possibly in response to window being resized'''
        xsize = int((event.width-1) / self.columns)
        ysize = int((event.height-1) / self.rows)
        self.size = min(xsize, ysize)
        self.canvas.delete("square")
        color = self.color2
        for row in range(self.rows):
            color = self.color1 if color == self.color2 else self.color2
            for col in range(self.columns):
                x1 = (col * self.size)
                y1 = (row * self.size)
                x2 = x1 + self.size
                y2 = y1 + self.size
                self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", fill=color, tags="square")
                color = self.color1 if color == self.color2 else self.color2
        for name in self.pieces:
            self.placepiece(name, self.pieces[name][0], self.pieces[name][1])
        self.canvas.tag_raise("piece")
        self.canvas.tag_lower("square")




if __name__ == "__main__":
    root = tk.Tk()
    board = GameBoard(root)
    board.pack(side="top", fill="both", expand="true", padx=4, pady=4)
  # player1 = tk.PhotoImage(data=imagedata)
  # board.addpiece("player1", player1, 0,0)
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

您有一行试图将图像放到GUI:

self.canvas.create_image(1,1, image=bishop, tags=(name, "Bishop"), anchor= "c")

位于addpiece

之下

以下行将图像保存在参考中:

bishop = tk.PhotoImage(file='C:\\Users\\Sharjeel Jan\\Desktop\\final shit man\\Pieces\\Bishop.gif')

addpiece的范围专门定义,当方法完成时,如果显示图像,图像就会消失。

有效使此问题与Why does Tkinter image not show up if created in a function?

重复

为了防止这种情况,请在调用mainloop的范围内使图像引用可用。

附加引用bishop 附加到对象,例如self.canvas,其引用在{{1}的范围内可用}:

mainloop

或者只是传递来自self.canvas.bishop = tk.PhotoImage(file='C:\\Users\\Sharjeel Jan\\Desktop\\final shit man\\Pieces\\Bishop.gif') self.canvas.create_image(1,1, image=self.canvas.bishop, tags=(name, "Bishop"), anchor= "c") 范围的图像引用,并且使用该对象引用:

mainloop
相关问题