我在python tk中创建一个3d框,但是当在角落屏幕和窗口全屏幕中绘制时,框会循环并创建多个。
我不确定为什么会这样,只应该有一个盒子。
为什么这会在画布上的不同位置创建框?
以下是代码:
from tkinter import *
window = Tk()
window.title("Game.py")
window.geometry("800x500")
canvas = Canvas(window, width = window.winfo_width(), height = window.winfo_height())
canvas.configure(background='#5d90e2')
canvas.pack()
verts = ( (-1,-1,-1), (1,-1,-1), (1,1,-1), (-1,1,-1), (-1,-1,1), (1,-1,1),(1,1,1), (-1,1,1) )
edges = (0,1), (1,2), (2,3), (3,0), (4,5), (5,6), (6,7), (7,4), (0,4),(1,5), (2,6), (3,7)
def fit_canvas():
canvas.config(width= window.winfo_width(), height=window.winfo_height())
def main():
fit_canvas()
hw = window.winfo_width()/2
hh = window.winfo_height()/2
for edge in edges:
points = []
a = verts[edge[0]]
b = verts[edge[1]]
for x,y,z in (a,b):
z += 5
f = int(200/z)
x,y = x*f, y*f
points += [(hw+x, hh+y)]
print(points[0] , points[1])
canvas.create_line(points[0][0],points[0][1],points[1][0],points[1}[1])
loop_no = 0
while True:
loop_no += 1
main()
canvas.update()
答案 0 :(得分:0)
您在无限循环中调用main()
,因此您将获得无限数量的框(直到程序崩溃...)。
要解决此问题,请移除循环。