Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import RPi.GPIO as GPIO
- from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QAction, QLabel
- from PyQt5.QtGui import QFontDatabase, QPalette, QColor, QFont, QPixmap
- from PyQt5.QtCore import Qt, QTimer
- from gpiozero import RotaryEncoder
- import pygame
- class MainWindow(QMainWindow):
- def __init__(self):
- super().__init__()
- self.initUI()
- self.initGPIO()
- def initUI(self):
- # Set the window to fullscreen
- self.showFullScreen()
- # Set the background color to black
- palette = self.palette()
- palette.setColor(QPalette.Window, QColor("black"))
- self.setPalette(palette)
- #SUBTOOLBAR STAT
- # Create a toolbar named 'toolbar 0'
- self.toolbar0 = QToolBar('toolbar 0', self)
- self.toolbar0.setFloatable(False) # Prevent docking
- self.toolbar0.setMovable(False) # Prevent user from moving it
- self.toolbar0.setParent(self) # Detach from the QMainWindow toolbar system
- # Make the toolbar transparent by setting its attribute
- self.toolbar0.setAttribute(Qt.WA_OpaquePaintEvent, False)
- # Set the background of the toolbar to transparent
- self.toolbar0.setStyleSheet(
- """
- QToolBar{
- background-color: transparent;
- border: 0px;
- opacity: 0;
- }
- QToolButton{
- margin: 0px;
- background-color: transparent;
- opacity: 0;
- color: rgb(0, 255, 0);
- font-family: 'monofonto';
- font-size: 30px;
- margin-left: -15px;
- }
- """
- )
- # Create actions
- self.actions = [
- QAction("Option1", self),
- QAction("Option2", self),
- QAction("Option3", self)
- ]
- # Add actions to toolbar
- for action in self.actions:
- self.toolbar0.addAction(action)
- # Track selected action index
- self.current_index = 0
- self.update_selection()
- # Setup rotary encoder on GPIO pins 19 and 20
- self.encoder = RotaryEncoder(19, 20)
- self.encoder.when_rotated_clockwise = self.next_option
- self.encoder.when_rotated_counter_clockwise = self.prev_option
- def update_selection(self):
- # Update toolbar selection by highlighting the current action
- for i, action in enumerate(self.actions):
- action.setEnabled(i == self.current_index)
- def next_option(self):
- # Rotate forward
- self.current_index = (self.current_index + 1) % len(self.actions)
- self.update_selection()
- print(f"Selected: {self.actions[self.current_index].text()}")
- def prev_option(self):
- # Rotate backward
- self.current_index = (self.current_index - 1) % len(self.actions)
- self.update_selection()
- print(f"Selected: {self.actions[self.current_index].text()}")
- self.toolbar0.setGeometry(95, 20, 790, 100)
- #SUBTOOLBAR INV
- # Cree a toolbar named 'toolbar 1'
- self.toolbar1 = QToolBar('toolbar 1', self)
- self.toolbar1.setFloatable(False) # Prevent docking
- self.toolbar1.setMovable(False) # Prevent user from moving it
- self.toolbar1.setParent(self) # Detach from the QMainWindow toolbar system
- # Make the toolbar transparent by setting its attribute
- self.toolbar1.setAttribute(Qt.WA_OpaquePaintEvent, False)
- # Set the background of the toolbar to transparent
- self.toolbar1.setStyleSheet(
- """
- QToolBar{
- background-color: transparent;
- border: 0px;
- opacity: 0;
- }
- QToolButton{
- margin: 0px;
- background-color: transparent;
- opacity: 0;
- color: rgb(0, 255, 0);
- font-family: 'monofonto';
- font-size: 30px;
- margin-left: -15px;
- }
- """
- )
- # Create actions
- self.actions = [
- QAction("Option1", self),
- QAction("Option2", self),
- QAction("Option3", self)
- ]
- # Add actions to toolbar
- for action in self.actions:
- self.toolbar1.addAction(action)
- # Track selected action index
- self.current_index = 0
- self.update_selection()
- # Setup rotary encoder on GPIO pins 19 and 20
- self.encoder = RotaryEncoder(19, 20)
- self.encoder.when_rotated_clockwise = self.next_option
- self.encoder.when_rotated_counter_clockwise = self.prev_option
- def update_selection(self):
- # Update toolbar selection by highlighting the current action
- for i, action in enumerate(self.actions):
- action.setEnabled(i == self.current_index)
- def next_option(self):
- # Rotate forward
- self.current_index = (self.current_index + 1) % len(self.actions)
- self.update_selection()
- print(f"Selected: {self.actions[self.current_index].text()}")
- def prev_option(self):
- # Rotate backward
- self.current_index = (self.current_index - 1) % len(self.actions)
- self.update_selection()
- print(f"Selected: {self.actions[self.current_index].text()}")
- self.toolbar1.setGeometry(220, 20, 790, 100)
- #SUBTOOLBAR DATA
- # Create a toolbar named 'toolbar 2'
- self.toolbar2 = QToolBar('toolbar 2', self)
- self.toolbar2.setFloatable(False) # Prevent docking
- self.toolbar2.setMovable(False) # Prevent user from moving it
- self.toolbar2.setParent(self) # Detach from the QMainWindow toolbar system
- # Make the toolbar transparent by setting its attribute
- self.toolbar2.setAttribute(Qt.WA_OpaquePaintEvent, False)
- # Set the background of the toolbar to transparent
- self.toolbar2.setStyleSheet(
- """
- QToolBar{
- background-color: transparent;
- border: 0px;
- opacity: 0;
- }
- QToolButton{
- margin: 0px;
- background-color: transparent;
- opacity: 0;
- color: rgb(0, 255, 0);
- font-family: 'monofonto';
- font-size: 30px;
- margin-left: -15px;
- }
- """
- )
- # Add actions to the toolbar (no functionality yet)
- action1 = QAction('QUEST', self)
- action1.triggered.connect(lambda: self.toolbar2.move(337, 20))
- self.toolbar2.addAction(action1)
- action2 = QAction('NOTES', self)
- action2.triggered.connect(lambda: self.toolbar2.move(240, 20))
- self.toolbar2.addAction(action2)
- action3 = QAction('HOLOTAPE', self)
- action3.triggered.connect(lambda: self.toolbar2.move(120, 20))
- self.toolbar2.addAction(action3)
- action4 = QAction('DATABASE', self)
- action4.triggered.connect(lambda: self.toolbar2.move(-23, 20))
- self.toolbar2.addAction(action4)
- self.toolbar2.setGeometry(337, 20, 790, 100)
- #SUBTOOLBAR RADIO
- # Create a toolbar named 'toolbar 3'
- self.toolbar3 = QToolBar('toolbar 3', self)
- self.toolbar3.setFloatable(False) # Prevent docking
- self.toolbar3.setMovable(False) # Prevent user from moving it
- self.toolbar3.setParent(self) # Detach from the QMainWindow toolbar system
- # Make the toolbar transparent by setting its attribute
- self.toolbar3.setAttribute(Qt.WA_OpaquePaintEvent, False)
- # Set the background of the toolbar to transparent
- self.toolbar3.setStyleSheet(
- """
- QToolBar{
- background-color: transparent;
- border: 0px;
- opacity: 0;
- }
- QToolButton{
- margin: 0px;
- background-color: transparent;
- opacity: 0;
- color: rgb(0, 255, 0);
- font-family: 'monofonto';
- font-size: 30px;
- margin-left: -15px;
- }
- """
- )
- # Add actions to the toolbar (no functionality yet)
- action1 = QAction('RADIO', self)
- action1.triggered.connect(lambda: self.toolbar3.move(600, 20))
- self.toolbar3.addAction(action1)
- action2 = QAction('MP3', self)
- action2.triggered.connect(lambda: self.toolbar3.move(519, 20))
- self.toolbar3.addAction(action2)
- action3 = QAction('HOLOTAPE', self)
- action3.triggered.connect(lambda: self.toolbar3.move(415, 20))
- self.toolbar3.addAction(action3)
- action4 = QAction('NOTES', self)
- action4.triggered.connect(lambda: self.toolbar3.move(295, 20))
- self.toolbar3.addAction(action4)
- self.toolbar3.setGeometry(600, 20, 790, 100)
- self.label0 = QLabel(self)
- self.label0.setGeometry(-450, -500, 1300, 35) # Fixed position
- pixmap = QPixmap("SubToolbar_Barrier.png")
- self.label0.setPixmap(pixmap)
- self.label0.setScaledContents(True)
- self.label0.show()
- # Toolbar Images
- label1 = QLabel(self)
- label1.setGeometry(10, 50, 780, 15) # Fixed position
- pixmap = QPixmap("Toolbarimage2.png")
- label1.setPixmap(pixmap)
- label1.setScaledContents(True)
- label1.show()
- self.label = QLabel(self)
- self.label.setGeometry(390, 1000, 110, 50) # Initial position
- pixmap = QPixmap("Toolbarimage1.png")
- self.label.setPixmap(pixmap)
- self.label.setScaledContents(True)
- self.label.show()
- # Create and customize the toolbar
- self.toolbar = QToolBar("Main_Toolbar")
- self.toolbar.setFloatable(False) # Prevent docking
- self.toolbar.setMovable(False) # Prevent user from moving it
- self.toolbar.setParent(self) # Detach from the QMainWindow toolbar system
- # Make the toolbar transparent by setting its attribute
- self.toolbar.setAttribute(Qt.WA_OpaquePaintEvent, False)
- # Set the background of the toolbar to transparent
- self.toolbar.setStyleSheet(
- """
- QToolBar{
- background-color: transparent;
- border: 0px;
- opacity: 0;
- }
- QToolButton{
- margin: 0px;
- background-color: transparent;
- opacity: 0;
- color: rgb(0, 255, 0);
- font-size: 40px;
- }
- """
- )
- # Add actions to the toolbar
- self.actions = []
- self.add_toolbar_action("STAT")
- self.add_toolbar_action("INV")
- self.add_toolbar_action("DATA")
- self.add_toolbar_action("NAVI")
- self.add_toolbar_action("MUSIC")
- self.toolbar.setGeometry(80, -20, 790, 100)
- self.toolbar.show()
- # Initialize pygame mixer
- pygame.mixer.init()
- def add_toolbar_action(self, name):
- action = QAction(name, self)
- action.triggered.connect(lambda checked, option=name: self.change_image_position(option))
- self.toolbar.addAction(action)
- self.actions.append(action)
- def change_image_position(self, option):
- # Change the position of the second image (label) based on the selected option
- if option == "STAT":
- self.label.move(90, 10) # Default position
- self.label0.move(-500, 50)
- if option == "STAT":
- self.toolbar0.show()
- else:
- self.toolbar0.hide()
- if option == "INV":
- self.label.move(207, 10)
- self.label0.move(-390, 50)
- if option == "INV":
- self.toolbar1.show()
- else:
- self.toolbar1.hide()
- if option == "DATA":
- self.label.move(322, 10)
- self.label0.move(-280, 50)
- self.toolbar2.show()
- if option == "DATA":
- self.toolbar2.show()
- else:
- self.toolbar2.hide()
- if option == "NAVI":
- self.label.move(449, 10)
- if option == "MUSIC":
- self.label.move(587, 10)
- self.label0.move(0, 50)
- self.toolbar3.show()
- if option == "MUSIC":
- self.toolbar3.show()
- else:
- self.toolbar3.hide()
- def initGPIO(self):
- # GPIO setup
- self.gpio_pins = [17, 18, 27, 22, 23] # Example GPIO pins for rotary switch
- GPIO.setmode(GPIO.BCM)
- for pin in self.gpio_pins:
- GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- # Start a QTimer to poll GPIO states
- self.timer = QTimer(self)
- self.timer.timeout.connect(self.check_gpio)
- self.timer.start(100) # Check every 100ms
- self.current_index = 0 # Default selected option
- def check_gpio(self):
- for i, pin in enumerate(self.gpio_pins):
- if GPIO.input(pin) == GPIO.LOW: # Active LOW
- self.handle_rotary_position(i)
- break
- def handle_rotary_position(self, position):
- # Determine whether we are moving forward or backward in the toolbar options
- if position != self.current_index:
- # Determine the direction of movement
- if position > self.current_index:
- sound_file = "Rotary03.mp3" # Sound for next option
- else: sound_file = "RotaryHoriz01.mp3" # Sound for previous option
- # Update the current index and play the sound
- self.current_index = position
- self.play_sound(sound_file)
- # Check the selected action
- self.actions[self.current_index].setChecked(True)
- # Trigger corresponding image position update
- self.change_image_position(self.actions[self.current_index].text())
- def play_sound(self, sound_file):
- try:
- pygame.mixer.music.load(sound_file)
- pygame.mixer.music.play()
- except Exception as e:
- print(f"Error playing sound: {e}")
- def closeEvent(self, event):
- # Cleanup GPIO on exit
- GPIO.cleanup()
- super().closeEvent(event)
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- # Load the custom font
- font_id = QFontDatabase.addApplicationFont("monofonto.ttf")
- font_family = QFontDatabase.applicationFontFamilies(font_id)[0]
- app.setFont(QFont(font_family))
- window = MainWindow()
- window.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment