Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t访问类,因为uu init_uu()中的未定义(事件)属性中存在nameError,我不知道该属性的';我不知道如何定义它!(Python)_Python_Events_Tkinter_Bind_Nameerror - Fatal编程技术网

Can';t访问类,因为uu init_uu()中的未定义(事件)属性中存在nameError,我不知道该属性的';我不知道如何定义它!(Python)

Can';t访问类,因为uu init_uu()中的未定义(事件)属性中存在nameError,我不知道该属性的';我不知道如何定义它!(Python),python,events,tkinter,bind,nameerror,Python,Events,Tkinter,Bind,Nameerror,我得到这个错误: 回溯(最近一次调用上次):文件 “D:\Mario\modules\MovingBackground_4.py”,bg第54行= BackgroundImage(画布、事件)名称错误:未定义名称“事件” 我是Python新手,现在阅读了很多Python语言的各个方面及其背后的逻辑,但我不熟悉特定的参数“event”,也不熟悉大多数错误。在一次打印(事件)测试后,我发现它存储位置、键号等,。我不知道如何定义它(事先?),所以我可以将它传递给bg=BackgroundImage(c

我得到这个错误:

回溯(最近一次调用上次):文件 “D:\Mario\modules\MovingBackground_4.py”,bg第54行= BackgroundImage(画布、事件)名称错误:未定义名称“事件”

我是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)
    
从tkinter导入*
将tkinter作为tk导入
导入时间
输入键盘
随机输入
posOfBackgrImage_X=500
posOfBackgrImage_Y=-40
tk=tk()
画布=画布(tk,宽度=256,高度=224)
canvas.pack()
tk.update()
课程背景图片:
定义初始化(自我、画布、事件):
self.canvas=画布
#self.event=事件
self.background=PhotoImage(file=“d:\Mario\images\Backgrounds\smw-bg-clouds1.png”)#加载背景图像并生成该图像的实例
self.marioBackGround=self.canvas.create_image(posOfBackgrImage_X,posOfBackgrImage_Y,image=self.background,anchor=NE)#在参数中加载高度、宽度和对象“filename”。
self.x=0
self.canvas_width=self.canvas.winfo_width()
def移动(自我):
如果event.char==“a”:
self.canvas.move(self.marioBackGround,-10,0)
elif event.char==“d”:
self.canvas.move(self.marioBackGround,10,0)
tk.bind(“,move)
def牵引(自):
self.canvas.move(self.marioBackGround,self.x,0)
pos=self.canvas.coords(self.marioBackGround)
#canvas.pack()
背景图像(画布、事件)
#g、 GameLoop()
而1:
bg.move()
bg.draw()
tk.update_idletasks()
tk.update()
睡眠时间(0.01)

我认为构造函数不需要事件。删除它,看看会发生什么。您已将类定义为在创建实例时需要一个事件,但您没有传递该事件。您的类不使用事件,那么您为什么需要它?这是您的代码,还是您从其他人那里复制的?在
bg=BackgroundImage(canvas,event)
之前,您必须创建
event=…
或从定义和执行中删除此
event
,因为您没有在class.tk.bind()中使用此
event
)应该在
\uuuu init\uuuu
内,并且应该使用
self.move()
而不是
move()
Bind
将发送有关事件和函数的额外信息
move()
必须获取
def move(self,event)
。我不知道为什么在循环中运行相同的函数
bg.move()
——如果要执行它,那么必须使用事件信息执行,因为在
def move(self,event)中需要它:
简而言之:您有许多小错误,必须学习tkinter和面向对象编程(OOP)的基础知识也就是说上课。