【问题标题】:How to add image in tkinter gui?如何在 tkinter gui 中添加图像?
【发布时间】:2018-01-31 21:25:25
【问题描述】:

我拿起这段代码并想弄乱它,我遇到的主要问题是无法将图像添加到二维数组上设定位置的实际 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] = (行, 列) x0 = (列 * self.size) + int(self.size/2) y0 = (行 * 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()

【问题讨论】:

标签: python python-3.x tkinter


【解决方案1】:

您有一行尝试将图像放入 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,其引用在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 范围内传递图像引用,并使用该对象引用:

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

【讨论】:

  • 我似乎得到了相同的结果,我不确定问题是什么。
猜你喜欢
  • 2015-01-18
  • 2017-12-04
  • 2012-04-25
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 2019-01-31
  • 2017-11-14
  • 2013-04-04
相关资源
最近更新 更多