Advertisement
Vasilena

gui.py

Apr 20th, 2023 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.99 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import *
  3. from tkinter import ttk
  4. from tkinter import messagebox
  5. import tkinter.messagebox
  6. from pump_two_on import PumpTwoOn
  7. from pump_two_off import PumpTwoOff
  8. from pump_one_on import PumpOneOn
  9. from pump_one_off import PumpOneOff
  10. from drain import TankDrain
  11. from exit import Exit
  12. import RPi.GPIO as GPIO
  13. import math
  14.  
  15. GPIO.setwarnings(False)
  16. GPIO.setmode(GPIO.BOARD)
  17.  
  18. GPIO.setup(36, GPIO.IN) # pump one
  19. GPIO.setup(40, GPIO.IN) # pumo two
  20. GPIO.setup(38, GPIO.IN) # pumo three
  21.  
  22. # tank one gpio senzors setup
  23. GPIO.setup(18, GPIO.IN)
  24. GPIO.setup(22, GPIO.IN)
  25. GPIO.setup(21, GPIO.IN)
  26. GPIO.setup(23, GPIO.IN)
  27. GPIO.setup(24, GPIO.IN)
  28.  
  29. # tank two gpio sen+zors setup
  30. GPIO.setup(11, GPIO.IN)
  31. GPIO.setup(12, GPIO.IN)
  32. GPIO.setup(13, GPIO.IN)
  33. GPIO.setup(15, GPIO.IN)
  34. GPIO.setup(16, GPIO.IN)
  35.  
  36. # tank three gpio senzors setup
  37. GPIO.setup(29, GPIO.IN)
  38. GPIO.setup(31, GPIO.IN)
  39. GPIO.setup(33, GPIO.IN)
  40. GPIO.setup(35, GPIO.IN)
  41. GPIO.setup(37, GPIO.IN)
  42.  
  43.  
  44. class Window:
  45.  
  46. def __init__(self, root):
  47. # --------------------------------TK-------------------------------
  48. # root setup
  49. self.window = root
  50. self.background_color = '#999da0'
  51. self.window.attributes('-fullscreen', True)
  52. self.window.title("Control Panel")
  53. self.window.configure(background='#999da0')
  54.  
  55. self.animating_one = False
  56. self.animating_two = False
  57. self.animating_three = False
  58.  
  59. # root geometry
  60. self.screen_width = self.window.winfo_screenwidth() # 1920
  61. self.screen_height = self.window.winfo_screenheight() # 1080
  62.  
  63. # manual mode
  64. self.manual = Label(root, text="РЪЧНО УПРАВЛЕНИЕ", font=('Arial', 30), bg=self.background_color, fg="black")
  65. self.manual.place(x=(self.screen_width / 3) * 2.15, y=self.screen_height / 20)
  66.  
  67. self.pumpOne = Label(root, text="ПОМПА 1", font=('Arial', 20), bg=self.background_color, fg="black")
  68. self.pumpOne.place(x=(self.screen_width / 3) * 2.17, y=self.screen_height / 8)
  69.  
  70. self.pumpTwo = Label(root, text="ПОМПА 2", font=('Arial', 20), bg=self.background_color, fg="black")
  71. self.pumpTwo.place(x=(self.screen_width / 3) * 2.6, y=self.screen_height / 8)
  72.  
  73. # buttons
  74. self.pump_one_on_btn = Button(root, text="ВКЛЮЧИ", font=("Arial", 15, "bold"), command=self.pump_one_on_func, bg="#198FF5", fg="white")
  75. self.pump_one_on_btn.place(x=(self.screen_width / 3) * 2.18, y=self.screen_height / 5.8)
  76.  
  77. self.pump_one_off_btn = Button(root, text="ИЗКЛЮЧИ", font=("Arial", 15, "bold"), command=self.pump_one_off_func, bg="#696969", fg="white")
  78. self.pump_one_off_btn.place(x=(self.screen_width / 3) * 2.16, y=self.screen_height / 4.5)
  79.  
  80. # separator
  81. self.separator_frame = Frame(root, bg="black", width=2, height=150)
  82. self.separator_frame.place(x=1590, y=137)
  83.  
  84. self.pump_two_on_btn = Button(root, text="ВКЛЮЧИ", font=("Arial", 15, "bold"), command=self.pump_two_on_func, bg="#198FF5", fg="white")
  85. self.pump_two_on_btn.place(x=(self.screen_width / 3) * 2.6, y=self.screen_height / 5.8)
  86.  
  87. self.pump_two_off_btn = Button(root, text="ИЗКЛЮЧИ", font=("Arial", 15, "bold"), command=self.pump_two_off_func, bg="#696969", fg="white")
  88. self.pump_two_off_btn.place(x=(self.screen_width / 3) * 2.6, y=self.screen_height / 4.5)
  89.  
  90. # draning
  91. self.manual = Label(root, text="ГЛАВЕН РЕЗЕРВОАР", font=('Arial', 30), bg=self.background_color, fg="black")
  92. self.manual.place(x=(self.screen_width / 3) * 2.15, y=self.screen_height / 3.3)
  93.  
  94. self.pump_two_off_btn = Button(root, text="ИЗТОЧВАНЕ", font=("Arial", 15, "bold"), command=self.drain_func, bg="#F06B5D", fg="white")
  95. self.pump_two_off_btn.place(x=(self.screen_width / 3) * 2.35, y=self.screen_height / 2.7)
  96.  
  97. # separator
  98. self.separator_frame = Frame(root, bg="black", width=470, height=2)
  99. self.separator_frame.place(x=1360, y=480)
  100.  
  101. # exit program button
  102. self.exit_btn = Button(root, text="X", height=1, width=2, font=("Arial", 15, "bold"),
  103. command=self.Exit_btn_func, bg="#EF4324", fg="white")
  104. self.exit_btn.place(x=self.screen_width - 55, y=self.screen_height - (self.screen_height - 5))
  105.  
  106. # automatic mode
  107. self.automatic = Label(root, text="АВТОМАТИЧНО УПРАВЛЕНИЕ", font=('Arial', 25), bg=self.background_color,
  108. fg="black")
  109. self.automatic.place(x=(self.screen_width / 3) * 2.1, y=self.screen_height / 2.1)
  110.  
  111. self.tankOne = Label(root, text="РЕЗЕРВОАР 1", font=('Arial', 20), bg=self.background_color, fg="black")
  112. self.tankOne.place(x=(self.screen_width / 3) * 2.13, y=self.screen_height / 1.9)
  113.  
  114. # separator
  115. self.separator_frame = Frame(root, bg="black", width=2, height=120)
  116. self.separator_frame.place(x=1590, y=570)
  117.  
  118. self.tankTwo = Label(root, text="РЕЗЕРВОАР 2", font=('Arial', 20), bg=self.background_color, fg="black")
  119. self.tankTwo.place(x=(self.screen_width / 3) * 2.55, y=self.screen_height / 1.9)
  120.  
  121. # dropdown menus
  122. self.tank1_label = tk.Label(root, text="избери:", bg=self.background_color, font=('Arial', 18))
  123. self.tank1_label.place(x=(self.screen_width / 3) * 2.17, y=self.screen_height / 1.7)
  124. self.tank1_value_inside = tk.StringVar(value="0%")
  125. self.tank1_options = ["0%", "20%", "40%", "60%", "80%", "100%"]
  126. self.tank1_dropdown = tk.OptionMenu(root, self.tank1_value_inside, *self.tank1_options)
  127. self.tank1_dropdown.place(x=(self.screen_width / 3) * 2.32, y=self.screen_height / 1.7)
  128.  
  129. # dropdown menus
  130. self.tank2_label = tk.Label(root, text="избери:", bg=self.background_color, font=('Arial', 18))
  131. self.tank2_label.place(x=(self.screen_width / 3) * 2.55, y=self.screen_height / 1.7)
  132. self.tank2_value_inside = tk.StringVar(value="0%")
  133. self.tank2_options = ["0%", "20%", "40%", "60%", "80%", "100%"]
  134. self.tank2_dropdown = tk.OptionMenu(root, self.tank2_value_inside, *self.tank2_options)
  135. self.tank2_dropdown.place(x=(self.screen_width / 3) * 2.70, y=self.screen_height / 1.7)
  136.  
  137. # sequence option
  138. self.automatic = Label(root, text="ИЗБЕРИ ПОСЛЕДОВАТЕЛНОСТ", font=('Arial', 25), bg=self.background_color,
  139. fg="black")
  140. self.automatic.place(x=(self.screen_width / 3) * 2.1, y=self.screen_height / 1.5)
  141.  
  142. # create a label and two radio buttons for the first sequence option
  143. self.sequence1_label = tk.Label(root, text='ЕДНО', bg=self.background_color, font=('Arial', 20))
  144. self.sequence1_label.place(x=(self.screen_width / 3) * 2.3, y=self.screen_height / 1.4)
  145.  
  146. self.sequence1_var = tk.StringVar(root)
  147. self.sequence1_var.set('Резервоар 1')
  148.  
  149. self.sequence1_rb1 = tk.Radiobutton(root, text='Резервоар 1 ', bg=self.background_color, font=('Arial', 15),
  150. variable=self.sequence1_var, value='Резервоар 1')
  151. self.sequence1_rb1.place(x=(self.screen_width / 3) * 2.15, y=self.screen_height / 1.31)
  152. self.sequence1_rb2 = tk.Radiobutton(root, text='Резервоар 2 ', bg=self.background_color, font=('Arial', 15),
  153. variable=self.sequence1_var, value='Резервоар 2')
  154. self.sequence1_rb2.place(x=(self.screen_width / 3) * 2.15, y=self.screen_height / 1.24)
  155.  
  156. # separator
  157. self.separator_frame = Frame(root, bg="black", width=2, height=122)
  158. self.separator_frame.place(x=1590, y=780)
  159.  
  160. # create a label and two radio buttons for the second sequence option
  161. self.sequence2_label = tk.Label(root, text='ДВЕ', font=('Arial', 20), bg=self.background_color, fg="black")
  162. self.sequence2_label.place(x=(self.screen_width / 3) * 2.55, y=self.screen_height / 1.4)
  163.  
  164. self.sequence2_var = tk.StringVar(root)
  165. self.sequence2_var.set('Резервоар 2')
  166.  
  167. self.sequence2_rb1 = tk.Radiobutton(root, text='Резервоар 1 ', bg=self.background_color, font=('Arial', 15),
  168. variable=self.sequence2_var, value='Резервоар 1')
  169. self.sequence2_rb1.place(x=(self.screen_width / 3) * 2.57, y=self.screen_height / 1.31)
  170. self.sequence2_rb2 = tk.Radiobutton(root, text='Резервоар 2 ', bg=self.background_color, font=('Arial', 15),
  171. variable=self.sequence2_var, value='Резервоар 2')
  172. self.sequence2_rb2.place(x=(self.screen_width / 3) * 2.57, y=self.screen_height / 1.24)
  173.  
  174. # start and stop button
  175.  
  176. self.start = Button(root, text="СТАРТИРАЙ", font=('Arial', 25), bg="#41be87", fg="white",
  177. command=self.overflow_btn_on)
  178. self.start.place(x=(self.screen_width / 3) * 2.17, y=self.screen_height / 1.15)
  179.  
  180. self.stop = Button(root, text="СПРИ", font=('Arial', 25), bg="#e2651d", fg="white",
  181. command=self.overflow_btn_off)
  182. self.stop.place(x=(self.screen_width / 3) * 2.6, y=self.screen_height / 1.15)
  183.  
  184. # --------------------------------Canvas-------------------------------
  185. # canvas setup
  186. self.canvas = Canvas(root, width=(self.screen_width / 3) * 2, height=self.screen_height,
  187. bg=self.background_color)
  188. self.canvas.pack()
  189. self.canvas.place(bordermode=OUTSIDE)
  190.  
  191. # heading
  192. self.canvas.create_text(self.screen_width / 3, self.screen_height / 18, text='К О Н Т Р О Л Е Н П А Н Е Л',
  193. fill='black', font='Arial 25')
  194.  
  195. # create scale tank One
  196. self.canvas.create_line(65, 140, 65, 520, width=2)
  197. for i in range(6):
  198. self.p1 = 100 - i * 20
  199. self.y1 = 140
  200. self.y1 = self.y1 + i * 76
  201. self.canvas.create_text(25, self.y1, text=self.p1, font=("Arial", 13, "bold"))
  202. self.canvas.create_text(45, self.y1, text="%", font=("Arial", 13, "bold"))
  203. self.canvas.create_line(65, self.y1, 75, self.y1, width=2)
  204.  
  205. # create scale tank Two
  206. self.canvas.create_line(65, 600, 65, 980, width=2)
  207. for i in range(6):
  208. self.p = 100 - i * 20
  209. self.y = 600
  210. self.y = self.y + i * 76
  211. self.canvas.create_text(25, self.y, text=self.p, font=("Arial", 13, "bold"))
  212. self.canvas.create_text(45, self.y, text=" %", font=("Arial", 13, "bold"))
  213. self.canvas.create_line(65, self.y, 75, self.y, width=2)
  214.  
  215. # create scale tank Three
  216. self.canvas.create_line(1185, 200, 1185, 900, width=3)
  217. for i in range(6):
  218. self.p = 100 - i * 20
  219. self.y2 = 200
  220. self.y2 = self.y2 + i * 140
  221. self.canvas.create_text(1205, self.y2, text=self.p, font=("Arial", 15, "bold"))
  222. self.canvas.create_text(1230, self.y2, text=" %", font=("Arial", 15, "bold"))
  223. self.canvas.create_line(1160, self.y2, 1186, self.y2, width=3)
  224.  
  225. # tank One (T1)
  226. self.canvas.create_rectangle(80, 140, 360, 520, fill='#d9dddc')
  227. # tank One Water
  228. self.waterOne = self.canvas.create_rectangle(80, 140, 360, 520, fill='blue')
  229.  
  230. self.tank1_label_canvas = tk.Label(root, text="Резервоар 1", bg=self.background_color, font=('Arial', 18))
  231. self.tank1_label_canvas.place(x=80, y=100)
  232.  
  233. # tank Two (T2)
  234. self.canvas.create_rectangle(80, 600, 360, 980, fill='#d9dddc')
  235. # tank Two Water
  236. self.waterTwo = self.canvas.create_rectangle(80, 600, 360, 980, fill='blue')
  237.  
  238. self.tank2_label_canvas = tk.Label(root, text="Резервоар 2", bg=self.background_color, font=('Arial', 18))
  239. self.tank2_label_canvas.place(x=80, y=560)
  240.  
  241. # tank Three (T3)
  242. self.canvas.create_rectangle(750, 200, 1150, 900, fill='#d9dddc')
  243. # tank Three Water
  244. self.waterThree = self.canvas.create_rectangle(750, 200, 1150, 900, fill='blue')
  245.  
  246. self.tank3_label_canvas = tk.Label(root, text="Резервоар 3", bg=self.background_color, font=('Arial', 20))
  247. self.tank3_label_canvas.place(x=995, y=155)
  248.  
  249. # pump One (p40)
  250. self.ovalOne = self.canvas.create_oval(385, 380, 515, 510, fill='#6b6b6b', width=15)
  251. self.Pump1line1 = self.canvas.create_line(400, 450, 500, 450, width=3)
  252. self.Pump1line2 = self.canvas.create_line(450, 400, 450, 500, width=3)
  253.  
  254. self.pump1_label_canvas = tk.Label(root, text="Помпа 1", bg=self.background_color, font=('Arial', 18))
  255. self.pump1_label_canvas.place(x=405, y=340)
  256.  
  257. # pump Two (p40)
  258. self.ovalTwo = self.canvas.create_oval(385, 835, 515, 965, fill='#6b6b6b', width=15)
  259. self.Pump2line1 = self.canvas.create_line(400, 900, 500, 900, width=3)
  260. self.Pump2line2 = self.canvas.create_line(450, 850, 450, 950, width=3)
  261.  
  262. self.pump2_label_canvas = tk.Label(root, text="Помпа 2", bg=self.background_color, font=('Arial', 18))
  263. self.pump2_label_canvas.place(x=405, y=790)
  264.  
  265. # pump Three (p38)
  266. self.ovalTwo = self.canvas.create_oval(985, 910, 1115, 1040, fill='#6b6b6b', width=15)
  267. self.Pump2line1 = self.canvas.create_line(1000, 980, 1100, 980, width=3)
  268. self.Pump2line2 = self.canvas.create_line(1050, 930, 1050, 1030, width=3)
  269.  
  270. self.pump3_label_canvas = tk.Label(root, text="Помпа 3", bg=self.background_color, font=('Arial', 15), wraplength=1)
  271. self.pump3_label_canvas.place(x=1130, y=903)
  272.  
  273. def Exit_btn_func(self):
  274. self.Exit_btn = Exit().exit()
  275. if self.Exit_btn > 0:
  276. self.window.destroy()
  277. return
  278.  
  279. def check_condition_water_tank_one(self):
  280. if self.sum == 1:
  281. return 444
  282. elif self.sum == 2:
  283. return 368
  284. elif self.sum == 3:
  285. return 292
  286. elif self.sum == 4:
  287. return 216
  288. elif self.sum == 5:
  289. return 140
  290. else:
  291. return 520
  292.  
  293.  
  294. def check_condition_water_tank_two(self):
  295. if self.sum == 1:
  296. return 904
  297. elif self.sum == 2:
  298. return 828
  299. elif self.sum == 3:
  300. return 752
  301. elif self.sum == 4:
  302. return 676
  303. elif self.sum == 5:
  304. return 600
  305. else:
  306. return 980
  307.  
  308. def check_condition_water_tank_three(self):
  309. if self.sum == 1:
  310. return 760
  311. elif self.sum == 2:
  312. return 620
  313. elif self.sum == 3:
  314. return 480
  315. elif self.sum == 4:
  316. return 340
  317. elif self.sum == 5:
  318. return 200
  319. else:
  320. return 900
  321.  
  322. def update_water_tank_one(self):
  323. self.sum = GPIO.input(18) + GPIO.input(22) + GPIO.input(21) + GPIO.input(23) + GPIO.input(24)
  324. self.y1 = self.check_condition_water_tank_one()
  325. self.canvas.coords(self.waterOne, 80, self.y1, 360, 520)
  326. self.window.after(300, self.update_water_tank_one)
  327.  
  328. def update_water_tank_two(self):
  329. self.sum = GPIO.input(15) + GPIO.input(13) + GPIO.input(12) + GPIO.input(16) + GPIO.input(11)
  330. self.y1 = self.check_condition_water_tank_two()
  331. self.canvas.coords(self.waterTwo, 80, self.y1, 360, 980)
  332. self.window.after(300, self.update_water_tank_two)
  333.  
  334. def update_water_tank_three(self):
  335. self.sum = GPIO.input(29) + GPIO.input(31) + GPIO.input(33) + GPIO.input(35) + GPIO.input(37)
  336. self.y1 = self.check_condition_water_tank_three()
  337. self.canvas.coords(self.waterThree, 750, self.y1, 1150, 900)
  338. self.window.after(300, self.update_water_tank_three)
  339.  
  340. def animate_one(self, angle):
  341. x1, y1, x2, y2 = self.canvas.coords(self.Pump1line1)
  342. cx, cy = (x1 + x2) / 2, (y1 + y2) / 2
  343. new_x1 = cx + (x1 - cx) * math.cos(angle) - (y1 - cy) * math.sin(angle)
  344. new_y1 = cy + (x1 - cx) * math.sin(angle) + (y1 - cy) * math.cos(angle)
  345. new_x2 = cx + (x2 - cx) * math.cos(angle) - (y2 - cy) * math.sin(angle)
  346. new_y2 = cy + (x2 - cx) * math.sin(angle) + (y2 - cy) * math.cos(angle)
  347. self.canvas.coords(self.Pump1line1, new_x1, new_y1, new_x2, new_y2)
  348.  
  349. x1, y1, x2, y2 = self.canvas.coords(self.Pump1line2)
  350. cx, cy = (x1 + x2) / 2, (y1 + y2) / 2
  351. new_x1 = cx + (x1 - cx) * math.cos(angle) - (y1 - cy) * math.sin(angle)
  352. new_y1 = cy + (x1 - cx) * math.sin(angle) + (y1 - cy) * math.cos(angle)
  353. new_x2 = cx + (x2 - cx) * math.cos(angle) - (y2 - cy) * math.sin(angle)
  354. new_y2 = cy + (x2 - cx) * math.sin(angle) + (y2 - cy) * math.cos(angle)
  355. self.canvas.coords(self.Pump1line2, new_x1, new_y1, new_x2, new_y2)
  356.  
  357. if self.animating_one:
  358. self.canvas.after(10, self.animate_one, angle + 0.1)
  359.  
  360. def animate_two(self, angle):
  361. x1, y1, x2, y2 = self.canvas.coords(self.Pump2line1)
  362. cx, cy = (x1 + x2) / 2, (y1 + y2) / 2
  363. new_x1 = cx + (x1 - cx) * math.cos(angle) - (y1 - cy) * math.sin(angle)
  364. new_y1 = cy + (x1 - cx) * math.sin(angle) + (y1 - cy) * math.cos(angle)
  365. new_x2 = cx + (x2 - cx) * math.cos(angle) - (y2 - cy) * math.sin(angle)
  366. new_y2 = cy + (x2 - cx) * math.sin(angle) + (y2 - cy) * math.cos(angle)
  367. self.canvas.coords(self.Pump2line1, new_x1, new_y1, new_x2, new_y2)
  368.  
  369. x1, y1, x2, y2 = self.canvas.coords(self.Pump2line2)
  370. cx, cy = (x1 + x2) / 2, (y1 + y2) / 2
  371. new_x1 = cx + (x1 - cx) * math.cos(angle) - (y1 - cy) * math.sin(angle)
  372. new_y1 = cy + (x1 - cx) * math.sin(angle) + (y1 - cy) * math.cos(angle)
  373. new_x2 = cx + (x2 - cx) * math.cos(angle) - (y2 - cy) * math.sin(angle)
  374. new_y2 = cy + (x2 - cx) * math.sin(angle) + (y2 - cy) * math.cos(angle)
  375. self.canvas.coords(self.Pump2line2, new_x1, new_y1, new_x2, new_y2)
  376.  
  377. if self.animating_two:
  378. self.canvas.after(10, self.animate_two, angle + 0.1)
  379.  
  380. def pump_one_on_func(self):
  381. PumpOneOn()
  382. if not self.animating_one:
  383. self.animating_one = True
  384. self.animate_one(0)
  385.  
  386. def pump_one_off_func(self):
  387. PumpOneOff()
  388. self.animating_one = False
  389.  
  390. def pump_two_on_func(self):
  391. PumpTwoOn()
  392. if not self.animating_two:
  393. self.animating_two = True
  394. self.animate_two(0)
  395.  
  396. def pump_two_off_func(self):
  397. PumpTwoOff()
  398. self.animating_two = False
  399.  
  400. def drain_func(self):
  401. TankDrain()
  402. if not self.animating_three:
  403. self.animating_three = True
  404. self.animate_three(0)
  405.  
  406. def is_empty(self):
  407. if (GPIO.input(18) + GPIO.input(22) + GPIO.input(21) + GPIO.input(23) + GPIO.input(24) + GPIO.input(15) + GPIO.input(13) + GPIO.input(12) + GPIO.input(16) + GPIO.input(11)) == 0:
  408. self.animating_three = False
  409. obj.is_empty()
  410.  
  411. def overflow_btn_on(self):
  412. if int(self.tank2_value_inside.get()[:-1]) + int(self.tank1_value_inside.get()[:-1]) != 100 \
  413. or self.sequence2_var.get() == self.sequence1_var.get():
  414. messagebox.showwarning("Предупреждение", "Въведените данни не са равни на 100%!")
  415. else:
  416. print("OK!")
  417.  
  418. def overflow_btn_off(self):
  419. ...
  420.  
  421.  
  422. if __name__ == "__main__":
  423. root = tk.Tk()
  424. obj = Window(root)
  425. obj.update_water_tank_one()
  426. obj.update_water_tank_two()
  427. obj.update_water_tank_three()
  428. root.mainloop()
  429.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement