-1

这是我的整个代码:

from Graphics import *
import random
import time
from Myro import *
pt = Point(100,50)
pink = makeColor(200,100,150)
black = makeColor(0,0,0)
red = makeColor(255, 0, 0)
green = makeColor(0, 255, 0)
blue = makeColor(0, 0, 255)
purple = makeColor(255, 0, 255)
orange = makeColor(255, 153, 0)

win = Window("name", 1000, 500)
p1 = Point(0,0)
p2 = Point(200, 300)
p3 = Point(200,0)
d3 = Dot(p3)
p4 = Point(400, 300)
d4 = Dot(p4)
p5 = Point(400, 0)
p6 = Point(600, 300)
p7 = Point(600, 0)
p8 = Point(800,300)
p9 = Point(800,0)
p0 = Point(1000, 300)

win.setBackground(pink)
class Classes(object):
    WIDTH = 200
    HEIGHT = 300

    five = Rectangle(p9, p0)
    five.setOutline(black)
    five.setFill(orange)
    five.draw(win)
    four = Rectangle(p7, p8)
    four.setOutline(black)
    four.setFill(purple)
    four.draw(win)
    three = Rectangle(p5, p6)
    three.setOutline(black)
    three.setFill(blue)
    three.draw(win)
    two = Rectangle(p3, p4)
    two.setOutline(black)
    two.setFill(green)
    two.draw(win)
    one = Rectangle(p1, p2)
    one.setOutline(black)
    one.setFill(red)
    one.draw(win)

    '''def __init__(self,p,win):
        def numClasses(self):
            num = ask("How many classes do you have? Enter a number 1-5")
            int(num)
            if num == 1:


        def askOne(self):
            one = ask'''

'''class classOne(Classes):
    def __init__(self, win):
        Classes.__init__(self, win)
        self.appearance.setFill(red)
        self.appearance.setOutline(black)'''

        #self.append(win)
class classTwo(Classes):
    def __init__(self, win):
        Classes.__init__(self,win)
        self.appearance= Text(Point(100, 10), "Class 1")
        self.appearance.fontSize = 10
        self.appearance.setFill(black)
        self.appearance.draw(win)
        win.flip()
class classThree(Classes):
    def __init__(self, win):
        Classes.__init__(self,  win)
        self.appearance.setFill(blue)
        self.appearance.setOutline(black)
class classFour(Classes):
    def __init__(self,  win):
        Classes.__init__(self,  win)
        self.appearance.setFill(orange)
        self.appearance.setOutline(black)
class classFive(Classes):
    def __init__(self,  win):
        Classes.__init__(self, win)
        self.appearance.setFill(purple)
        self.appearance.setOutline(black)

t = time.strftime("%Y-%m-%d")
ti = Text(Point(win.getWidth()/2, 475), t)
ti.fontSize = 26
ti.setFill(black)
ti.draw(win)

title = Text(Point(win.getWidth()/2, 440), "Schedule for the week of:")
title.setFill(black)
title.fontSize = 20
title.draw(win)

classes = []

另一件事,Window 是我正在使用的版本中的一个函数,我无法定义它,因为它是预定义的。它只是打开一个单独的窗口(1000 x 500)像素,您可以在其中绘制东西。我只需要知道当在课程下输入文本时如何显示文本。它适用于矩形/点/形状,但不适用于文本。我不知道为什么。

4

2 回答 2

1

首先 - 虽然你描述了类(你告诉 Python classThree 应该是什么样子以及它应该如何工作),但你实际上从未创建过一个类!

其次 - 如果您绘制文本,那么框,框将覆盖文本并且您将看不到它。

第三 - 你真的在这里严重滥用课程。看起来您正在尝试制作一个学校日历,并为每个学校期间制作一个单独的 Python 课程 - 获取应该是数据的内容并将其作为代码进行硬连接。相反,您应该有一个通用的 Period Python 类,然后在每个学校期间创建一个单独的实例。

即,而不是

