Python论坛  - 讨论区

标题:[python-chinese] Tkinter

2005年11月07日 星期一 06:56

Shi Mu samrobertsmith at gmail.com
Mon Nov 7 06:56:01 HKT 2005

下面的代码可以运行,并且在CONSOLE上出现结果,
请问如何
在Tkinter里实现?
多谢了!
from math import sqrt
class Point:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def draw(self):
        print 'point at (%d,%d)'%(self.x,self.y)
class Line:
    def __init__(self,head,tail):
        self.head = head
        self.tail = tail
        dx = self.head.x - self.tail.x
        dy = self.head.y - self.tail.y
        self.length = sqrt(dx*dx + dy*dy)
    def draw(self):
        print 'dawing line'
        print 'line starts at: '
        self.head.draw()
        print 'line ends at: '
        self.tail.draw()
class Polygon:
    def __init__(self,lines):
        self.lines = lines
        self.__perimeter = None
    def draw(self):
        print 'drawing polygon'
        print 'poly has %d lines'%len(self.lines)
        for line in self.lines:
            line.draw()
            print 'polygon done'
    def perimeter(self):
        if not self.__perimeter:
            self.__perimeter = 0
            for line in self.lines:
                self.__perimeter += line.length
                if self.lines[0] != self.lines[-1]:
                    p2 = self.lines[0].head
                    p1 = self.lines[-1].tail
                    dx = p2.x - p1.x
                    dy = p2.y - p1.y
                    self.__perimeter += sqrt(dx*dx + dy*dy)
        return self.__perimeter
class Route:
    def __init__(self,legs):
        self.legs = legs
        d = 0
        for leg in self.legs:
            d += leg.length
            self.distance = d
    def draw(self):
        print 'drawing route'
        for leg in self.legs:
            leg.draw()
class Map:
    def __init__(self,objects):
        self.objects = objects
        self.draw()
    def draw(self):
        for object in self.objects:
            object.draw()
p1 = Point(1,2)
p2 = Point(3,4)
p3 = Point(7,8)
p4 = Point(12,1)
line1 = Line(p1,p2)
line2 = Line(p2,p3)
line3 = Line(p3,p1)
line4 = Line(p1,p3)
poly1 = Polygon([line1,line2,line3])
route1 = Route([line2,line2,line3,line4])
map = Map([p1,p2,p3,line1,line2,poly1])

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号