import sys
# for TkInter GUI support
from Tkinter import *
import tkMessageBox
import tkColorChooser
# for COM support
import comtypes.client as cc
import comtypes
# Load the typelibrary registered with the Windows registry
tlb_id = comtypes.GUID("{FA3BF2A2-7220-47ED-8F07-D154B65AA031}")
cc.GetModule((tlb_id, 1, 0))
# Alternately you can use this method also by direct loading the dll file
#cc.GetModule("SimpleCOM.dll")
# Import the SimpleCOMLib library from comtypes
import comtypes.gen.SimpleCOMLib as SimpleCOMLib
# Application class for the GUI
class AppDlg:
# member variables
X1 = 0.0
Y1 = 0.0
Z1 = 0.0
X2 = 0.0
Y2 = 0.0
Z2 = 0.0
distVal = 0.0
# methods
# constructor
def __init__(self, master):
master.title("Test COM InterOp in Python/Tkinter")
master.maxsize(400, 210)
master.minsize(400, 210)
frame1 = Frame(master, padx=5, pady=5)
frame1.pack(anchor=N, side="top", fill=X, expand=Y)
# Point 1 Data
self.labelframe1 = LabelFrame(frame1, padx=5, pady=5, relief="groove", text="Point 1 Data")
self.labelframe1.pack(side=LEFT, fill=BOTH, expand=Y)
self.frameX1 = Frame(self.labelframe1)
self.frameY1 = Frame(self.labelframe1)
self.frameZ1 = Frame(self.labelframe1)
self.frameX1.pack()
self.frameY1.pack()
self.frameZ1.pack()
self.labelX1 = Label(self.frameX1, text="X")
self.labelX1.pack(side=LEFT, padx=2, pady=2)
self.entryX1 = Entry(self.frameX1)
self.entryX1.insert(0, self.X1)
self.entryX1.pack()
self.labelY1 = Label(self.frameY1, text="Y")
self.labelY1.pack(side=LEFT, padx=2, pady=2)
self.entryY1 = Entry(self.frameY1)
self.entryY1.insert(0, self.Y1)
self.entryY1.pack()
self.labelZ1 = Label(self.frameZ1, text="Z")
self.labelZ1.pack(side=LEFT, padx=2, pady=2)
self.entryZ1 = Entry(self.frameZ1)
self.entryZ1.insert(0, self.Z1)
self.entryZ1.pack()
self.colorbtn1 = Button(self.labelframe1, text="Select Color", command=self.onSelectColor1)
self.colorbtn1.pack(fill=BOTH, side=BOTTOM)
# Point 2 Data
self.labelframe2 = LabelFrame(frame1, padx=5, pady=5, relief="groove", text="Point 2 Data")
self.labelframe2.pack(side=RIGHT, fill=BOTH, expand=Y)
self.frameX2 = Frame(self.labelframe2)
self.frameY2 = Frame(self.labelframe2)
self.frameZ2 = Frame(self.labelframe2)
self.frameX2.pack()
self.frameY2.pack()
self.frameZ2.pack()
self.labelX2 = Label(self.frameX2, text="X")
self.labelX2.pack(side=LEFT, padx=2, pady=2)
self.entryX2 = Entry(self.frameX2)
self.entryX2.insert(0, self.X2)
self.entryX2.pack()
self.labelY2 = Label(self.frameY2, text="Y")
self.labelY2.pack(side=LEFT, padx=2, pady=2)
self.entryY2 = Entry(self.frameY2)
self.entryY2.insert(0, self.Y2)
self.entryY2.pack()
self.labelZ2 = Label(self.frameZ2, text="Z")
self.labelZ2.pack(side=LEFT, padx=2, pady=2)
self.entryZ2 = Entry(self.frameZ2)
self.entryZ2.insert(0, self.Z2)
self.entryZ2.pack()
frame2 = Frame(master, padx=5, pady=5)
frame2.pack()
self.labelDist = Label(frame2, text="Distance Between Point1 and Point2")
self.labelDist.pack(side=LEFT)
self.entryDist = Entry(frame2, textvariable=self.distVal)
self.entryDist.insert(0, self.distVal)
self.entryDist.pack()
self.colorbtn2 = Button(self.labelframe2, text="Select Color", command=self.onSelectColor2)
self.colorbtn2.pack(fill=BOTH, side=BOTTOM)
# Buttons
frame3 = Frame(master, padx=2, pady=2)
frame3.pack(anchor=E)
self.apply = Button(frame3, width=10, text="Apply", command=self.onApply)
self.apply.pack(side=LEFT, padx=2, pady=2)
self.close = Button(frame3, width=10, text="Close", command=master.quit)
self.close.pack(side=LEFT, padx=2, pady=2)
# variable to store colors
self.colorTuple1 = ((255, 255, 255), '#ffffff')
self.colorTuple2 = ((255, 255, 255), '#ffffff')
# Apply button callback
def onApply(self):
self.X1 = self.entryX1.get()
self.Y1 = self.entryY1.get()
self.Z1 = self.entryZ1.get()
self.X2 = self.entryX2.get()
self.Y2 = self.entryY2.get()
self.Z2 = self.entryZ2.get()
#print self.colorTuple1
#print self.colorTuple2
# Check if the user selects cancel on the color chooser
# in that case the tuple will contain None, None values
if self.colorTuple1[0] is None:
r = 255
g = 255
b = 255
else:
r = self.colorTuple1[0][0]
g = self.colorTuple1[0][1]
b = self.colorTuple1[0][2]
self.color1 = (((0xff & b) << 16) | ((0xff & g) << 8) | (0xff & r))
#print "Color Point1 is %d\n" % self.color1
if self.colorTuple2[0] is None:
r = 255
g = 255
b = 255
else:
r = self.colorTuple2[0][0]
g = self.colorTuple2[0][1]
b = self.colorTuple2[0][2]
self.color2 = (((0xff & b) << 16) | ((0xff & g) << 8) | (0xff & r))
#print "Color Point2 is %d\n" % self.color2
# Create COM Point1
self.aGrPoint = cc.CreateObject("SimpleCOM.GraphicPoint", None, None, SimpleCOMLib.IGraphicPoint)
self.aPoint = self.aGrPoint.QueryInterface(SimpleCOMLib.IPoint)
#help(self.aPoint)
self.aPoint.SetCoord(float(self.X1), float(self.Y1), float(self.Z1))
tkMessageBox.showinfo("From Python-Tkinter App", "Point 2 Created At X%0.3f, Y%0.3f, Z%0.3f" % (float(self.aPoint.X), float(self.aPoint.Y), float(self.aPoint.Z)))
self.aColor = self.aGrPoint.QueryInterface(SimpleCOMLib.IColor)
if self.colorTuple1:
self.aColor.SetColor(self.color1)
self.aGrPoint.Draw()
# Create COM Point2
self.aGrPoint2 = cc.CreateObject("SimpleCOM.GraphicPoint", None, None, SimpleCOMLib.IGraphicPoint)
self.aPoint2 = self.aGrPoint2.QueryInterface(SimpleCOMLib.IPoint)
#help(self.aPoint2)
self.aPoint2.SetCoord(float(self.X2), float(self.Y2), float(self.Z2))
tkMessageBox.showinfo("From Python-Tkinter App", "Point 2 Created At X%0.3f, Y%0.3f, Z%0.3f" % (float(self.aPoint2.X), float(self.aPoint2.Y), float(self.aPoint2.Z)))
self.aColor2 = self.aGrPoint2.QueryInterface(SimpleCOMLib.IColor)
if self.colorTuple2:
self.aColor2.SetColor(self.color2)
self.aGrPoint2.Draw()
self.distVal = self.aPoint.Distance(self.aPoint2)
self.entryDist.delete(0, END)
self.entryDist.insert(0, self.distVal)
# Color selection button callbacks
def onSelectColor1(self):
# tkColorChooser returns a tuple containing the RGB tuple and the html color value
self.colorTuple1 = tkColorChooser.askcolor()
if self.colorTuple1:
self.colorbtn1.configure(bg=self.colorTuple1[1])
def onSelectColor2(self):
self.colorTuple2 = tkColorChooser.askcolor()
if self.colorTuple2:
self.colorbtn2.configure(bg=self.colorTuple2[1])
# Start the TkInter root window
rootwin = Tk()
# Instantiate the GUI class object
AppDlg(rootwin)
# Run the TkInter main event loop
rootwin.mainloop()