问题描述
我遇到此错误:
回溯(最近通话最近):文件 “ D:\ Mario \ modules \ MovingBackground_4.py”,第54行,以bg = BackgroundImage(canvas,event)NameError:名称“ event”未定义。
我是Python的新手,现在阅读了很多有关Python语言的各个方面及其背后的逻辑的知识,但是我对特定的事件'event'并不熟悉,但还是有很多错误。经过打印(事件)测试后,我发现它存储位置,键号等。我不知道如何定义它(事先?),所以我可以将其传递给bg = BackgroundImage(canvas,event)。有人吗?
from tkinter import *
import tkinter as tk
import time
import keyboard
import random
posOfBackgrImage_X = 500
posOfBackgrImage_Y = -40
tk = Tk()
canvas = Canvas (tk,width = 256,height = 224 )
canvas.pack()
tk.update()
class BackgroundImage:
def __init__(self,canvas,event):
self.canvas = canvas
#self.event = event
self.background = PhotoImage(file = "d:\Mario\images\Backgrounds\smw-bg-clouds1.png") #loads a background image and makes an instance of that
self.marioBackGround = self.canvas.create_image(posOfBackgrImage_X,posOfBackgrImage_Y,image=self.background,anchor=NE) #loads in the parameters the height,witth and the object 'filename'.
self.x = 0
self.canvas_width = self.canvas.winfo_width()
def move(self):
if event.char == "a":
self.canvas.move(self.marioBackGround,-10,0)
elif event.char == "d":
self.canvas.move(self.marioBackGround,10,0)
tk.bind("<Key>",move)
def draw(self):
self.canvas.move(self.marioBackGround,self.x,0)
pos = self.canvas.coords(self.marioBackGround)
#canvas.pack()
bg = BackgroundImage(canvas,event)
#g.GameLoop()
while 1:
bg.move()
bg.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)