当我点击编译时,我创建的python程序似乎没有打开。我感到困惑的原因是因为我有一个.mainloop()附加到类。通过我自己的测试,我推断它与我的代码中的while循环有关。此外,不确定这是否有帮助,但是当我中止程序时,控制台中会出现以下内容:
File "C:\Users\zach\Anaconda3\lib\tkinter\__init__.py", line 2585, in move
self.tk.call((self._w, 'move') + args)
KeyboardInterrupt
这是我完成的代码:
from tkinter import *
class GUI(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("Lab 8")
self.grid()
canvas_width = 800
canvas_height = 400
self.canvas = Canvas(self, width = canvas_width, height =
canvas_height, bg = "white")
self.canvas.grid()
ball_diameter = 20
top_x = 2
top_y = 2
self.canvas.create_oval(top_x, top_y, top_x + ball_diameter,
top_y + ball_diameter, fill = "black", tags = "ball")
horizontal_direction = "east"
vertical_direction = "south"
dx = 2
dy = 2
while True:
if horizontal_direction == "east":
self.canvas.move("ball", dx, 0) # move ball horizontally dx pixels to the right/east
top_x += dx # dx is 2 because the ball moves 2 pixels horizontally every 15 milliseconds
if top_x >= canvas_width - ball_diameter: # ball has hit east wall
horizontal_direction = "west" # change direction
else: # i.e., horizontal_direction is "west"
self.canvas.move("ball", -dx, 0) # move ball horizontally dx pixels to the left/west
top_x -= dx
if top_x <= 0: # ball has hit west wall
horizontal_direction = "east" # change direction
if vertical_direction == "south":
self.canvas.move("ball", 0, dy)
top_y += dy
if top_y >= canvas_height - ball_diameter:
vertical_direction = "north"
else:
self.canvas.move("ball", 0, -dy)
top_y -= dy
if top_y <= 0 :
vertical_direction = "south"
def main():
GUI().mainloop()
main()
我想出来是因为我忘了添加更新以及等待球并更新其位置的方法。
答案 0 :(得分:2)
您的问题是您在__init__
功能中运行无限循环。它永远不会到达你的主循环来运行GUI。您需要让__init__
结束并调用您的更新代码,而不是在紧密循环中,而是以设定的时间间隔。
分解代码以将形状移动到单独的函数中,减去无限循环并使用tk小部件的after
方法间隔调用该函数。
from tkinter import *
class GUI(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("Lab 8")
self.grid()
self.canvas_width = 800
self.canvas_height = 400
self.canvas = Canvas(self,
width=self.canvas_width,
height=self.canvas_height,
bg="white")
self.canvas.grid()
self.ball_diameter = 20
self.top_x = 2
self.top_y = 2
self.canvas.create_oval(self.top_x,
self.top_y,
self.top_x + self.ball_diameter,
self.top_y + self.ball_diameter,
fill = "black", tags = "ball")
self.horizontal_direction = "east"
self.vertical_direction = "south"
self.dx = 2
self.dy = 2
self.after(10, self.move)
def move(self):
if self.horizontal_direction == "east":
self.canvas.move("ball", self.dx, 0) # move ball horizontally dx pixels to the right/east
self.top_x += self.dx # dx is 2 because the ball moves 2 pixels horizontally every 15 milliseconds
if self.top_x >= self.canvas_width - self.ball_diameter: # ball has hit east wall
self.horizontal_direction = "west" # change direction
else: # i.e., horizontal_direction is "west"
self.canvas.move("ball", -self.dx, 0) # move ball horizontally dx pixels to the left/west
self.top_x -= self.dx
if self.top_x <= 0: # ball has hit west wall
self.horizontal_direction = "east" # change direction
if self.vertical_direction == "south":
self.canvas.move("ball", 0, self.dy)
self.top_y += self.dy
if self.top_y >= self.canvas_height - self.ball_diameter:
self.vertical_direction = "north"
else:
self.canvas.move("ball", 0, -self.dy)
self.top_y -= self.dy
if self.top_y <= 0 :
self.vertical_direction = "south"
self.after(10, self.move)
def main():
GUI().mainloop()
main()
请注意,self
变量的数量已经爆炸并变得非常难以管理恕我直言,这可能表明您需要将其中一些值分解为其他类。
但从根本上说这是运行。它不是很漂亮,但你可以继续下一步。