【问题标题】:How to animate objects of a class from within another class?如何从另一个类中为一个类的对象设置动画?
【发布时间】:2019-07-21 01:53:05
【问题描述】:

我正在使用 Python 和 tkinter 创建一个 GUI。我创建了两个类,AppdrawBallApp 类继承自 Tk.Frame。我无法从App 中创建drawBall 对象。

我也非常感谢有关我的代码的任何其他反馈,我对 OOP 还很陌生。

创建类App后,继承自Tk.Frame。我想创建另一个类来在屏幕上绘制一个球(使用画布)。我创建了一个基本 GUI,但在尝试调用 drawBall 类时,我收到以下错误:'drawBall' 对象没有属性'canvas'。

class App(tk.Frame):

      def __init__(self,master):
          super().__init__(master)

        #create title and size for the window
           self.master.geometry("640x360")

           self.canvas = tk.Canvas(self.master,relief = 'raised',borderwidth = 1)
           self.canvas.grid(row = 0,column = 0,sticky = 'NW')



        #create a startSimulation button, place it in the bottom right corner
           self.startButton = tk.Button(self.master,text = 'Start',command = self.startCallback)
           self.startButton.grid(row = 2,column = 3)

        #create a quit button, place it in the bottom right corner
           self.quitButton = tk.Button(self.master,text = "Quit",command = self.master.destroy)
           self.quitButton.grid(row = 3, column =3)


    #callback for start button click
       def startCallback(self):
            #### this is where the error occurs #####
            self.ball1 = drawBall(self.master,self.canvas) 


class drawBall():
     def __init__(self,master,canvas):
         self.canvas.create_oval(25,75,35,85,fill = 'blue')


     def moveBall(self):
         deltaX = 1
         self.canvas.move(self.seed,deltaX,0)
         self.canvas.after(50,self.moveBall)


if __name__ == '__main__':

    window = tk.Tk()
    simulate = App(window)
    window.mainloop()

我希望调用“self.ball1 = drawBall(self.master,self.canvas)”会导致在屏幕上绘制圆。

【问题讨论】:

  • 您在drawBall 中忘记了self.canvas = canvas。在drawBall 中不需要master
  • 使用名词作为类名是个好规则——即。 class Ball - 仅用于函数名称的动词 - (def moveBall)。如果您的班级是Ball,那么没有理由在moveBall 中使用名称Ball,使用move() 而不是moveBall()moveSquare()moveSomethingElse() 会更容易。您可以将它们保留在列表中并使用for item in list: item.move()

标签: python python-3.x class animation tkinter


【解决方案1】:

你需要一个类Ball,它需要一个canvas,并且能够自行移动。
然后,在App 中,创建一个球集合,并命令它们移动。

类似这样的:

import tkinter as tk


class App(tk.Frame):

    def __init__(self, master):
        super().__init__(master)

        self.master.geometry("640x360")

        self.canvas = tk.Canvas(self.master, relief='raised', borderwidth=1)
        self.canvas.grid(row=0, column=0, sticky='NW')

        self.startButton = tk.Button(self.master, text='animate', command=self.launch_animation)
        self.startButton.grid(row=2, column=3)

        self.stopButton = tk.Button(self.master, text='stop', command=self.stop_animation)
        self.stopButton.grid(row=3, column=3)

        self.quitButton = tk.Button(self.master, text="Quit",command=self.master.destroy)
        self.quitButton.grid(row=4, column=3)

        self.balls = [Ball(self.canvas)]

        self.anim_is_on = False

    def stop_animation(self):
        self.anim_is_on = False

    def launch_animation(self):
        if self.anim_is_on:      # prevent launching several overlapping animation cycles
            return
        self.anim_is_on = True
        self.animate()

    def animate(self):
        if not self.anim_is_on:
            return
        for ball in self.balls:
            ball.moveball()
        self.after(100, self.animate)


class Ball():

    def __init__(self, canvas):
        self.canvas = canvas
        self.id = self.canvas.create_oval(25, 75, 35, 85, fill='blue')

    def moveball(self):
        delta_x = 1
        self.canvas.move(self.id, delta_x, 0)


window = tk.Tk()
simulate = App(window)
window.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多