a = FirstPeriod()
b = SecondPeriod()
c = ThirdPeriod()

你应该考虑

a = Period("First", blue, black)
b = Period("Second", orange, black)
c = Period("Third", purple, black)

为什么?

  • 它使你想要完成的事情更加明显
  • 它减少了代码量(从而减少了调试量)
  • 它使以后的更改变得更加容易

编辑:这是一些经过大量重组的代码 - 我没有安装 Calico,因此它未经测试,但我希望它能给你一个想法:

import Myro
from Graphics import Window, Point, Dot, Text, makeColor
import random
import time

Black  = makeColor(0,0,0)
White  = makeColor(255,255,255)
Red    = makeColor(255, 0, 0)
Green  = makeColor(0, 255, 0)
Blue   = makeColor(0, 0, 255)
Purple = makeColor(255, 0, 255)
Pink   = makeColor(200,100,150)
Orange = makeColor(255, 153, 0)
Grey   = makeColor(165, 165, 165)

Monday, Tuesday, Wednesday, Thursday, Friday = range(5)

class Period(object):
    def __init__(self,
        className="<spare>",
        textColor=Black,
        bgColor=White,
        borderColor=Black,
        schedule=None
    ):
        self.name     = className
        self.text     = textColor
        self.bg       = bgColor
        self.border   = borderColor
        self.schedule = schedule or []

    def draw(self, win, rows, columns):
        for day,(start,length) in self.schedule:
            # draw background box
            box = Rectangle(
                Point(columns[day],  rows[start]),
                Point(columns[day+1],rows[start+length])
            )
            box.setFill(self.bg)
            box.setOutline(self.border)
            box.draw(win)
            # draw class name
            label = Text(Point(columns[day]+10,rows[start]+40), self.name)
            label.fontSize = 9
            label.setFill(self.text)
            label.draw(win)

def Week(object):
    def __init__(self, label, periods=None):
        self.label   = label
        self.periods = periods or []

    def draw(self, win, left, top, width, height):
        # how much room to reserve at the top
        label_space = 40

        # draw label at top
        label = Text(Point(Point(left+0.5*width, top+0.5*label_space)), self.label)
        label.fontSize = 20
        label.setFill(Black)
        label.draw(win)

        # figure out the grid for displaying the calendar boxes
        days = 5
        columns = [left + width*n/days for n in range(days+1)]
        periods = 5
        rows = [top + label_space + (height-label_space)*n/periods for n in range(periods+1)]

        # draw class periods based on the grid
        for p in self.periods:
            p.draw(win, rows, columns)

def main():
    win = Window("My window", 1000, 500)
    win.setBackground(Pink)

    week = Week("14 May 2012",
        [
            Period("Math",    bgColor=Red,   schedule=[(Monday,(0,1)), (Wednesday,(0,1)), (Friday,(0,1))]),
            Period("Geology", bgColor=Grey,  schedule=[(Monday,(1,1)), (Tuesday,(0,1)), (Thursday,(0,1))]),
            Period("English", bgColor=Blue,  schedule=[(Tuesday,(1,1)), (Wednesday,(3,1)), (Thursday,(1,1))]),
            Period("LUNCH",   bgColor=White, schedule=[(Monday,(2,1)), (Tuesday,(2,1)), (Wednesday,(2,1)), (Thursday,(2,1)), (Friday,(2,1))]),
            Period("Gym",     bgColor=Orange, schedule=[(Tuesday,(3,2)), (Thursday,(3,2))])
        ]
    )
    week.draw(win, 10, 10, 980, 290)
    # have to do something here to prevent closing immediately?

if __name__=="__main__":
    main()
于 2012-05-16T16:06:03.997 回答
0

如果您创建类的实例,您的代码应该将文本写入屏幕。所以如果你在你的课程下面添加这个,

c2 = classTwo(win)

它应该将 classTwo 的对象绘制到窗口。

另外,我之前误解了 win.flip() 部分。它不应该在代码中。

于 2012-05-16T03:12:04.783 回答