Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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
Python 物体不能移动_Python_Tkinter_Tkinter Canvas - Fatal编程技术网

Python 物体不能移动

Python 物体不能移动,python,tkinter,tkinter-canvas,Python,Tkinter,Tkinter Canvas,代码没有错误,但mycar无法移动。我是否错误地定义了for循环?我还尝试将for循环移动到def moveit(self、vx、vy),但没有区别。 这是我的密码: from tkinter import * import tkinter as tk import time class Example(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self) #create a ca

代码没有错误,但mycar无法移动。我是否错误地定义了for循环?我还尝试将for循环移动到def moveit(self、vx、vy),但没有区别。 这是我的密码:

from tkinter import *
import tkinter as tk
import time
class Example(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self)

        #create a canvas
        self.canvas = tk.Canvas(width=600, height=250)
        self.canvas.pack()
        self.road()
        self.crossing()

    def road(self):
        self.canvas.create_line(50, 50, 450, 50)
        self.canvas.create_line(50, 100, 450, 100)

    def crossing(self):
        self.canvas.create_line(350, 50, 350, 100)
        self.canvas.create_line(375, 50, 375, 100)


class Car:
    def __init__(self, x1, y1, x2, y2, vx, vy, color, example):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.vx = vx
        self.vy = vy
        self.color = color
        self.example = example

    def drawit(self, x1, y1, x2, y2, color):
        self.example.canvas.create_rectangle(x1, y1, x2, y2, fill=color)

    def moveit(self, vx, vy):
        self.example.canvas.move(self, vx, vy)

if __name__ == "__main__":
    root = tk.Tk()
    my_canvas = Example(root)
    my_canvas.pack(fill="both", expand=True)
    mycar = Car(60, 60, 125, 90, 20, 0, "red", my_canvas)
    mycar.drawit(60, 60, 125, 90, "red")
    mycar.moveit(20, 0)
    print(mycar.x1)
    root.update()
    root.mainloop()

非常感谢您的帮助

您没有看到您的汽车移动,因为您没有为
移动
功能提供正确的参数:

self.example.canvas.move(self,vx,vy)
:这里的
self
Car
对象,而不是表示画布上汽车的矩形的索引/标记。所以这是正常的,没有任何附加。在画布上绘制汽车时,应注册汽车索引:

def drawit(self, x1, y1, x2, y2, color):
    self.index = self.example.canvas.create_rectangle(x1, y1, x2, y2, fill=color)
然后用self.example.canvas.move(self.index,vx,vy)移动它

此外,再次使用
x1
y1
。。。作为
drawit
的参数,因为它们是汽车的属性。当汽车在
moveit
中移动时,您还忘记了增加汽车的位置,我建议您使用
after
方法而不是for循环来创建动画。因此,我用我的建议重写了您的代码:

import tkinter as tk

class Example(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self)

        #create a canvas
        self.canvas = tk.Canvas(width=600, height=250)
        self.canvas.pack()
        self.road()
        self.crossing()

    def road(self):
        self.canvas.create_line(50, 50, 450, 50)
        self.canvas.create_line(50, 100, 450, 100)

    def crossing(self):
        self.canvas.create_line(350, 50, 350, 100)
        self.canvas.create_line(375, 50, 375, 100)

class Car:
    def __init__(self, x1, y1, x2, y2, vx, vy, color, example):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.vx = vx
        self.vy = vy
        self.color = color
        self.example = example

    def drawit(self):
        self.index = self.example.canvas.create_rectangle(self.x1, self.y1, self.x2, self.y2, fill=self.color)

    def moveit(self):
        self.example.canvas.move(self.index, self.vx, self.vy)
        self.x1 += self.vx
        self.x2 += self.vx
        self.y1 += self.vy
        self.y2 += self.vy
        self.example.after(100, self.moveit)

if __name__ == "__main__":
    root = tk.Tk()
    my_canvas = Example(root)
    my_canvas.pack(fill="both", expand=True)
    mycar = Car(60, 60, 125, 90, 20, 0, "red", my_canvas)
    mycar.drawit()
    mycar.moveit()
    root.mainloop()

它看起来最多只能在40,60的时候展示汽车,而不做任何其他事情(除非有错误或其他错误;我没有试过运行它);80,60.哪个是循环?我在代码中没有看到任何for循环。这很有效!谢谢你。这段代码之间有什么区别<代码>适用于范围(0200)内的i:mycar.moveit(20,0)打印(mycar.x1)时间。睡眠(0.005)您的GUI将在睡眠期间冻结(您无法单击按钮…),而不会使用after方法。