473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Playing with Tkinter (run this)

kudos
127 Expert 100+
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



Expand|Select|Wrap|Line Numbers
  1. # basis for this stuff was found at this guys website : http://matt.wandel.ca/
  2. #kudos@spray.no
  3.  
  4. from Tkinter import *
  5. import math 
  6.  
  7. root = Tk()
  8. w = Canvas(root, width=300, height=300)
  9. theta = 0.015
  10. sx = 358.690012296
  11. sy = 452.337492999
  12. w.create_rectangle(0,0,300,300,fill="#000000")
  13. while(theta<4*3.1415):
  14.  xt = math.sin(theta * 10) * 270 + 300 
  15.  yt = math.cos(theta * 9.5) * 270 + 300 
  16.  nthet = xt / 30 + yt / 30 
  17.  yp = yt + math.sin(nthet) * 20
  18.  xp = xt + math.cos(nthet) * 20 
  19.  w.create_line(sx/2,sy/2,xp/2,yp/2,fill="#ffffff",width=2)
  20.  sx = xp
  21.  sy = yp
  22.  theta+=0.004
  23. w.pack()
  24. root.mainloop()
  25.  
Oct 8 '08 #1
1 2515
bvdet
2,851 Expert Mod 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. import random
  2. from Tkinter import *
  3.  
  4. class Point(object):
  5.  
  6.     def __init__(self, x=0.0, y=0.0):
  7.         self.x = float(x)
  8.         self.y = float(y)
  9.  
  10.     def __add__(self, other):
  11.         return Point(self.x+other.x, self.y+other.y)
  12.  
  13.     def __sub__(self, other):
  14.         return Point(self.x-other.x, self.y-other.y)
  15.  
  16.     def __mul__(self, f):
  17.         return Point(self.x*f, self.y*f)
  18.  
  19.     def __div__(self, f):
  20.         return Point(self.x/f, self.y/f)
  21.  
  22.     def ray(self, limit):
  23.         return self, Point(limit, self.y)
  24.  
  25.     def inters(self, v1, v2):
  26.         self.limit = max(v1.x, v2.x)+1
  27.         P1, P2 = self.ray(limit)
  28.         P3, P4 = v1, v2
  29.         num_a = (P4.x-P3.x)*(P1.y-P3.y) - (P4.y-P3.y)*(P1.x-P3.x)
  30.         num_b = (P2.x-P1.x)*(P1.y-P3.y) - (P2.y-P1.y)*(P1.x-P3.x)
  31.         den = (P4.y-P3.y)*(P2.x-P1.x) - (P4.x-P3.x)*(P2.y-P1.y)
  32.         self.ua = num_a/den
  33.         self.ub = num_b/den
  34.         x = P1.x + self.ua*(P2.x-P1.x)
  35.         y = P1.y + self.ua*(P2.y-P1.y)
  36.         self.px = Point(x,y)
  37.         # print self.ua, self.ub
  38.         if (0 <= self.ua <= 1) and (0 <= self.ub <= 1):
  39.             return True
  40.         return False
  41.  
  42.     def __iter__(self):
  43.         for a in [self.x, self.y]:
  44.             yield a
  45.  
  46.     def __str__(self):
  47.         return 'Point(%0.4f, %0.4f)' % (self.x,self.y)
  48.  
  49.     def __repr__(self):
  50.         return 'Point(%0.4f, %0.4f)' % (self.x,self.y)
  51.  
  52. epsilon = 0.00001
  53.  
  54. def common_vertex(pt,vertices):
  55.     for v in vertices:
  56.         if (abs(pt.x-v.x) < epsilon) and (abs(pt.y-v.y) < epsilon):
  57.             return True
  58.     return False
  59.  
  60. def inside_polygon(pt, polygon):
  61.     # Consider pt is inside if on a vertex 
  62.     if common_vertex(pt,polygon):
  63.         print "The point lies on a polygon vertex."
  64.         return True
  65.     count = 0
  66.     # check each line segment between vertices
  67.     # 5 unique vertices represent a 5 sided polygon
  68.     for i in range(len(polygon)-1):
  69.         if pt.inters(polygon[i], polygon[i+1]):
  70.             count += 1
  71.             #if pt.ub in (0, 1):
  72.                 #print "The ray intersects at a vertex."
  73.     # check line segment from last vertex to first vertex      
  74.     if pt.inters(polygon[-1], polygon[0]):
  75.         count += 1
  76.         #if pt.ub in (0, 1):
  77.             #print "The ray intersects at a vertex."
  78.     # print count
  79.     if count%2:
  80.         #print "Point is INSIDE"
  81.         return True
  82.     else:
  83.         #print "Point is OUTSIDE"
  84.         return False
  85.  
  86. limit = 151    
  87.  
  88. pt1 = Point(random.choice(range(0,limit)), random.choice(range(0,limit)))
  89. print pt1
  90. polygon = [Point(122,122),
  91.            Point(140,20),
  92.            Point(100,34),
  93.            Point(30,5),
  94.            Point(19,90)]
  95.  
  96. print inside_polygon(pt1, polygon)
  97.  
  98. root = Tk()
  99. w = Canvas(root, width=limit+10, height=limit+10)
  100. w.create_rectangle(0,0,160,160,fill="#000000")
  101. for i in range(len(polygon)-1):
  102.     w.create_line(polygon[i].x, polygon[i].y, polygon[i+1].x, polygon[i+1].y,fill="#ffffff",width=1)
  103. w.create_line(polygon[-1].x, polygon[-1].y, polygon[0].x, polygon[0].y,fill="#ffffff",width=1)
  104. w.create_line(pt1.x, pt1.y, pt1.ray(limit+10)[1].x, pt1.ray(limit+10)[1].y,fill="#ffffff",width=1)
  105. w.create_line(pt1.x, pt1.y-2, pt1.x, pt1.y+2,fill="#ffffff",width=1)
  106. w.pack()
  107. root.mainloop()
Oct 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter...
3
by: Rob Andrews | last post by:
I'm on a Red Hat 9 system, which has Python 2.2.2 installed, and I installed 2.3 separately into /home/rob/Python-2.3/ (creating the symbolic link "py23" to point to my 2.3 installation). Now I'm...
2
by: Rick Olson | last post by:
I'm trying to add a Tkinter interface to an existing C program with embedded python, but seem to have trouble importing Tkinter (or accessing it). I tried a simple program that would run the...
2
by: Michael Zhang | last post by:
My project uses Python-2.3.4 + Tkinter + PIL-1.1.4 to retrieve images from server and display those images. I created a thread (also a separate toplevel window) for displaying images and another...
1
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the...
2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
2
by: Andrew Trevorrow | last post by:
Our app uses embedded Python to allow users to run arbitrary scripts. Scripts that import Tkinter run fine on Windows, but on Mac OS X there is a serious problem. After a script does "root = Tk()"...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
8
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.