如何在python tkinter中创建图像?

时间:2014-01-28 03:35:57

标签: python tkinter

我正在尝试在tkinter中创建一个图像,但该文件是不可读的。我在我的文件夹中有文件,但它一直通过控制台向我发送错误。这是我的代码:

from tkinter import *
import time


class Game:
    def __init__(self):
        self.tk = Tk()
        self.tk.title("Game")
        self.tk.resizable(0,0)
        self.tk.wm_attributes("-topmost", 1)
        self.canvas = Canvas(self.tk, width=500, height=400, bd=0, highlightthickness=0)
        self.canvas.pack()
        self.tk.update()
        self.bg = PhotoImage(file="bg.gif")
        w = self.bg.width()
        h = self.bg.height()
        for x in range(0, 10):
            for y in range(0, 8):
                self.canvas.create_image(x * w, y * h, image=self.bg, anchor='nw')

    def mainloop(self):
        time.sleep(1)
        self.tk.update_idletasks()
        self.tk.update()
        time.sleep(0.01)

编辑代码,同样的错误:

from tkinter import *
import time

tk = Tk()
tk.title("Game")
tk.resizable(0,0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()
bg = PhotoImage(file="bg.gif")
w = self.bg.width()
h = self.bg.height()
for x in range(0, 10):
    for y in range(0, 8):
        canvas.create_image(x * w, y * h, image=bg, anchor='nw')

0 个答案:

没有答案