You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
4.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. from tkinter import *
  2. from decimal import Decimal as D
  3. class ButtonWithCheck:
  4. def __init__(self,root,name):
  5. self.name = name
  6. self.root = root
  7. self.checked = BooleanVar()
  8. self.checkbox = Checkbutton(self.root,variable=self.checked,state = DISABLED)
  9. self.button = Button(self.root,text = name,command = self.check)
  10. def check(self):
  11. self.checkbox.toggle()
  12. def get_status(self):
  13. return self.checked.get()
  14. def grid(self,row,start_col):
  15. col = start_col
  16. self.checkbox.grid(row = row,column = col,sticky=E)
  17. col += 1
  18. self.button.grid(row = row,column = col,sticky=E+W)
  19. col += 1
  20. return col
  21. class SunkenText:
  22. def __init__(self,root,text):
  23. self.text = text
  24. self.root = root
  25. self.label = Label(self.root,text = self.text,relief = "sunken")
  26. def grid(self,row,start_col):
  27. col = start_col
  28. self.label.grid(row = row,column = col,sticky=E+W)
  29. col += 1
  30. return col
  31. class Row:
  32. labels = ["","Product name","Price","Count","Total"]
  33. def __init__(self,root,info):
  34. self.root = root
  35. self.name = info['name']
  36. self.price = D(info['price'])
  37. self.count = info['count']
  38. self.total = self.price * self.count
  39. self.button = ButtonWithCheck(self.root,self.name)
  40. self.label_price = SunkenText(self.root,'${}'.format(self.price))
  41. self.label_count = SunkenText(self.root,str(info['count']))
  42. self.label_total = SunkenText(self.root,'${}'.format(self.total))
  43. def grid(self,row_num):
  44. col = self.button.grid(row_num,0)
  45. col = self.label_count.grid(row_num,col)
  46. col = self.label_price.grid(row_num,col)
  47. col = self.label_total.grid(row_num,col)
  48. def get_status(self):
  49. return self.button.get_status()
  50. class MainWindow:
  51. def __init__(self,prices):
  52. self.root = Tk()
  53. self.root.overrideredirect(True)
  54. self.root.wm_attributes("-topmost", True)
  55. self.rows = sorted(map(lambda row: Row(self.root,row),prices),key = lambda r: r.name)
  56. offset = 0
  57. for i,label in enumerate(Row.labels):
  58. Label(self.root,text = label).grid(row = offset,column = i)
  59. offset += 1
  60. for row in self.rows:
  61. row.grid(offset)
  62. offset += 1
  63. total = sum(row.total for row in self.rows)
  64. individual = sum(row.price for row in self.rows)
  65. Label(self.root,text = "Total:").grid(row = offset,column=Row.labels.index("Total")-1)
  66. Label(self.root,text = "${}".format(total)).grid(row = offset,column = Row.labels.index("Total"))
  67. Label(self.root,text = "Individual:").grid(row = offset,column = Row.labels.index("Price")-1,sticky = E)
  68. Label(self.root,text = "${}".format(individual)).grid(row = offset,column = Row.labels.index("Price"))
  69. offset += 1
  70. Button(self.root,text = "OK", command = self.root.quit).grid(row=offset)
  71. def start(self):
  72. mainloop()
  73. self.root.withdraw()
  74. def get_status(self):
  75. return dict(
  76. (row.name,row.get_status()) for row in self.rows
  77. )
  78. if __name__ == "__main__":
  79. 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'}]
  80. window = MainWindow(info)
  81. window.start()