#! /usr/bin/python # # Copyright 2009 Andy Shelley # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # import Tkinter module from Tkinter import * import tkMessageBox # import MySQL module import MySQLdb # database details server = "localhost" username = "root" password = "" database = "emc" # attempt to connect to database try: db = MySQLdb.connect(host=server, user=username, passwd=password, db=database) except MySQLdb.Error, e: tkMessageBox.showwarning("Failure", "Cannot connect to database\n" + e.args[1]) sys.exit (1) # create a cursor cursor = db.cursor() # execute SQL statement cursor.execute("SELECT * FROM gcode") # get the result set as a tuple result = cursor.fetchall() # test to see if there is any gcode in the database if len(result) == 0: tkMessageBox.showwarning("Failure", database + " contains no records\n") sys.exit (1) else: # create main window root = Tk() root.title("Component Database") root.geometry("300x150+300+200") # create label lbl = Label(root, text = "", bg="white", fg="midnightblue", font = ("arial", 16)) lbl.configure(text = "Component list from " + database + " database") lbl.grid(row = 0, column = 0, columnspan = 2) # create list box, and populate it list = Listbox(root, height=4, selectmode = SINGLE) for item in result: list.insert(END, item[2]) list.select_set(0) # create scrollbar scroll = Scrollbar(root, command = list.yview) list.configure(yscrollcommand = scroll.set) list.grid(row = 1, column = 0, sticky = NS) scroll.grid(row = 1, column = 1, sticky = NS) # import selected gcode def import_gcode(): x = list.curselection() sql = "SELECT gcode FROM gcode WHERE gcid='" + str(result[int(x[0])][0]) + "'" cursor.execute(sql) gcode = cursor.fetchall() for line in gcode: print line[0] root.destroy() # create a button to import gcode btn = Button(root, text = "Import", command = import_gcode) btn.grid(row = 2, column = 0, columnspan = 2) # start main window event loop root.mainloop()