IDLE3.6.1的python图形中的以下代码未运行它只是运行控制台窗口但没有执行:
from graphics import*
import time
def moveAll(shapeList,dx,dy):
for shape in shapeList:
shape.move(dx,dy)
def moveAllOnline(shapeList,dx,dy,repititions,delay):
for i in range(repititions):
moveAll(shapeList,dx,dy)
time.sleep(delay)
def makeFace(center,win):
head=Circle(center,25)
head.setFill("green")
head.draw(win)
eye1Center=center.clone()
eye1Center.move(-10,5)
eye1=Circle(eye1Center,5)
eye1.setFill("blue")
eye1.draw(win)
eye2End1=eye1Center.clone()
eye2End1.move(15,0)
eye2End2=eye2End1.clone()
eye2End2.move(10,0)
eye2=Line(eye2End1,eye2End2)
eye2.setWidth(3)
eye2.draw(win)
mouthCorner1=center.clone()
mouthCorner1.move(-10,-10)
mouthCorner2=mouthCorner1.clone()
mouthCorner2.move(20,-5)
mouth=Oval(mouthcorner1,mouthCorner2)
mouth.setFill("red")
mouth.draw(win)
return [head,eye1,eye2,mouth]
def main():
winWidth=300
winHeight=300
win=GraphWin('Back and forth',winWidth,winHeight)
win.setCoords(0,0,winWidth,winHeight)
rect=Rectangle(Point(200,90),Point(220,100))
rect.setFill("blue")
rect.draw(win)
faceList=makeFace(Point(40,100),win)
faceList2=makeFace(Point(150,125),win)
stepsAcross=46
dx=5
dy=3
wait=0.05
for i in range(3):
makeAllOnline(facelist,dx,0,stepsAcross,wait)
moveAllOnLine(faceList,-dx,dy,stepsAcross//2,wait)
moveAllOnLine(faceList,-dx,dy,stepsAcross//2,wait)
message=Text(Point(winWidth/2,20),'Click here to quit')
message.draw(win)
win.getmouse()
win.close()
main()
答案 0 :(得分:0)
我发现你的代码存在一些问题:即使@phd格式化了@mkiever的请求后,这些行也缩进了一个级别:
message=Text(Point(winWidth/2,20),'Click here to quit')
message.draw(win)
win.getmouse()
win.close()
它们不应该成为循环的一部分。您还在代码中遇到了多个与案例相关的拼写错误:
makeAllOnline -> moveAllOnLine
moveAllOnline -> moveAllOnLine
mouthcorner1 -> mouthCorner1
facelist -> faceList
我已经重新设计它以使其可从Python控制台读取和运行:
import time
from graphics import *
def moveAll(shapeList, dx, dy):
for shape in shapeList:
shape.move(dx, dy)
def moveAllOnLine(shapeList, dx, dy, repetitions, delay):
for _ in range(repetitions):
moveAll(shapeList, dx, dy)
time.sleep(delay)
def makeFace(center, win):
head = Circle(center, 25)
head.setFill('green')
head.draw(win)
eye1Center = center.clone()
eye1Center.move(-10, 5)
eye1 = Circle(eye1Center, 5)
eye1.setFill('blue')
eye1.draw(win)
eye2End1 = eye1Center.clone()
eye2End1.move(15, 0)
eye2End2 = eye2End1.clone()
eye2End2.move(10, 0)
eye2 = Line(eye2End1, eye2End2)
eye2.setWidth(3)
eye2.draw(win)
mouthCorner1 = center.clone()
mouthCorner1.move(-10, -10)
mouthCorner2 = mouthCorner1.clone()
mouthCorner2.move(20, -5)
mouth = Oval(mouthCorner1, mouthCorner2)
mouth.setFill('red')
mouth.draw(win)
return [head, eye1, eye2, mouth]
def main():
winWidth, winHeight = 300, 300
win = GraphWin('Back and forth', winWidth, winHeight)
win.setCoords(0, 0, winWidth, winHeight)
rect = Rectangle(Point(200, 90), Point(220, 100))
rect.setFill('blue')
rect.draw(win)
faceList = makeFace(Point(40, 100), win)
stepsAcross = 46
dx, dy = 5, 3
wait = 0.05
for _ in range(3):
moveAllOnLine(faceList, dx, 0, stepsAcross, wait)
moveAllOnLine(faceList, -dx, dy, stepsAcross // 2, wait)
moveAllOnLine(faceList, -dx, dy, stepsAcross // 2, wait)
message = Text(Point(winWidth / 2, 20), 'Click here to quit')
message.draw(win)
win.getMouse()
win.close()
main()
就IDLE而言,我的经验是控制台窗口后面出现了图形窗口,因此您需要在启动时移动窗口。无论它是否符合你的意图,我都不知道: