diff --git a/README.md b/README.md index 9ce4b87..bf3b383 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ python ====== -Some python projects that I've created over the years. +Some python projects that I, scotty3785, have created over the years. + +Don't fork this unless you intend to contribute. Star it if you just want to keep a link to it. + +https://github.com/scotty3785/python diff --git a/SenseHatDraw.gif b/SenseHatDraw.gif new file mode 100644 index 0000000..e9838a9 Binary files /dev/null and b/SenseHatDraw.gif differ diff --git a/TK Widgets/table.py b/TK Widgets/table.py new file mode 100644 index 0000000..8339777 --- /dev/null +++ b/TK Widgets/table.py @@ -0,0 +1,124 @@ +#Modified by Scott Thomson to make table column width resizable. + +from tkinter import * +#import tkSimpleDialog + + +class MultiListbox(Frame): + def __init__(self, master, lists): + Frame.__init__(self, master) + self.master = master + self.titles = lists + self.lists = [] + self.tablePane = PanedWindow(self,orient=HORIZONTAL) + self.tablePane.pack(side=LEFT,fill=BOTH,expand=1) + for l,w in lists: + + frame = Frame(self.tablePane) + frame.pack(side=LEFT, expand=YES, fill=BOTH) + Label(frame, text=l, borderwidth=1, relief=RAISED).pack(fill=X) + lb = Listbox(frame, width=w, borderwidth=0, selectborderwidth=0, + relief=FLAT, exportselection=FALSE) + lb.pack(expand=YES, fill=BOTH) + self.lists.append(lb) + lb.bind('', lambda e, s=self: s._select(e.y)) + lb.bind('', lambda e, s=self: s._select(e.y)) + lb.bind('', lambda e: 'break') + lb.bind('', lambda e, s=self: s._b2motion(e.x, e.y)) + lb.bind('', lambda e, s=self: s._button2(e.x, e.y)) + # Scrolling whilst hovering over a list only scrolls one column + # this attempt at a fix doesn't work. + lb.bind('', self._mousewheel) + self.tablePane.add(frame) + frame = Frame(self) + Label(frame, borderwidth=1, relief=RAISED).pack(fill=X) + sb = Scrollbar(frame, orient=VERTICAL, command=self._scroll) + sb.pack(expand=YES, fill=Y) + #self.tablePane.add(frame,cursor=None,sashcursor=None) + frame.pack(side=RIGHT,expand=1,fill=Y,anchor=W) + self.lists[0]['yscrollcommand']=sb.set + + def _mousewheel(self,event): + #print(vars(event)) + scroll_by = int(-1*(event.delta/120)) + #print(scroll_by) + for l in self.lists: + #l.yview('scroll',scroll_by, 'units') + l.yview_scroll(scroll_by, "units") + + def _select(self, y): + row = self.lists[0].nearest(y) + self.selection_clear(0, END) + self.selection_set(row) + return 'break' + + def _button2(self, x, y): + for l in self.lists: l.scan_mark(x, y) + return 'break' + + def _b2motion(self, x, y): + for l in self.lists: l.scan_dragto(x, y) + return 'break' + + def _scroll(self, *args): + #print(args) + for l in self.lists: + #apply(l.yview, args) + l.yview(*args) + + def curselection(self): + return self.lists[0].curselection() + + def delete(self, first, last=None): + for l in self.lists: + l.delete(first, last) + + def get(self, first, last=None): + result = [] + for l in self.lists: + result.append(l.get(first,last)) + if last: + #return apply(map, [None] + result) + return map([None] + result) + return result + + def index(self, index): + self.lists[0].index(index) + + def insert(self, index, *elements): + for e in elements: + i = 0 + for l in self.lists: + l.insert(index, e[i]) + i = i + 1 + + def size(self): + return self.lists[0].size() + + def see(self, index): + for l in self.lists: + l.see(index) + + def selection_anchor(self, index): + for l in self.lists: + l.selection_anchor(index) + + def selection_clear(self, first, last=None): + for l in self.lists: + l.selection_clear(first, last) + + def selection_includes(self, index): + return self.lists[0].selection_includes(index) + + def selection_set(self, first, last=None): + for l in self.lists: + l.selection_set(first, last) + +if __name__ == '__main__': + tk = Tk() + Label(tk, text='MultiListbox').pack() + mlb = MultiListbox(tk, (('Field Name', 40),('Type', 40), ('Data', 20))) + for i in range(50): + mlb.insert(END, ('Field %d' % i,'Binary%d' % i, 'Data%d' % i)) + mlb.pack(expand=YES,fill=BOTH) + tk.mainloop() diff --git a/python-weather/weather_get.py b/python-weather/weather_get.py index ac74d45..8c4bb8f 100644 --- a/python-weather/weather_get.py +++ b/python-weather/weather_get.py @@ -2,59 +2,67 @@ # -*- coding: utf-8 -*- # # untitled.py -# +# # Copyright 2012 Scott Thomson -# +# # 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., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. -# -# +# +# import urllib import json +units = "metric" # metric or imperial previous_weather_file = "weather_log.txt" +api_key_path = "api_key.txt" previous_weather = "" +city = "Cheltenham,uk" + try: log = open(previous_weather_file,"r") previous_weather = log.read() log.close() + with open(api_key_path) as f: + api_key = f.read() + except: print "No previous data" -f = urllib.urlopen("http://api.openweathermap.org/data/2.5/weather?q=Cheltenham,uk") +if len(api_key) <= 1: + print "api-key required." + print "Create an account at www.openweathermap.org, and add your api-key to './api_key.txt'" + exit(0) + +f = urllib.urlopen("http://api.openweathermap.org/data/2.5/weather" +"?q=%s&appid=%s&units=%s" % (city, api_key, units)) weather = f.read() log = open(previous_weather_file,'w') log.write(weather) log.close() - - weather_json = json.loads(weather) #print weather #print weather_json['weather'] -curr_temp = float(weather_json['main']['temp'])-273.13 -print "Temperature is: %.2f degrees C" % (curr_temp) - +curr_temp = float(weather_json['main']['temp']) +print "Temperature is: %.2f degrees" % (curr_temp) if (not previous_weather == ""): prev_weather_json = json.loads(previous_weather) - prev_temp = float(prev_weather_json['main']['temp'])-273.13 + prev_temp = float(prev_weather_json['main']['temp']) temp_diff = curr_temp - prev_temp - - if not( temp_diff == 0.0): - print "Temperature has changed by: %.2f degrees C" % (temp_diff) - + if not( temp_diff == 0.0): + print "Temperature has changed by: %.2f degrees" % (temp_diff) diff --git a/tkinter-keypad/keypad.py b/tkinter-keypad/keypad.py new file mode 100644 index 0000000..beb9251 --- /dev/null +++ b/tkinter-keypad/keypad.py @@ -0,0 +1,77 @@ +from tkinter import * +from tkinter import simpledialog + +class App(Frame): + def __init__(self,parent=None,**kw): + Frame.__init__(self,parent,**kw) + self.textEntryVar = StringVar() + self.e = Entry(self, width=10, background='white', textvariable=self.textEntryVar, justify=CENTER, font='-weight bold') + self.e.grid(padx=10, pady=5, row=17, column=1, sticky='W,E,N,S') + self.e.bind('',self.numpadEntry) + self.e.bind('',self.numpadExit) + self.edited = False + def numpadEntry(self,event): + if self.edited == False: + print("You Clicked on me") + self.e['bg']= '#ffffcc' + self.edited = True + new = numPad(self,self) + else: + self.edited = False + + def numpadExit(self,event): + self.edited = False + self.e['bg']= '#ffffff' + + +class numPad(simpledialog.Dialog): + def __init__(self,master=None,parent=None): + self.parent = parent + self.top = Toplevel(master=master) + self.top.protocol("WM_DELETE_WINDOW",self.ok) + self.createWidgets() + def createWidgets(self): + btn_list = ['7', '8', '9', '4', '5', '6', '1', '2', '3', '0', 'Close', 'Del'] + # create and position all buttons with a for-loop + # r, c used for row, column grid values + r = 1 + c = 0 + n = 0 + # list(range()) needed for Python3 + btn = [] + for label in btn_list: + # partial takes care of function and argument + cmd = lambda x = label: self.click(x) + # create the button + cur = Button(self.top, text=label, width=10, height=5, command=cmd) + btn.append(cur) + # position the button + btn[-1].grid(row=r, column=c) + # increment button index + n += 1 + # update row/column position + c += 1 + if c == 3: + c = 0 + r += 1 + def click(self,label): + print(label) + if label == 'Del': + currentText = self.parent.textEntryVar.get() + self.parent.textEntryVar.set(currentText[:-1]) + elif label == 'Close': + self.ok() + else: + currentText = self.parent.textEntryVar.get() + self.parent.textEntryVar.set(currentText+label) + def ok(self): + self.top.destroy() + self.top.master.focus() + + +if __name__ == '__main__': + root = Tk() + root.geometry("200x100") + app = App(root) + app.grid() + root.mainloop() \ No newline at end of file