IDLE3.6.1只是运行控制台窗口,而不是执行

2024-06-26 00:30:19 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

Tags: moveclonedefwinheadpointcenterdraw
1条回答
网友
1楼 · 发布于 2024-06-26 00:30:19

我发现您的代码有几个问题:即使@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()

至于空闲,我的经验是,图形窗口出现在控制台窗口后面,因此您需要在启动时移动窗口。我不知道它是否如你所愿:

enter image description here

相关问题 更多 >