使用tkinter在python中旋转立方体

使用tkinter在python中旋转立方体,python,tkinter,graphics,Python,Tkinter,Graphics,我正在尝试使用tkinter旋转立方体。我的策略是让一组顶点旋转,然后画出连接这些顶点的线。然而,当我尝试运行这个时,画布并没有打开。我尝试通过将打印条件放入循环中进行调试,代码似乎正在旋转多维数据集,但GUI没有显示: from tkinter import* import math import time import numpy as np class Window(Frame): def __init__(self, master=None): Frame._

我正在尝试使用tkinter旋转立方体。我的策略是让一组顶点旋转,然后画出连接这些顶点的线。然而,当我尝试运行这个时,画布并没有打开。我尝试通过将打印条件放入循环中进行调试,代码似乎正在旋转多维数据集,但GUI没有显示:

from tkinter import*
import math
import time
import numpy as np

class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master

class Square():
    def __init__(self, length, cx, cy):
        self._cx = cx
        self._cy = cy
        self._length = length
        self._vertices = []
        self._vertices.append([-length/2,length/2])
        self._vertices.append([length/2, -length/2])
        self._vertices.append([length/2, length/2])
        self._vertices.append([-length/2, length/2])


    def rotate(self, theta):
        rotat_matrix = np.array([[math.cos(theta), math.sin(theta)],[-math.sin(theta), math.cos(theta)]])
        #print("rotation matrix:", rotat_matrix)
        for i in range(len(self._vertices)):
            vector = np.dot(rotat_matrix,self. _vertices[i]).tolist()
            #print("V:", vector)
            self._vertices[i] = vector

square = Square(100, 500, 500)


top = Tk()
canvas = Canvas(top,bg="#EBEDEF",height=800, width=800)
canvas.pack()



while (True):
    v0 = square._vertices[0]
    v1 = square._vertices[1]
    v2 = square._vertices[2]
    v3 = square._vertices[3]
    canvas.create_line(v0[0] + 200, v0[1] + 200, v1[0]+200,v1[1]+200)
    canvas.create_line(v1[0] + 200, v1[1] + 200, v2[0]+200,v2[1]+200)
    canvas.create_line(v2[0] + 200, v2[1] + 200, v3[0]+200,v3[1]+200)
    canvas.create_line(v3[0] + 200, v3[1] + 200, v0[0]+200,v0[1]+200)
    square.rotate(math.pi/8)
    #time.sleep(2)
    print("rotated")

top.mainloop()


while True
循环永远不会退出,并且阻止代码到达
top.mainloop()
,因此您的画布永远不会显示

您可以将其替换为使用
top.after
方法调用自身的函数

from tkinter import*
import math
import time
import numpy as np

class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master

class Square():
    def __init__(self, length, cx, cy):
        self._cx = cx
        self._cy = cy
        self._length = length
        self._vertices = []
        self._vertices.append([-length/2,length/2])
        self._vertices.append([length/2, -length/2])
        self._vertices.append([length/2, length/2])
        self._vertices.append([-length/2, length/2])


    def rotate(self, theta):
        rotat_matrix = np.array([[math.cos(theta), math.sin(theta)],[-math.sin(theta), math.cos(theta)]])
        #print("rotation matrix:", rotat_matrix)
        for i in range(len(self._vertices)):
            vector = np.dot(rotat_matrix,self. _vertices[i]).tolist()
            #print("V:", vector)
            self._vertices[i] = vector

square = Square(100, 500, 500)


top = Tk()
canvas = Canvas(top,bg="#EBEDEF",height=800, width=800)
canvas.pack()



def run():
    v0 = square._vertices[0]
    v1 = square._vertices[1]
    v2 = square._vertices[2]
    v3 = square._vertices[3]
    canvas.create_line(v0[0] + 200, v0[1] + 200, v1[0]+200,v1[1]+200)
    canvas.create_line(v1[0] + 200, v1[1] + 200, v2[0]+200,v2[1]+200)
    canvas.create_line(v2[0] + 200, v2[1] + 200, v3[0]+200,v3[1]+200)
    canvas.create_line(v3[0] + 200, v3[1] + 200, v0[0]+200,v0[1]+200)
    square.rotate(math.pi/8)
    print("rotated")
    top.after(100, run)


if __name__ == '__main__':
    run()
    top.mainloop()

最好将
def run()
前面的四行放入
中,如果uuu name\uuuu=='\uuuu main\uuuu'