在类之间传递 tkinter 画布而不从父级内部调用子级

当谈到 Python 时,我有点像初学者,但我决定要编写一个基本的 2-d 物理操场。Unfortionetly 我在尝试设置基本结构时遇到了麻烦。

我的计划是在名为 mainWindow 的父函数中创建一个带有画布的 GUI,然后我想我会创建一个子类(Hero),它创建一个用户可以在画布上操作的圆圈。这似乎工作得很好。

当我尝试对 Hero 类做任何事情时,就会出现问题,比如调用一个函数来删除圆圈,这样我就可以在某个方向重绘它。我似乎无法将画布从 mainWindow 传递给 Hero 类。任何帮助将不胜感激,包括告诉我这是错误的做事方式。

我添加了我正在使用的两个文档,因为我的漫无边际可能难以理解。

我从 phesics.py 文档中运行程序,导致 GUI 弹出我的画布和一个红色圆圈。当我关闭窗口时,出现以下错误:

classes.py”,第 29 行,在 moveHeroBody canvas.delete(heroBody) NameError: name 'canvas' is not defined

Unfortionetly我不知道如何让“世界”进入孩子

类.py

from tkinter import *



class mainWindow():

    def __init__(self):

            #Setup the GUI

            root = Tk()

            root.geometry('800x600')

            # Setup the canvas within the GUI (master)

            world = Canvas(root, height = 600, width = 800, bg = "#FFFFFF")

            world.place(relx = 0.5, rely = 0.5, anchor = CENTER)


            Hero(world)


            root.mainloop()


class Hero(mainWindow):

    def __init__(self,world):

        #Initial creation of hero at coordinates

        x1 = 10

        y1 = 10

        x2 = 70

        y2 = 70

        heroBody = world.create_oval(x1,y1,x2,y2, fill = "#FF0000", outline = "#FF0000")


    #Move the hero

    def moveHeroBody():

        print("moveHeroBody")

        world.delete(heroBody)

物理学.py


from tkinter import *

from classes import *


mainWindow1 = mainWindow()

moveHero = Hero.moveHeroBody()


白衣非少年
浏览 223回答 2
2回答

临摹微笑

您可以通过它,但是您正在丢弃价值。另外,Hero不应该继承自mainWindow.您需要另存world为属性,以便以后可以引用它。class Hero():    def __init__(self,world):        self.world = world        ...然后,您可以使用self.world来引用画布:def moveHeroBody():    print("moveHeroBody")    self.world.delete(heroBody)虽然,上面的代码会失败,因为heroBody它是一个局部变量__init__- 你需要对它做同样的事情:class Hero():    def __init__(self,world):        self.world = world        ...        self.heroBody = world.create_oval(...)    #Move the hero    def moveHeroBody():        print("moveHeroBody")        self.world.delete(self.heroBody)

呼啦一阵风

我认为您需要Hero在 mainWindow 类中初始化该类。代码中需要做的修改是:类.pyfrom tkinter import *from time import sleepclass mainWindow():&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Setup the GUI&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.jump_gap = 25&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root = Tk()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root.geometry('800x600')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Setup the canvas within the GUI (master)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.world = Canvas(root, height = 600, width = 800, bg = "#FFFFFF")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.world.place(relx = 0.5, rely = 0.5, anchor = CENTER)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.hero = Hero(self.world)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.world.pack()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root.bind("<space>",self.jump) # -> [1] Binds the SPACE BAR Key to the function jump&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root.mainloop()&nbsp; &nbsp; def jump(self,event):&nbsp; &nbsp; &nbsp; &nbsp; gaps = list(range(self.jump_gap))&nbsp; &nbsp; &nbsp; &nbsp; for i in gaps:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.world.after(1,self.hero.moveHeroJump(h=i))&nbsp; # [2] -> Binds the moveHeroJump method with the window action to a queue of updates&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.world.update() #[2] updates the canvas&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sleep(0.01*i) # Added some linear wait time to add some look to it&nbsp; &nbsp; &nbsp; &nbsp; gaps.reverse()&nbsp; &nbsp; &nbsp; &nbsp; for i in gaps:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.world.after(1,self.hero.moveHeroJump(h=-i))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.world.update()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sleep(0.01*i)class Hero():&nbsp; &nbsp; def __init__(self,world):&nbsp; &nbsp; &nbsp; &nbsp; #Initial creation of hero at coordinates&nbsp; &nbsp; &nbsp; &nbsp; self.world = world&nbsp; &nbsp; &nbsp; &nbsp; self.x1 = 10&nbsp; &nbsp; &nbsp; &nbsp; self.y1 = 410&nbsp; &nbsp; &nbsp; &nbsp; self.x2 = 70&nbsp; &nbsp; &nbsp; &nbsp; self.y2 = 470&nbsp; &nbsp; &nbsp; &nbsp; self.heroBody = self.world.create_oval(self.x1,self.y1,self.x2,self.y2, fill = "#FF0000", outline = "#FF0000")&nbsp; &nbsp; #Move the hero&nbsp; &nbsp; def moveHeroJump(self,h):&nbsp; &nbsp; &nbsp; &nbsp; print("moveHeroBody")&nbsp; &nbsp; &nbsp; &nbsp; self.y1 -= h&nbsp; &nbsp; &nbsp; &nbsp; self.y2 -= h&nbsp; &nbsp; &nbsp; &nbsp; self.world.delete(self.heroBody)&nbsp; &nbsp; &nbsp; &nbsp; self.heroBody = self.world.create_oval(self.x1,self.y1,self.x2,self.y2, fill = "#FF0000", outline = "#FF0000")物理.pyfrom tkinter import *from classes import *mainWindow1 = mainWindow()编辑所以这让我几分钟前开始玩了,我从堆栈中研究了一些资源来完成这个问题。以下是来源(在代码中也有引用):如何将空格键键绑定到 tkinter python 中的某个方法移动 Tkinter 画布上面编辑的解决方案能够执行一个简单的跳球动画。self.jump_gap是一个固定的量,它告诉球它需要跳跃多少。jump解析一定高度的方法h让moveHeroJump小球改变它的位置,改变位置后排队进入Canvas一个更新被调用来查看小球的变化。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python