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.

93 lines
4.6 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
  1. from tkinter import *
  2. class ButtonWithCheck:
  3. def __init__(self,root,name):
  4. self.name = name
  5. self.root = root
  6. self.checked = BooleanVar()
  7. self.checkbox = Checkbutton(self.root,variable=self.checked,state = DISABLED)
  8. self.button = Button(self.root,text = name,command = self.check)
  9. def check(self):
  10. self.checkbox.toggle()
  11. def get_status(self):
  12. return self.checked.get()
  13. def grid(self,row,start_col):
  14. col = start_col
  15. self.checkbox.grid(row = row,column = col,sticky=E)
  16. col += 1
  17. self.button.grid(row = row,column = col,sticky=E+W)
  18. col += 1
  19. return col
  20. class SunkenText:
  21. def __init__(self,root,text):
  22. self.text = text
  23. self.root = root
  24. self.label = Label(self.root,text = self.text,relief = "sunken")
  25. def grid(self,row,start_col):
  26. col = start_col
  27. self.label.grid(row = row,column = col,sticky=E+W)
  28. col += 1
  29. return col
  30. class Row:
  31. labels = ["","Product name","Price","Count","Total"]
  32. def __init__(self,root,info):
  33. self.root = root
  34. self.name = info['name']
  35. self.price = info['price']
  36. self.count = info['count']
  37. self.total = self.price * self.count
  38. self.button = ButtonWithCheck(self.root,self.name)
  39. self.label_price = SunkenText(self.root,'${}'.format(self.price))
  40. self.label_count = SunkenText(self.root,str(info['count']))
  41. self.label_total = SunkenText(self.root,'${}'.format(self.total))
  42. def grid(self,row_num):
  43. col = self.button.grid(row_num,0)
  44. col = self.label_price.grid(row_num,col)
  45. col = self.label_count.grid(row_num,col)
  46. col = self.label_total.grid(row_num,col)
  47. def get_status(self):
  48. return self.button.get_status()
  49. class MainWindow:
  50. def __init__(self,prices):
  51. self.root = Tk()
  52. self.root.wm_title("Today's Prices")
  53. self.root.protocol("WM_DELETE_WINDOW", self.root.quit)
  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. from decimal import Decimal as D
  80. 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'}]
  81. window = MainWindow(info)
  82. window.start()