gui.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from tkinter import *
  2. from linked_data import Chain, format_dict
  3. import pprint
  4. chain = Chain()
  5. chain.populate()
  6. root = Tk()
  7. root.title('dsp blockchain')
  8. view = Entry(root, width=35, borderwidth=5)
  9. view.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  10. accept_id = Entry(root, width=35, borderwidth=5)
  11. accept_id.grid(row=4, column=1, columnspan=3, padx=10, pady=10)
  12. accept_id.insert(0, 'contract')
  13. accept_status = StringVar(root)
  14. accept_status.set('False') # default value
  15. OptionMenu(root, accept_status, 'False', 'True').grid(row=4, column=4)
  16. def myClick():
  17. chain.current_contract_state(int(view.get()))
  18. pprint.pprint(chain.state)
  19. def add_contract():
  20. # add contract to the blockchain
  21. def add():
  22. chain.new_contract(client.get(), contractor.get(), deadline.get(), description.get(), name.get(), int(price.get()), people.get().split(','), initiator.get())
  23. # destroy add contract window, and print conformation
  24. newWindow.destroy()
  25. print('contract added')
  26. # make new window for adding the information for the contract
  27. newWindow = Toplevel(root)
  28. newWindow.title('Add contract')
  29. # create all text boxes
  30. Label(newWindow, text='The client: ').grid(row=0, column=0)
  31. client = Entry(newWindow, width=35, borderwidth=5)
  32. client.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  33. Label(newWindow, text='The contractor: ').grid(row=1, column=0)
  34. contractor = Entry(newWindow, width=35, borderwidth=5)
  35. contractor.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  36. Label(newWindow, text='Deadline: ').grid(row=2, column=0)
  37. deadline = Entry(newWindow, width=35, borderwidth=5)
  38. deadline.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  39. Label(newWindow, text='Description: ').grid(row=3, column=0)
  40. description = Entry(newWindow, width=35, borderwidth=5)
  41. description.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  42. Label(newWindow, text='Project name: ').grid(row=4, column=0)
  43. name = Entry(newWindow, width=35, borderwidth=5)
  44. name.grid(row=4, column=1, columnspan=3, padx=10, pady=10)
  45. Label(newWindow, text='Price: ').grid(row=5, column=0)
  46. price = Entry(newWindow, width=35, borderwidth=5)
  47. price.grid(row=5, column=1, columnspan=3, padx=10, pady=10)
  48. Label(newWindow, text='People involved: ').grid(row=6, column=0)
  49. people = Entry(newWindow, width=35, borderwidth=5)
  50. people.grid(row=6, column=1, columnspan=3, padx=10, pady=10)
  51. Label(newWindow, text='Initiator').grid(row=7, column=0)
  52. initiator = Entry(newWindow, width=35, borderwidth=5)
  53. initiator.grid(row=7, column=1, columnspan=3, padx=10, pady=10)
  54. # create the add contract button
  55. add_c = Button(newWindow, text = "Add contract", command=add)
  56. add_c.grid(row=8, column=0)
  57. def update():
  58. def up():
  59. chain.update_contract(int(iden.get()), term.get(), update_value.get(), name.get())
  60. newWindow.destroy()
  61. print('update pending approval')
  62. newWindow = Toplevel(root)
  63. newWindow.title('Update terms')
  64. Label(newWindow, text='Contract id: ').grid(row=0, column=0)
  65. iden = Entry(newWindow, width=35, borderwidth=5)
  66. iden.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  67. Label(newWindow, text='Term: ').grid(row=1, column=0)
  68. term = Entry(newWindow, width=35, borderwidth=5)
  69. term.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  70. Label(newWindow, text='New value: ').grid(row=2, column=0)
  71. update_value = Entry(newWindow, width=35, borderwidth=5)
  72. update_value.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  73. Label(newWindow, text='Name: ').grid(row=3, column=0)
  74. name = Entry(newWindow, width=35, borderwidth=5)
  75. name.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  76. Button(newWindow, text='Update', command=up).grid(row=4, column=0)
  77. def accept_update():
  78. def accept_up():
  79. c = comment.get()
  80. ans = awnser.get()
  81. if ans == 'False':
  82. ans = False
  83. else: ans = True
  84. if c == '':
  85. chain.accept_update(int(iden.get()), int(update_id.get()), ans, name.get())
  86. else:
  87. chain.accept_update(int(iden.get()), int(update_id.get()), ans, name.get(), c)
  88. newWindow.destroy()
  89. newWindow = Toplevel(root)
  90. newWindow.title('Update terms')
  91. Label(newWindow, text='Contract id: ').grid(row=0, column=0)
  92. iden = Entry(newWindow, width=35, borderwidth=5)
  93. iden.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
  94. Label(newWindow, text='Update id: ').grid(row=1, column=0)
  95. update_id = Entry(newWindow, width=35, borderwidth=5)
  96. update_id.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
  97. Label(newWindow, text='Awnser ').grid(row=2, column=0)
  98. awnser = Entry(newWindow, width=35, borderwidth=5)
  99. awnser.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
  100. Label(newWindow, text='Name: ').grid(row=3, column=0)
  101. name = Entry(newWindow, width=35, borderwidth=5)
  102. name.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  103. Label(newWindow, text='Comment optional: ').grid(row=3, column=0)
  104. comment = Entry(newWindow, width=35, borderwidth=5)
  105. comment.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
  106. Button(newWindow, text='Update', command=accept_up).grid(row=4, column=0)
  107. def view_updates():
  108. status =accept_status.get()
  109. if status == 'False':
  110. status = False
  111. else: status = True
  112. iden = int(accept_id.get())
  113. if status == None:
  114. pprint.pprint(chain.retrieve_updates(iden))
  115. else:
  116. pprint.pprint(chain.retrieve_updates(iden, status))
  117. Button(root, text="View contract", command=myClick).grid(row=0, column=0)
  118. Button(root, text="New contract", command=add_contract).grid(row=1, column=0)
  119. Button(root, text="Update contract", command=update).grid(row=2, column=0)
  120. Button(root, text="Accept updates", command=accept_update).grid(row=3, column=0)
  121. Button(root, text="View pending updates", command=view_updates).grid(row=4, column=0)
  122. root.mainloop()