|
|
from tkinter import *
class ButtonWithCheck: def __init__(self,root,name): self.name = name self.root = root self.checked = BooleanVar() self.checkbox = Checkbutton(self.root,variable=self.checked,state = DISABLED) self.button = Button(self.root,text = name,command = self.check) def check(self): self.checkbox.toggle() def get_status(self): return self.checked.get() def grid(self,row,start_col): col = start_col self.checkbox.grid(row = row,column = col,sticky=E) col += 1 self.button.grid(row = row,column = col,sticky=E+W) col += 1 return col
class SunkenText: def __init__(self,root,text): self.text = text self.root = root self.label = Label(self.root,text = self.text,relief = "sunken") def grid(self,row,start_col): col = start_col self.label.grid(row = row,column = col,sticky=E+W) col += 1 return col
class Row: labels = ["","Product name","Price","Count","Total"] def __init__(self,root,info): self.root = root self.name = info['name'] self.price = info['price'] self.count = info['count'] self.total = self.price * self.count
self.button = ButtonWithCheck(self.root,self.name)
self.label_price = SunkenText(self.root,'${}'.format(self.price)) self.label_count = SunkenText(self.root,str(info['count'])) self.label_total = SunkenText(self.root,'${}'.format(self.total))
def grid(self,row_num): col = self.button.grid(row_num,0) col = self.label_price.grid(row_num,col) col = self.label_count.grid(row_num,col) col = self.label_total.grid(row_num,col)
def get_status(self): return self.button.get_status()
class MainWindow: def __init__(self,prices): self.root = Tk() self.root.wm_title("Today's Prices") self.root.protocol("WM_DELETE_WINDOW", self.root.quit) self.root.wm_attributes("-topmost", True) self.rows = sorted(map(lambda row: Row(self.root,row),prices),key = lambda r: r.name) offset = 0 for i,label in enumerate(Row.labels): Label(self.root,text = label).grid(row = offset,column = i) offset += 1 for row in self.rows: row.grid(offset) offset += 1 total = sum(row.total for row in self.rows) individual = sum(row.price for row in self.rows)
Label(self.root,text = "Total:").grid(row = offset,column=Row.labels.index("Total")-1) Label(self.root,text = "${}".format(total)).grid(row = offset,column = Row.labels.index("Total")) Label(self.root,text = "Individual:").grid(row = offset,column = Row.labels.index("Price")-1,sticky = E) Label(self.root,text = "${}".format(individual)).grid(row = offset,column = Row.labels.index("Price")) offset += 1 Button(self.root,text = "OK", command = self.root.quit).grid(row=offset) def start(self): mainloop() self.root.withdraw()
def get_status(self): return dict( (row.name,row.get_status()) for row in self.rows )
if __name__ == "__main__": from decimal import Decimal as D info = [{'name': 'battery', 'link': 2, 'count': 4, 'purchased': 0, 'product_name': 'AHTECH Infinity 1300mah 14.8V 90C 4S1P Race', 'price': D('24.99'), 'time': '11:52:37.877501'}, {'name': 'charger', 'link': 6, 'count': 1, 'purchased': 0, 'product_name': 'Turnigy Reaktor 250W 10A 1-6S Balance Charger', 'price': D('52.88'), 'time': '15:05:16.120764'}, {'name': 'controller', 'link': 11, 'count': 1, 'purchased': 0, 'product_name': 'FrSky Taranis Q X7 2.4GHz 16CH Transmitter', 'price': D('104.99'), 'time': '15:26:24.942471'}, {'name': 'drone', 'link': 0, 'count': 1, 'purchased': 1, 'product_name': 'X215 PRO 215mm FPV Racing Drone BNF', 'price': D('212.35'), 'time': '11:41:46.865581'}, {'name': 'fpv', 'link': 7, 'count': 1, 'purchased': 1, 'product_name': 'Aomway Commander Goggles V1 FPV 2D 3D', 'price': D('299.00'), 'time': '15:09:55.486299'}, {'name': 'parallel_charger', 'link': 5, 'count': 1, 'purchased': 0, 'product_name': 'Paraboard Parallel Charging Board for Lipos with', 'price': D('19.99'), 'time': '15:04:41.003013'}, {'name': 'props', 'link': 4, 'count': 4, 'purchased': 0, 'product_name': 'DAL T5046 Cyclone Tri-Blade', 'price': D('2.79'), 'time': '15:03:59.716362'}, {'name': 'power_supply', 'link': 12, 'count': 1, 'purchased': 0, 'product_name': 'Turnigy Reaktor Pro 240W 16A Power Supply', 'price': D('34.29'), 'time': '15:27:19.812154'}] window = MainWindow(info) window.start()
|