当谈到 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()
白衣非少年
临摹微笑
呼啦一阵风
随时随地看视频慕课网APP
相关分类