-2

我正在创建一个 python 棋盘游戏,但我什至不能把我的棋盘放在我的 GUI 上!我在用:

from tkinter import *
root = Tk()
panel = Label(root, image = "board.gif")
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

但它返回:

Traceback (most recent call last):
  File "/Users/GAMEKNIGHT7/Desktop/genius hour/chineseCheckersAI(genius hour).py", line 3, in <module>
    label = Label(image="board.gif")
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2760, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2293, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "board.gif" doesn't exist

发生了什么?我怎样才能做到这一点?

4

1 回答 1

2

您必须使用 PhotoImage 类:

from tkinter import *
root = Tk()
img = PhotoImage(file="board.gif")
panel = Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
root.mainloop()
于 2018-11-05T08:43:23.963 回答