如何在Python(GUI)中绘制一个面

时间:2016-12-05 23:58:20

标签: python

让我先说一下我只使用过Java,所以Python对我来说真的很新。结果,即使检查我的程序是否有效,我也遇到了很多麻烦。令人沮丧。 (顺便说一下,我正在使用Python 2.7)

这目前产生一个' X'当我按下" Eyes"按钮,但我应该像这里的那样做一张脸:https://www.cs.montana.edu/hunterl/132/labs/Python/

我无法弄清楚如何画脸。我知道它与

有关
c = Circle(Point(50,50), 10)
    c.draw(win)

但每次我尝试在python中实现它时,都没有任何反应。我不知道我需要做些什么来改变' X'并制作一个圆圈,两个较小的圆圈用于眼睛,一条线条用于嘴巴。任何帮助将非常感激。我完全失去了,并且非常想放弃。

以下是代码:

from button import Button 

from tick import Tick

def main():
    win = GraphWin("Face Mover", 250, 250)
    win.setCoords(0, 0, 10, 10)
    win.setBackground("green")

    tic = Tick(win, Point(5,5), 2)  #the window, middle, size of drawing

    eyesButt = Button(win, Point(3, 1), 3, 1, "Eyes")
    talkButt = Button(win, Point(7, 1), 3, 1, "Talk")
    quitButt = Button(win, Point(5, 9), 2, 1, "Quit")

    eyesButt.activate()
    talkButt.activate()
    quitButt.activate()

    pt = win.getMouse()
    while not quitButt.clicked(pt):
        if eyesButt.clicked(pt):
            tic.newSpot(win)
        pt = win.getMouse()

    win.close()
     main()

下一堂课

from graphics import *
from random import randint

class Tick:
    def __init__(self, window, center, size):
        self.center = center

        p1 = Point(randint(1,9), randint(1,8))
        self.p2, self.p3 = p1.clone(), p1.clone()
        self.p2.move(-.3, -.3)
        self.p3.move(.3, .3)
        self.p4, self.p5 = p1.clone(), p1.clone()
        self.p4.move(-.3, .3)
        self.p5.move(.3, -.3)

        self.line1 = Line(self.p2, self.p3)
        self.line2 = Line(self.p4, self.p5)

        self.line1.draw(window)
        self.line2.draw(window)

    def newSpot(self, window):
        self.line1.undraw()
        self.line2.undraw()
        self.placeSpot()
        self.line1 = Line(self.p2, self.p3)
        self.line2 = Line(self.p4, self.p5)

        self.line1.draw(window)
        self.line2.draw(window)


    def placeSpot(self):
        p1 = Point(randint(1,9), randint(1,8))
        self.p2, self.p3 = p1.clone(), p1.clone()
        self.p2.move(-.3, -.3)
        self.p3.move(.3, .3)
        self.p4, self.p5 = p1.clone(), p1.clone()
        self.p4.move(-.3, .3)
        self.p5.move(.3, -.3)

1 个答案:

答案 0 :(得分:0)

问题是

win.setCoords(0, 0, 10, 10)

Circle(Point(50,50), 10)

你的圈子在窗外,所以你看不到它。

删除win.setCoords(0, 0, 10, 10)或使用ie。 Circle(Point(5,5), 3)查看窗口内的圆圈。