为什么我无法在 canvas 中设置背景
Why I can't set background in canvas
我正在尝试将背景图片放入 canvas 中,如下所示:
import tkinter as tk
class App(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.canvas = GameBoard()
self.canvas.pack()
class GameBoard(tk.Canvas):
WIDTH = 600
HEIGHT = 600
BOARD_IMAGE_PATH = '../resources/background600x600.png'
def __init__(self):
super().__init__(width=GameBoard.WIDTH, height=GameBoard.HEIGHT)
self.boardImage = tk.PhotoImage(file=GameBoard.BOARD_IMAGE_PATH)
self.create_image(0, 0, self.boardImage, anchor='nw')
root = tk.Tk()
app = App(root)
app.mainloop()
但我收到此错误消息:
C:\Users\Tata\Documents\python\PycharmProjects\CanvasBackground\venv\Scripts\python.exe C:/Users/Tata/Documents/python/PycharmProjects/CanvasBackground/GUI/frame.py
Traceback (most recent call last):
File "C:\Users\Tata\Documents\python\PycharmProjects\CanvasBackground\GUI\frame.py", line 21, in <module>
app = App(root)
File "C:\Users\Tata\Documents\python\PycharmProjects\CanvasBackground\GUI\frame.py", line 14, in __init__
self.canvas = GameBoard()
File "C:\Users\Tata\Documents\python\PycharmProjects\CanvasBackground\GUI\gameBoard.py", line 13, in __init__
self.create_image(0, 0, self.boardImage, anchor='nw')
File "C:\Users\Tata\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2787, in create_image
return self._create('image', args, kw)
File "C:\Users\Tata\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2773, in _create
return self.tk.getint(self.tk.call(
_tkinter.TclError: unknown option "pyimage3"
Process finished with exit code 1
当我尝试 运行 调试器时,它说
Connection to Python debugger failed
Interrupted function call: accept failed
这完全超出了我的理解范围。为什么不行,为什么骗子不配合?
定义图像时需要关键字参数。改变这个:
self.create_image(0, 0, self.boardImage, anchor='nw')
...为此:
self.create_image(0, 0, image=self.boardImage, anchor='nw')
我正在尝试将背景图片放入 canvas 中,如下所示:
import tkinter as tk
class App(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.canvas = GameBoard()
self.canvas.pack()
class GameBoard(tk.Canvas):
WIDTH = 600
HEIGHT = 600
BOARD_IMAGE_PATH = '../resources/background600x600.png'
def __init__(self):
super().__init__(width=GameBoard.WIDTH, height=GameBoard.HEIGHT)
self.boardImage = tk.PhotoImage(file=GameBoard.BOARD_IMAGE_PATH)
self.create_image(0, 0, self.boardImage, anchor='nw')
root = tk.Tk()
app = App(root)
app.mainloop()
但我收到此错误消息:
C:\Users\Tata\Documents\python\PycharmProjects\CanvasBackground\venv\Scripts\python.exe C:/Users/Tata/Documents/python/PycharmProjects/CanvasBackground/GUI/frame.py
Traceback (most recent call last):
File "C:\Users\Tata\Documents\python\PycharmProjects\CanvasBackground\GUI\frame.py", line 21, in <module>
app = App(root)
File "C:\Users\Tata\Documents\python\PycharmProjects\CanvasBackground\GUI\frame.py", line 14, in __init__
self.canvas = GameBoard()
File "C:\Users\Tata\Documents\python\PycharmProjects\CanvasBackground\GUI\gameBoard.py", line 13, in __init__
self.create_image(0, 0, self.boardImage, anchor='nw')
File "C:\Users\Tata\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2787, in create_image
return self._create('image', args, kw)
File "C:\Users\Tata\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2773, in _create
return self.tk.getint(self.tk.call(
_tkinter.TclError: unknown option "pyimage3"
Process finished with exit code 1
当我尝试 运行 调试器时,它说
Connection to Python debugger failed
Interrupted function call: accept failed
这完全超出了我的理解范围。为什么不行,为什么骗子不配合?
定义图像时需要关键字参数。改变这个:
self.create_image(0, 0, self.boardImage, anchor='nw')
...为此:
self.create_image(0, 0, image=self.boardImage, anchor='nw')