Browse Source

Re-wrote quad_dialog.py in an oop fashion

Much easier to debug now
master
Raphael Roberts 7 years ago
parent
commit
1491b48da6
  1. 139
      interface/quad_dialog.py
  2. 55
      interface/test_price_new.py

139
interface/quad_dialog.py

@ -1,71 +1,92 @@
# import saveto
import datetime
from decimal import Decimal as D
from tkinter import *
def dicter(l):
ret = {}
for thing in l:
name = thing['name']
ret[name] = thing
return ret
def message(qpb,qpd):
qpb,qpd = map(dicter,(qpb,qpd))
def button_callback(arg):
check_boxes[arg].toggle()
prods = list(filter(lambda item: not item[1]['purchased'],qpb.items()))
root = Tk()
checks = dict(
(prod[0],BooleanVar()) for prod in prods)
root.wm_title("Today's Prices")
labels = ["","Product name","Price","Count","Total"]
for i,label in enumerate(labels):
Label(root,text = label).grid(row =0,column = i)
from decimal import Decimal as D
row = 1
total = 0
single_total = 0
buttons = []
check_boxes = []
for prod,data in prods:
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
check_boxes.append(
Checkbutton(root,variable=checks[prod],state = DISABLED)
)
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
check_boxes[-1].grid(row = row,column = labels.index(""),stick=E)
class Row:
labels = ["","Product name","Price","Count","Total"]
def __init__(self,root,info):
self.root = root
self.name = info['name']
self.price = D(info['price'])
self.count = info['count']
self.total = self.price * self.count
buttons.append(
Button(
root,text=prod,command = lambda row=row-1: button_callback(row)
)
)
self.button = ButtonWithCheck(self.root,self.name)
buttons[-1].grid(row=row,column = labels.index("Product name"),sticky = E+W)
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))
price = D(qpd[prod]['price'])
row_total = data['count']*price
def grid(self,row_num):
col = self.button.grid(row_num,0)
col = self.label_count.grid(row_num,col)
col = self.label_price.grid(row_num,col)
col = self.label_total.grid(row_num,col)
Label(root,text = '${}'.format(price),relief = "sunken").grid(row = row,column=labels.index('Price'),sticky=E+W)
Label(root,text = data['count'],relief = "sunken").grid(row=row,column=labels.index('Count'),sticky = E+W)
Label(root,text = '${}'.format(row_total),relief = "sunken").grid(row = row,column=labels.index("Total"),sticky=E+W)
def get_status(self):
return self.button.get_status()
total += row_total
single_total += price
row += 1
class MainWindow:
def __init__(self,prices):
self.root = Tk()
self.root.overrideredirect(True)
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(root,text = "Total:").grid(row = row,column=labels.index("Count"))
Label(root,text = "Individual:").grid(row = row,column=labels.index("Product name"),sticky = E)
Label(root,text = "${}".format(total)).grid(row=row,column = labels.index("Total"))
Label(root,text = "${}".format(single_total)).grid(row=row,column=labels.index("Price"))
Button(root,text = "OK", command = root.quit).grid(row=row+2)
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()
root.overrideredirect(True)
root.wm_attributes("-topmost", True)
mainloop()
root.destroy()
def get_status(self):
return dict(
(row.name,row.get_status()) for row in self.rows
)
for key in checks.keys():
checks[key] = checks[key].get()
return checks
if __name__ == "__main__":
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()

55
interface/test_price_new.py

@ -1,55 +0,0 @@
import copy
import sys
from quad_dialog import message,dicter
import datetime
import sqlite3
import pandas
from decimal import Decimal as D
import shutil
from send2trash import send2trash
import gc
def test():
con = sqlite3.connect('test.db')
cur = con.cursor()
date = '2018-08-18'
# print(date)
quad_links = list(row[0] for row in cur.execute('SELECT link FROM links ORDER BY id'))
quad_parts_base = pandas.read_sql('SELECT * FROM names',con).to_dict('records')
current = copy.deepcopy(quad_parts_base)
complete = None
while not complete:
complete = pandas.read_sql('SELECT * FROM parts where date == "{}"'.format(date),con).to_dict('records')
if not complete:
reset()
# print(complete)
for item in current:
name = item['name']
link = item['link']
# print(link)
item['product_name'] = complete[link]['product_name']
item['price'] = round(D(complete[link]['price']),2)
item['time'] = complete[link]['time']
print(current)
bought = message(quad_parts_base,current)
quad_parts_base = dicter(quad_parts_base)
for key,is_purchased in bought.items():
if is_purchased:
# print(key)
try:
if quad_parts_base[key]['count'] > 1:
cur.execute('UPDATE names SET count = count - 1 WHERE name == ?',(key,))
else:
cur.execute('UPDATE names SET purchased = 1 WHERE name == ?',(key,))
except:
print(quad_parts_base)
con.commit()
con.close()
def reset():
try:
send2trash('test.db')
except:
pass
shutil.copy('quadparts.db','test.db')
Loading…
Cancel
Save