Playing with Tkinter (run this)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kudos
    Recognized Expert New Member
    • Jul 2006
    • 127

    Playing with Tkinter (run this)

    There is alot of GUI packages for Python, but as far as I know the only one that "comes with python" is Tkinter. Here is a toy program that draws a funny shape



    Code:
    # basis for this stuff was found at this guys website : http://matt.wandel.ca/
    #kudos@spray.no
    
    from Tkinter import *
    import math 
    
    root = Tk()
    w = Canvas(root, width=300, height=300)
    theta = 0.015
    sx = 358.690012296
    sy = 452.337492999
    w.create_rectangle(0,0,300,300,fill="#000000")
    while(theta<4*3.1415):
     xt = math.sin(theta * 10) * 270 + 300 
     yt = math.cos(theta * 9.5) * 270 + 300 
     nthet = xt / 30 + yt / 30 
     yp = yt + math.sin(nthet) * 20
     xp = xt + math.cos(nthet) * 20 
     w.create_line(sx/2,sy/2,xp/2,yp/2,fill="#ffffff",width=2)
     sx = xp
     sy = yp
     theta+=0.004
    w.pack()
    root.mainloop()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    That's really neat, kudos. Here's my first attempt at a script using Tkinter, following your example. It is a 2D point inside/outside polygon test where the point to test is selected randomly. The polygon is drawn upside down, but that's OK with me for this example.[code=Python]import random
    from Tkinter import *

    class Point(object):

    def __init__(self, x=0.0, y=0.0):
    self.x = float(x)
    self.y = float(y)

    def __add__(self, other):
    return Point(self.x+ot her.x, self.y+other.y)

    def __sub__(self, other):
    return Point(self.x-other.x, self.y-other.y)

    def __mul__(self, f):
    return Point(self.x*f, self.y*f)

    def __div__(self, f):
    return Point(self.x/f, self.y/f)

    def ray(self, limit):
    return self, Point(limit, self.y)

    def inters(self, v1, v2):
    self.limit = max(v1.x, v2.x)+1
    P1, P2 = self.ray(limit)
    P3, P4 = v1, v2
    num_a = (P4.x-P3.x)*(P1.y-P3.y) - (P4.y-P3.y)*(P1.x-P3.x)
    num_b = (P2.x-P1.x)*(P1.y-P3.y) - (P2.y-P1.y)*(P1.x-P3.x)
    den = (P4.y-P3.y)*(P2.x-P1.x) - (P4.x-P3.x)*(P2.y-P1.y)
    self.ua = num_a/den
    self.ub = num_b/den
    x = P1.x + self.ua*(P2.x-P1.x)
    y = P1.y + self.ua*(P2.y-P1.y)
    self.px = Point(x,y)
    # print self.ua, self.ub
    if (0 <= self.ua <= 1) and (0 <= self.ub <= 1):
    return True
    return False

    def __iter__(self):
    for a in [self.x, self.y]:
    yield a

    def __str__(self):
    return 'Point(%0.4f, %0.4f)' % (self.x,self.y)

    def __repr__(self):
    return 'Point(%0.4f, %0.4f)' % (self.x,self.y)

    epsilon = 0.00001

    def common_vertex(p t,vertices):
    for v in vertices:
    if (abs(pt.x-v.x) < epsilon) and (abs(pt.y-v.y) < epsilon):
    return True
    return False

    def inside_polygon( pt, polygon):
    # Consider pt is inside if on a vertex
    if common_vertex(p t,polygon):
    print "The point lies on a polygon vertex."
    return True
    count = 0
    # check each line segment between vertices
    # 5 unique vertices represent a 5 sided polygon
    for i in range(len(polyg on)-1):
    if pt.inters(polyg on[i], polygon[i+1]):
    count += 1
    #if pt.ub in (0, 1):
    #print "The ray intersects at a vertex."
    # check line segment from last vertex to first vertex
    if pt.inters(polyg on[-1], polygon[0]):
    count += 1
    #if pt.ub in (0, 1):
    #print "The ray intersects at a vertex."
    # print count
    if count%2:
    #print "Point is INSIDE"
    return True
    else:
    #print "Point is OUTSIDE"
    return False

    limit = 151

    pt1 = Point(random.ch oice(range(0,li mit)), random.choice(r ange(0,limit)))
    print pt1
    polygon = [Point(122,122),
    Point(140,20),
    Point(100,34),
    Point(30,5),
    Point(19,90)]

    print inside_polygon( pt1, polygon)

    root = Tk()
    w = Canvas(root, width=limit+10, height=limit+10 )
    w.create_rectan gle(0,0,160,160 ,fill="#000000" )
    for i in range(len(polyg on)-1):
    w.create_line(p olygon[i].x, polygon[i].y, polygon[i+1].x, polygon[i+1].y,fill="#fffff f",width=1)
    w.create_line(p olygon[-1].x, polygon[-1].y, polygon[0].x, polygon[0].y,fill="#fffff f",width=1)
    w.create_line(p t1.x, pt1.y, pt1.ray(limit+1 0)[1].x, pt1.ray(limit+1 0)[1].y,fill="#fffff f",width=1)
    w.create_line(p t1.x, pt1.y-2, pt1.x, pt1.y+2,fill="# ffffff",width=1 )
    w.pack()
    root.mainloop()[/code]

    Comment

    Working...