main.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from Tkinter import *
  2. JUMP = 40
  3. MAX_WIDTH = 20 * JUMP
  4. MAX_HEIGHT = 20 * JUMP
  5. root = Tk()
  6. canvas = Canvas(root, width=MAX_WIDTH, height=MAX_HEIGHT, bg="white")
  7. canvas.pack()
  8. name = 0
  9. def lower(value):
  10. return value / JUMP * JUMP
  11. def upper(value):
  12. return (value / JUMP + 1) * JUMP
  13. def avg(a, b):
  14. return float(a + b) / 2
  15. class InterfaceCore():
  16. mode = ""
  17. drawn = set()
  18. refs = dict()
  19. def set_mode(self, mode):
  20. self.mode = mode
  21. def clicked(self, event):
  22. if self.mode == "":
  23. print("Please select a block first")
  24. else:
  25. x = event.x
  26. y = event.y
  27. r = canvas.create_rectangle(lower(x), lower(y), upper(x), upper(y), fill="white")
  28. t = canvas.create_text(avg(lower(x), upper(x)), avg(lower(y), upper(y)), text=self.mode, fill="black")
  29. global name
  30. b = (lower(x), lower(y), upper(x), upper(y), str(name))
  31. self.drawn.add(b)
  32. self.refs[str(name)] = [r, t]
  33. name += 1
  34. def find(self, location):
  35. x, y = location
  36. for e in self.drawn:
  37. if (e[0] <= x and
  38. e[1] <= y and
  39. e[2] >= x and
  40. e[3] >= y):
  41. return e[4]
  42. print("Found nothing at that location!")
  43. return []
  44. def draw(self, start, end):
  45. source = self.find(start)
  46. target = self.find(end)
  47. print("Connect from %s to %s" % (source, target))
  48. if source and target:
  49. canvas.create_line(start[0], start[1], end[0], end[1], fill="black", arrow=LAST)
  50. core = InterfaceCore()
  51. def addition():
  52. core.set_mode("+")
  53. def negation():
  54. core.set_mode("-")
  55. def clicked(event):
  56. core.clicked(event)
  57. def draw(event):
  58. global start_location
  59. start_location = (event.x, event.y)
  60. def release(event):
  61. core.draw(start_location, (event.x, event.y))
  62. Button(root, text="+", command=addition).pack()
  63. Button(root, text="-", command=negation).pack()
  64. core.canvas = canvas
  65. for i in range(JUMP, MAX_HEIGHT, JUMP):
  66. canvas.create_line(0, i, MAX_HEIGHT, i, fill="grey")
  67. for i in range(JUMP, MAX_WIDTH, JUMP):
  68. canvas.create_line(i, 0, i, MAX_WIDTH, fill="grey")
  69. canvas.bind("<Button-1>", clicked)
  70. canvas.bind("<Button-3>", draw)
  71. canvas.bind("<ButtonRelease-3>", release)
  72. root.mainloop()