tkinter 不打开图像
tkinter doesnt open images
请先看代码:
from tkinter import*
import time
import random
class Game:
def __init__(self):
self.tk = Tk()
self.tk.title("Mr Stickman races for the exit")
self.tk.resizable(0, 0)
self.tk.wm_attributes("-topmost", 1)
self.canvas = Canvas(self.tk, width=500, height=500, highlightthickness= 0)
self.canvas,Pack()
self.tk.update()
self.canvas_height = 500
self.canvas_width = 500
self.bg = PhotoImage(file="background1.gif")
w = self.bg.width()
h = self.bg.height()
for x in range(0, 5):
for y in range (0, 5):
self.canvas.create_image(x * w, y * h, Image= self.bg, anchor="nw")
self.sprites = []
self.running = True
def mainloop(self):
while 1:
if self.running == True:
for sprite in self.sprites:
sprite.move()
self.tk.update_idletasks()
self.tk.update()
time.sleep(0.01)
g = Game()
g.mainloop()
当我 运行 它时,它显示:
_tkinter.TclError: couldn't open "background1.gif": no such file or directory
我也尝试过这种方式并得到了这个错误:
self.bg = PhotoImage(file="C:\Users\Sajjad\Documents\python")
我把文件放在 excat 位置但得到了这个:
_tkinter.TclError: couldn't open "C:\Users\Sajjad\Documents\python": permission denied
我该怎么办?
你应该考虑多方面的事情:
1- Select 并正确找到图像地址 - 在我的例子中,我将图像和脚本放在同一个文件夹中
file=sys.path[0] + "/imageName.imageExt"
2- 替换
Image=self.bg
和
image=self.bg
3- 使用 place、pack 或 grid 将您的 canvas 放入应用程序 window 中;像这样:
self.canvas.place(x=0,y=0)
或您的情况更好:
self.canvas.pack()
4- 注意缩进块
来源:
from tkinter import*
import time
import sys
class Game:
def __init__(self):
self.tk = Tk()
self.tk.title("Mr Stickman races for the exit")
self.tk.resizable(0, 0)
self.tk.wm_attributes("-topmost", 1)
self.canvas = Canvas(self.tk, width=500,
height=500, highlightthickness=0)
self.canvas, Pack()
self.tk.update()
self.canvas_height = 500
self.canvas_width = 500
# ***CHANGE "/back.png" WITH YOUR IMAGE; THIS IS JUST FOR TEST***
self.bg = PhotoImage(file=sys.path[0] + "/back.png") # HERE sys.path
w = self.bg.width()
h = self.bg.height()
for x in range(0, 5):
for y in range(0, 5):
self.canvas.create_image(
x * w, y * h, image=self.bg, anchor="nw") # HERE Image -> image
self.canvas.pack() # HERE
self.sprites = []
self.running = True
def mainloop(self):
while 1:
if self.running == True:
for sprite in self.sprites:
sprite.move()
self.tk.update_idletasks()
self.tk.update()
time.sleep(0.01)
g = Game()
g.mainloop()
这是我在图形软件中绘制的“back.png”图像:
最终输出:
请先看代码:
from tkinter import*
import time
import random
class Game:
def __init__(self):
self.tk = Tk()
self.tk.title("Mr Stickman races for the exit")
self.tk.resizable(0, 0)
self.tk.wm_attributes("-topmost", 1)
self.canvas = Canvas(self.tk, width=500, height=500, highlightthickness= 0)
self.canvas,Pack()
self.tk.update()
self.canvas_height = 500
self.canvas_width = 500
self.bg = PhotoImage(file="background1.gif")
w = self.bg.width()
h = self.bg.height()
for x in range(0, 5):
for y in range (0, 5):
self.canvas.create_image(x * w, y * h, Image= self.bg, anchor="nw")
self.sprites = []
self.running = True
def mainloop(self):
while 1:
if self.running == True:
for sprite in self.sprites:
sprite.move()
self.tk.update_idletasks()
self.tk.update()
time.sleep(0.01)
g = Game()
g.mainloop()
当我 运行 它时,它显示:
_tkinter.TclError: couldn't open "background1.gif": no such file or directory
我也尝试过这种方式并得到了这个错误:
self.bg = PhotoImage(file="C:\Users\Sajjad\Documents\python")
我把文件放在 excat 位置但得到了这个:
_tkinter.TclError: couldn't open "C:\Users\Sajjad\Documents\python": permission denied
我该怎么办?
你应该考虑多方面的事情:
1- Select 并正确找到图像地址 - 在我的例子中,我将图像和脚本放在同一个文件夹中
file=sys.path[0] + "/imageName.imageExt"
2- 替换
Image=self.bg
和
image=self.bg
3- 使用 place、pack 或 grid 将您的 canvas 放入应用程序 window 中;像这样:
self.canvas.place(x=0,y=0)
或您的情况更好:
self.canvas.pack()
4- 注意缩进块
来源:
from tkinter import*
import time
import sys
class Game:
def __init__(self):
self.tk = Tk()
self.tk.title("Mr Stickman races for the exit")
self.tk.resizable(0, 0)
self.tk.wm_attributes("-topmost", 1)
self.canvas = Canvas(self.tk, width=500,
height=500, highlightthickness=0)
self.canvas, Pack()
self.tk.update()
self.canvas_height = 500
self.canvas_width = 500
# ***CHANGE "/back.png" WITH YOUR IMAGE; THIS IS JUST FOR TEST***
self.bg = PhotoImage(file=sys.path[0] + "/back.png") # HERE sys.path
w = self.bg.width()
h = self.bg.height()
for x in range(0, 5):
for y in range(0, 5):
self.canvas.create_image(
x * w, y * h, image=self.bg, anchor="nw") # HERE Image -> image
self.canvas.pack() # HERE
self.sprites = []
self.running = True
def mainloop(self):
while 1:
if self.running == True:
for sprite in self.sprites:
sprite.move()
self.tk.update_idletasks()
self.tk.update()
time.sleep(0.01)
g = Game()
g.mainloop()
这是我在图形软件中绘制的“back.png”图像:
最终输出: