I am currently nearing the end of the final unit in my A - Level computing course in which we are developing a system for a company. My choice of system is a KML generator which creates placemarks with details on rescues. I have developed a working KML and GUI but don't have the knowledge on how to implement the KML into Tkinter. The result should be a system where the user inputs the details into the KML code via the GUI, which on clicking the "Print KML" button should copy the code to the clipboard to be pasted into Google Earth. I am using Python 2.6.2 as it's the version which is compatible with KML. If anyone has knowledge on how this can be done help would be appreciated.

KML Code:

<?xml version="1.0" encoding="UTF-8"?> 

<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
	<name>KmlFile</name>
	<Schema name="MissionDetails" id="MissionDetailsId0">
		<SimpleField type="string" name="ReportNumberValue"><displayName>&lt;b&gt;ReportNumber&lt;/b&gt;</displayName>
</SimpleField>
		<SimpleField type="int" name="Date"><displayName>&lt;b&gt;Date&lt;/b&gt;</displayName>
</SimpleField>
		<SimpleField type="double" name="RotorStart"><displayName>&lt;b&gt;RotorStart&lt;/b&gt;</displayName>
</SimpleField>
		<SimpleField type="double" name="RotorStop"><displayName>&lt;b&gt;RotorStop&lt;/b&gt;</displayName>
</SimpleField>
		<SimpleField type="string" name="Nature"><displayName>&lt;b&gt;Nature&lt;/b&gt;</displayName>
</SimpleField>
		<SimpleField type="string" name="AlertedBy"><displayName>&lt;b&gt;AlertedBy&lt;/b&gt;</displayName>
</SimpleField>
		<SimpleField type="string" name="Captain"><displayName>&lt;b&gt;Captain&lt;/b&gt;</displayName>
</SimpleField>
		<SimpleField type="string" name="CoPilot"><displayName>&lt;b&gt;CoPilot&lt;/b&gt;</displayName>
</SimpleField>
		<SimpleField type="string" name="Helicopter"><displayName>&lt;b&gt;Helicopter&lt;/b&gt;</displayName>
</SimpleField>
	</Schema>
	<Placemark>
		<name>Test Marker</name>
<ExtendedData>
<SchemaData schemaUrl="#MissionDetailsId0">			
<SimpleData name="Nature">Diver overdue or missing</SimpleData>
				<SimpleData name="AlertedBy">Portland CG</SimpleData>
				<SimpleData name="Captain">Balls K.</SimpleData>
				<SimpleData name="CoPilot">Stracey G.</SimpleData>
				<SimpleData name="Helicopter">G-CGWB</SimpleData>
			</SchemaData>
			<Data name="Sar Report Number">
				<value>9210</value>
			</Data>
			<Data name="Date">
				<value>26/06/2010</value			</Data>
			<Data name="Rotor Start">
				<value>13.19</value>
			</Data>
			<Data name="Rotor Stop">
				<value>13.50</value>
			</Data>
		</ExtendedData>
		<Point>
			<coordinates>-1.957759,50.60696,0</coordinates>
		</Point>
	</Placemark>
</Document>
</kml>

Tkinter Code:

from Tkinter import *

from tkMessageBox import askokcancel          

class Quitter(Frame):                         
    def __init__(self, parent=None):          
        Frame.__init__(self, parent)
        self.pack()
        widget = Button(self, text='Quit', command=self.quit)
        widget.pack(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = askokcancel('Verify exit', "Really quit?")
        if ans: Frame.quit(self)


fields = 'SAR Reprot No.', 'Rotor Start', 'Rotor Stop', 'Nature', 'Alerted By', 'Captian', 'Co Pilot', 'Helicopter','Latitude','Longitude'

def fetch(entries):
    for entry in entries:
        print 'Input => "%s"' % entry.get()   

def makeform(root, fields):
    entries = []
    for field in fields:
        row = Frame(root)                     
        lab = Label(row, width=15, text=field) 
        ent = Entry(row)
        row.pack(side=TOP, fill=X)            
        lab.pack(side=LEFT)
        ent.pack(side=RIGHT, expand=YES, fill=X)
        entries.append(ent)
    return entries

if __name__ == '__main__':
    root = Tk()
    ents = makeform(root, fields)
    root.bind('<Return>', (lambda event, e=ents: fetch(e)))   
    Button(root, text='Print KML',
                 command=(lambda e=ents: fetch(e))).pack(side=LEFT)
    Quitter(root).pack(side=RIGHT)
    root.mainloop()

Recommended Answers

All 10 Replies

replace the values with {variable_name} and use format.

Yes: requires Mark Hammond's win32all package (http://sourceforge.net/projects/pywin32/

The readme says:

Welcome to the downloads for pywin32.

To download pywin32, please select the "View All Downloads" button, then

select the installer executable for your system. Note that there is one

download package for each supported version of Python - please check what

version of Python you have installed and download the corresponding

package.

Some packages have a 32bit and a 64bit version available - you must download

the one which corresponds to the Python you have installed. Even if you have

a 64bit computer, if you installed a 32bit version of Python you must install

the 32bit version of pywin32.

If the installation process informs you that Python is not found in the

registry, it almost certainly means you have downloaded the wrong version -

either for the wrong version of Python, or the wrong "bittedness".

A changelog can be found at http://pywin32.cvs.sourceforge.net/viewvc/pywin32/pywin32/CHANGES.txt?view=markup

This moment latest looks like to be Build216:

http://sourceforge.net/projects/pywin32/files/pywin32/Build216/

How would I make the copy function a command in the button?

How would I make the copy function a command in the button?

You would do just loop like in your fetch function and instead of printing to screen print with the template to clipboard.

I'm sorry, but I'm completely new to Python and programming in general. I don't have the knowledge to understand or incorporate any of this advice. Does anyone have a working example of something similar that I can work from?

Edit: Found this piece of code which works perfectly when appending text to the clipboard. Still trying to turn it into a button command.

New code:

from Tkinter import *

from tkMessageBox import askokcancel          

class Quitter(Frame):                         
    def __init__(self, parent=None):          
        Frame.__init__(self, parent)
        self.pack()
        widget = Button(self, text='Quit', command=self.quit)
        widget.pack(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = askokcancel('Verify exit', "Really quit?")
        if ans: Frame.quit(self)


fields = 'Name','SAR Reprot No.', 'Rotor Start', 'Rotor Stop', 'Nature', 'Alerted By', 'Captian', 'Co Pilot', 'Helicopter','Latitude','Longitude'

def callback(e):
    r = Tk() 
    r.withdraw() 
    r.clipboard_clear() 
    r.clipboard_append('<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"><Document><name>KmlFile</name><Schema name="MissionDetails" id="MissionDetailsId"><SimpleField type="string" name="ReportNumberValue"><displayName>&lt;b&gt;ReportNumber&lt;/b&gt;</displayName></SimpleField><SimpleField type="int" name="Date"><displayName>&lt;b&gt;Date&lt;/b&gt;</displayName></SimpleField><SimpleField type="double" name="RotorStart"><displayName>&lt;b&gt;RotorStart&lt;/b&gt;</displayName></SimpleField><SimpleField type="double" name="RotorStop"><displayName>&lt;b&gt;RotorStop&lt;/b&gt;</displayName></SimpleField><SimpleField type="string" name="Nature"><displayName>&lt;b&gt;Nature&lt;/b&gt;</displayName></SimpleField><SimpleField type="string" name="AlertedBy"><displayName>&lt;b&gt;AlertedBy&lt;/b&gt;</displayName></SimpleField><SimpleField type="string" name="Captain"><displayName>&lt;b&gt;Captain&lt;/b&gt;</displayName></SimpleField><SimpleField type="string" name="CoPilot"><displayName>&lt;b&gt;CoPilot&lt;/b&gt;</displayName></SimpleField><SimpleField type="string" name="Helicopter"><displayName>&lt;b&gt;Helicopter&lt;/b&gt;</displayName></SimpleField></Schema><Placemark><name>{fetch entry: Name}</name><ExtendedData><SchemaData schemaUrl="#MissionDetailsId"><SimpleData name="Nature">Diver overdue or missing</SimpleData><SimpleData name="AlertedBy">Portland CG</SimpleData><SimpleData name="Captain">Balls K.</SimpleData><SimpleData name="CoPilot">Stracey G.</SimpleData><SimpleData name="Helicopter">G-CGWB</SimpleData></SchemaData><Data name="sarReportNumber"><value>9210</value></Data><Data name="date"><value>26062010</value></Data><Data name="rotorStart"><value>13.19</value></Data><Data name="rotorStop"><value>13.50</value></Data></ExtendedData><gx:balloonVisibility>1</gx:balloonVisibility><Point><coordinates>-1.957759,50.60696,0</coordinates></Point></Placemark></Document></kml>') 
    r.destroy()   

def makeform(root, fields):
    entries = []
    for field in fields:
        row = Frame(root)                     
        lab = Label(row, width=15, text=field) 
        ent = Entry(row)
        row.pack(side=TOP, fill=X)            
        lab.pack(side=LEFT)
        ent.pack(side=RIGHT, expand=YES, fill=X)
        entries.append(ent)
    return entries

if __name__ == '__main__':
    root = Tk()
    ents = makeform(root, fields)
    root.bind('<Return>', (lambda event, e=ents: callback(e)))   
    Button(root, text='Print KML',
                 command=(lambda e=ents: callback(e))).pack(side=LEFT)
    Quitter(root).pack(side=RIGHT)
    root.mainloop()

Generates the code on button press now. Just need to append the values entered in fields within the KML source code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.