Guest User

Untitled

a guest
Mar 6th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.31 KB | None | 0 0
  1. import sys
  2. import RPi.GPIO as GPIO
  3. from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QAction, QLabel
  4. from PyQt5.QtGui import QFontDatabase, QPalette, QColor, QFont, QPixmap
  5. from PyQt5.QtCore import Qt, QTimer
  6. from gpiozero import RotaryEncoder
  7. import pygame
  8.  
  9.  
  10. class MainWindow(QMainWindow):
  11. def __init__(self):
  12. super().__init__()
  13. self.initUI()
  14. self.initGPIO()
  15.  
  16. def initUI(self):
  17. # Set the window to fullscreen
  18. self.showFullScreen()
  19.  
  20. # Set the background color to black
  21. palette = self.palette()
  22. palette.setColor(QPalette.Window, QColor("black"))
  23. self.setPalette(palette)
  24.  
  25. #SUBTOOLBAR STAT
  26. # Create a toolbar named 'toolbar 0'
  27. self.toolbar0 = QToolBar('toolbar 0', self)
  28.  
  29. self.toolbar0.setFloatable(False) # Prevent docking
  30. self.toolbar0.setMovable(False) # Prevent user from moving it
  31. self.toolbar0.setParent(self) # Detach from the QMainWindow toolbar system
  32.  
  33. # Make the toolbar transparent by setting its attribute
  34. self.toolbar0.setAttribute(Qt.WA_OpaquePaintEvent, False)
  35.  
  36. # Set the background of the toolbar to transparent
  37. self.toolbar0.setStyleSheet(
  38. """
  39. QToolBar{
  40. background-color: transparent;
  41. border: 0px;
  42. opacity: 0;
  43. }
  44. QToolButton{
  45. margin: 0px;
  46. background-color: transparent;
  47. opacity: 0;
  48. color: rgb(0, 255, 0);
  49. font-family: 'monofonto';
  50. font-size: 30px;
  51. margin-left: -15px;
  52.  
  53. }
  54. """
  55. )
  56.  
  57.  
  58. # Create actions
  59. self.actions = [
  60. QAction("Option1", self),
  61. QAction("Option2", self),
  62. QAction("Option3", self)
  63. ]
  64.  
  65. # Add actions to toolbar
  66. for action in self.actions:
  67. self.toolbar0.addAction(action)
  68.  
  69. # Track selected action index
  70. self.current_index = 0
  71. self.update_selection()
  72.  
  73. # Setup rotary encoder on GPIO pins 19 and 20
  74. self.encoder = RotaryEncoder(19, 20)
  75. self.encoder.when_rotated_clockwise = self.next_option
  76. self.encoder.when_rotated_counter_clockwise = self.prev_option
  77.  
  78. def update_selection(self):
  79. # Update toolbar selection by highlighting the current action
  80. for i, action in enumerate(self.actions):
  81. action.setEnabled(i == self.current_index)
  82.  
  83. def next_option(self):
  84. # Rotate forward
  85. self.current_index = (self.current_index + 1) % len(self.actions)
  86. self.update_selection()
  87. print(f"Selected: {self.actions[self.current_index].text()}")
  88.  
  89. def prev_option(self):
  90. # Rotate backward
  91. self.current_index = (self.current_index - 1) % len(self.actions)
  92. self.update_selection()
  93. print(f"Selected: {self.actions[self.current_index].text()}")
  94.  
  95. self.toolbar0.setGeometry(95, 20, 790, 100)
  96.  
  97.  
  98. #SUBTOOLBAR INV
  99. # Cree a toolbar named 'toolbar 1'
  100. self.toolbar1 = QToolBar('toolbar 1', self)
  101.  
  102. self.toolbar1.setFloatable(False) # Prevent docking
  103. self.toolbar1.setMovable(False) # Prevent user from moving it
  104. self.toolbar1.setParent(self) # Detach from the QMainWindow toolbar system
  105.  
  106. # Make the toolbar transparent by setting its attribute
  107. self.toolbar1.setAttribute(Qt.WA_OpaquePaintEvent, False)
  108.  
  109.  
  110. # Set the background of the toolbar to transparent
  111. self.toolbar1.setStyleSheet(
  112. """
  113. QToolBar{
  114. background-color: transparent;
  115. border: 0px;
  116. opacity: 0;
  117. }
  118. QToolButton{
  119. margin: 0px;
  120. background-color: transparent;
  121. opacity: 0;
  122. color: rgb(0, 255, 0);
  123. font-family: 'monofonto';
  124. font-size: 30px;
  125. margin-left: -15px;
  126.  
  127. }
  128. """
  129. )
  130.  
  131. # Create actions
  132. self.actions = [
  133. QAction("Option1", self),
  134. QAction("Option2", self),
  135. QAction("Option3", self)
  136. ]
  137.  
  138. # Add actions to toolbar
  139. for action in self.actions:
  140. self.toolbar1.addAction(action)
  141.  
  142. # Track selected action index
  143. self.current_index = 0
  144. self.update_selection()
  145.  
  146. # Setup rotary encoder on GPIO pins 19 and 20
  147. self.encoder = RotaryEncoder(19, 20)
  148. self.encoder.when_rotated_clockwise = self.next_option
  149. self.encoder.when_rotated_counter_clockwise = self.prev_option
  150.  
  151. def update_selection(self):
  152. # Update toolbar selection by highlighting the current action
  153. for i, action in enumerate(self.actions):
  154. action.setEnabled(i == self.current_index)
  155.  
  156. def next_option(self):
  157. # Rotate forward
  158. self.current_index = (self.current_index + 1) % len(self.actions)
  159. self.update_selection()
  160. print(f"Selected: {self.actions[self.current_index].text()}")
  161.  
  162. def prev_option(self):
  163. # Rotate backward
  164. self.current_index = (self.current_index - 1) % len(self.actions)
  165. self.update_selection()
  166. print(f"Selected: {self.actions[self.current_index].text()}")
  167.  
  168. self.toolbar1.setGeometry(220, 20, 790, 100)
  169.  
  170.  
  171. #SUBTOOLBAR DATA
  172. # Create a toolbar named 'toolbar 2'
  173. self.toolbar2 = QToolBar('toolbar 2', self)
  174.  
  175. self.toolbar2.setFloatable(False) # Prevent docking
  176. self.toolbar2.setMovable(False) # Prevent user from moving it
  177. self.toolbar2.setParent(self) # Detach from the QMainWindow toolbar system
  178.  
  179. # Make the toolbar transparent by setting its attribute
  180. self.toolbar2.setAttribute(Qt.WA_OpaquePaintEvent, False)
  181.  
  182.  
  183. # Set the background of the toolbar to transparent
  184. self.toolbar2.setStyleSheet(
  185. """
  186. QToolBar{
  187. background-color: transparent;
  188. border: 0px;
  189. opacity: 0;
  190. }
  191. QToolButton{
  192. margin: 0px;
  193. background-color: transparent;
  194. opacity: 0;
  195. color: rgb(0, 255, 0);
  196. font-family: 'monofonto';
  197. font-size: 30px;
  198. margin-left: -15px;
  199.  
  200. }
  201. """
  202. )
  203.  
  204. # Add actions to the toolbar (no functionality yet)
  205. action1 = QAction('QUEST', self)
  206. action1.triggered.connect(lambda: self.toolbar2.move(337, 20))
  207. self.toolbar2.addAction(action1)
  208.  
  209. action2 = QAction('NOTES', self)
  210. action2.triggered.connect(lambda: self.toolbar2.move(240, 20))
  211. self.toolbar2.addAction(action2)
  212.  
  213. action3 = QAction('HOLOTAPE', self)
  214. action3.triggered.connect(lambda: self.toolbar2.move(120, 20))
  215. self.toolbar2.addAction(action3)
  216.  
  217. action4 = QAction('DATABASE', self)
  218. action4.triggered.connect(lambda: self.toolbar2.move(-23, 20))
  219. self.toolbar2.addAction(action4)
  220.  
  221. self.toolbar2.setGeometry(337, 20, 790, 100)
  222.  
  223.  
  224. #SUBTOOLBAR RADIO
  225. # Create a toolbar named 'toolbar 3'
  226. self.toolbar3 = QToolBar('toolbar 3', self)
  227.  
  228. self.toolbar3.setFloatable(False) # Prevent docking
  229. self.toolbar3.setMovable(False) # Prevent user from moving it
  230. self.toolbar3.setParent(self) # Detach from the QMainWindow toolbar system
  231.  
  232. # Make the toolbar transparent by setting its attribute
  233. self.toolbar3.setAttribute(Qt.WA_OpaquePaintEvent, False)
  234.  
  235.  
  236. # Set the background of the toolbar to transparent
  237. self.toolbar3.setStyleSheet(
  238. """
  239. QToolBar{
  240. background-color: transparent;
  241. border: 0px;
  242. opacity: 0;
  243. }
  244. QToolButton{
  245. margin: 0px;
  246. background-color: transparent;
  247. opacity: 0;
  248. color: rgb(0, 255, 0);
  249. font-family: 'monofonto';
  250. font-size: 30px;
  251. margin-left: -15px;
  252.  
  253. }
  254. """
  255. )
  256.  
  257. # Add actions to the toolbar (no functionality yet)
  258. action1 = QAction('RADIO', self)
  259. action1.triggered.connect(lambda: self.toolbar3.move(600, 20))
  260. self.toolbar3.addAction(action1)
  261.  
  262. action2 = QAction('MP3', self)
  263. action2.triggered.connect(lambda: self.toolbar3.move(519, 20))
  264. self.toolbar3.addAction(action2)
  265.  
  266. action3 = QAction('HOLOTAPE', self)
  267. action3.triggered.connect(lambda: self.toolbar3.move(415, 20))
  268. self.toolbar3.addAction(action3)
  269.  
  270. action4 = QAction('NOTES', self)
  271. action4.triggered.connect(lambda: self.toolbar3.move(295, 20))
  272. self.toolbar3.addAction(action4)
  273.  
  274. self.toolbar3.setGeometry(600, 20, 790, 100)
  275.  
  276.  
  277.  
  278.  
  279.  
  280. self.label0 = QLabel(self)
  281. self.label0.setGeometry(-450, -500, 1300, 35) # Fixed position
  282. pixmap = QPixmap("SubToolbar_Barrier.png")
  283. self.label0.setPixmap(pixmap)
  284. self.label0.setScaledContents(True)
  285. self.label0.show()
  286.  
  287. # Toolbar Images
  288. label1 = QLabel(self)
  289. label1.setGeometry(10, 50, 780, 15) # Fixed position
  290. pixmap = QPixmap("Toolbarimage2.png")
  291. label1.setPixmap(pixmap)
  292. label1.setScaledContents(True)
  293. label1.show()
  294.  
  295. self.label = QLabel(self)
  296. self.label.setGeometry(390, 1000, 110, 50) # Initial position
  297. pixmap = QPixmap("Toolbarimage1.png")
  298. self.label.setPixmap(pixmap)
  299. self.label.setScaledContents(True)
  300. self.label.show()
  301.  
  302. # Create and customize the toolbar
  303. self.toolbar = QToolBar("Main_Toolbar")
  304. self.toolbar.setFloatable(False) # Prevent docking
  305. self.toolbar.setMovable(False) # Prevent user from moving it
  306. self.toolbar.setParent(self) # Detach from the QMainWindow toolbar system
  307.  
  308. # Make the toolbar transparent by setting its attribute
  309. self.toolbar.setAttribute(Qt.WA_OpaquePaintEvent, False)
  310.  
  311. # Set the background of the toolbar to transparent
  312. self.toolbar.setStyleSheet(
  313. """
  314. QToolBar{
  315. background-color: transparent;
  316. border: 0px;
  317. opacity: 0;
  318. }
  319. QToolButton{
  320. margin: 0px;
  321. background-color: transparent;
  322. opacity: 0;
  323. color: rgb(0, 255, 0);
  324. font-size: 40px;
  325. }
  326. """
  327. )
  328.  
  329. # Add actions to the toolbar
  330. self.actions = []
  331. self.add_toolbar_action("STAT")
  332. self.add_toolbar_action("INV")
  333. self.add_toolbar_action("DATA")
  334. self.add_toolbar_action("NAVI")
  335. self.add_toolbar_action("MUSIC")
  336.  
  337. self.toolbar.setGeometry(80, -20, 790, 100)
  338. self.toolbar.show()
  339.  
  340. # Initialize pygame mixer
  341. pygame.mixer.init()
  342.  
  343. def add_toolbar_action(self, name):
  344. action = QAction(name, self)
  345. action.triggered.connect(lambda checked, option=name: self.change_image_position(option))
  346. self.toolbar.addAction(action)
  347. self.actions.append(action)
  348.  
  349. def change_image_position(self, option):
  350. # Change the position of the second image (label) based on the selected option
  351. if option == "STAT":
  352. self.label.move(90, 10) # Default position
  353. self.label0.move(-500, 50)
  354.  
  355.  
  356. if option == "STAT":
  357. self.toolbar0.show()
  358. else:
  359. self.toolbar0.hide()
  360.  
  361.  
  362. if option == "INV":
  363. self.label.move(207, 10)
  364. self.label0.move(-390, 50)
  365.  
  366. if option == "INV":
  367. self.toolbar1.show()
  368. else:
  369. self.toolbar1.hide()
  370.  
  371.  
  372. if option == "DATA":
  373. self.label.move(322, 10)
  374. self.label0.move(-280, 50)
  375. self.toolbar2.show()
  376.  
  377. if option == "DATA":
  378. self.toolbar2.show()
  379. else:
  380. self.toolbar2.hide()
  381.  
  382.  
  383. if option == "NAVI":
  384. self.label.move(449, 10)
  385.  
  386. if option == "MUSIC":
  387. self.label.move(587, 10)
  388. self.label0.move(0, 50)
  389. self.toolbar3.show()
  390.  
  391. if option == "MUSIC":
  392. self.toolbar3.show()
  393. else:
  394. self.toolbar3.hide()
  395.  
  396. def initGPIO(self):
  397. # GPIO setup
  398. self.gpio_pins = [17, 18, 27, 22, 23] # Example GPIO pins for rotary switch
  399. GPIO.setmode(GPIO.BCM)
  400. for pin in self.gpio_pins:
  401. GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  402.  
  403. # Start a QTimer to poll GPIO states
  404. self.timer = QTimer(self)
  405. self.timer.timeout.connect(self.check_gpio)
  406. self.timer.start(100) # Check every 100ms
  407.  
  408. self.current_index = 0 # Default selected option
  409.  
  410. def check_gpio(self):
  411. for i, pin in enumerate(self.gpio_pins):
  412. if GPIO.input(pin) == GPIO.LOW: # Active LOW
  413. self.handle_rotary_position(i)
  414. break
  415.  
  416. def handle_rotary_position(self, position):
  417. # Determine whether we are moving forward or backward in the toolbar options
  418. if position != self.current_index:
  419. # Determine the direction of movement
  420. if position > self.current_index:
  421. sound_file = "Rotary03.mp3" # Sound for next option
  422. else: sound_file = "RotaryHoriz01.mp3" # Sound for previous option
  423.  
  424. # Update the current index and play the sound
  425. self.current_index = position
  426. self.play_sound(sound_file)
  427.  
  428.  
  429.  
  430.  
  431. # Check the selected action
  432. self.actions[self.current_index].setChecked(True)
  433. # Trigger corresponding image position update
  434. self.change_image_position(self.actions[self.current_index].text())
  435.  
  436. def play_sound(self, sound_file):
  437. try:
  438. pygame.mixer.music.load(sound_file)
  439. pygame.mixer.music.play()
  440. except Exception as e:
  441. print(f"Error playing sound: {e}")
  442.  
  443. def closeEvent(self, event):
  444. # Cleanup GPIO on exit
  445. GPIO.cleanup()
  446. super().closeEvent(event)
  447.  
  448.  
  449. if __name__ == "__main__":
  450. app = QApplication(sys.argv)
  451.  
  452. # Load the custom font
  453. font_id = QFontDatabase.addApplicationFont("monofonto.ttf")
  454. font_family = QFontDatabase.applicationFontFamilies(font_id)[0]
  455. app.setFont(QFont(font_family))
  456.  
  457. window = MainWindow()
  458. window.show()
  459. sys.exit(app.exec_())
  460.  
Advertisement
Add Comment
Please, Sign In to add comment