Unfortunately, as an AI language model, I cannot implement the changes as I do not have access to the complete codebase and the system environment necessary for execution. However, I can provide suggestions for improving the code's performance:
- Use a faster framework such as PyQt or PySide instead of tkinter.
- Use threading to prevent the UI from freezing while performing long operations like sending data. You can use the Python threading module for this.
- Refresh the UI more frequently, both before and after the operation. You can use the refresh() function to update the UI faster.
Here's an example of how you can implement threading in your on_button_click():
import threading
def on_button_click(data_path):
threading.Thread(target=send_data, args=(data_path,)).start()
This will start a new thread to call the send_data() function whenever a button is clicked, preventing the UI from freezing.
To refresh the UI more frequently, you can use the refresh() function:
def refresh():
window.update_idletasks()
window.after(100, refresh)
window.after(100, refresh)
This will refresh the UI every 100 milliseconds.
With these improvements, the resulting code could look like this:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QPushButton, QLabel, QGridLayout, QMenu
from PyQt5.QtCore import Qt, QThread
from ttkthemes import ThemedStyle
import subprocess
device_path = "/home/blu/broadlink/BEDROOM.device"
prev_selected_device = None
class Worker(QThread):
def __init__(self, path):
super().__init__()
self.path = path
def run(self):
command = f"broadlink_cli --device @{device_path} --send @{self.path}"
subprocess.run(command, shell=True)
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Broadlink Device Control'
self.left = 10
self.top = 10
self.width = 400
self.height = 400
self.devices_dropdown = None
self.button_frame = None
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.devices_dropdown = QComboBox(self)
self.devices_dropdown.move(20, 20)
self.devices_dropdown.currentIndexChanged.connect(self.update_buttons)
for device in devices:
self.devices_dropdown.addItem(device)
self.update_buttons()
self.show()
def update_window_size(self):
if self.button_frame is None:
return
self.updateGeometry()
def send_data(self, data_path):
Worker(data_path).start()
def on_button_click(self, data_path):
self.send_data(data_path)
def copy_command_line(self, data_path):
command = f"broadlink_cli --device @{device_path} --send @{data_path}"
QApplication.clipboard().setText(command)
self.show_message_toast("Command copied to clipboard!")
def update_buttons(self):
global prev_selected_device
selected_device = self.devices_dropdown.currentText()
if selected_device == prev_selected_device:
return
prev_selected_device = selected_device
data_paths = devices[selected_device]
if self.button_frame is not None:
self.button_frame.deleteLater()
self.button_frame = QGridLayout(self)
self.button_frame.setSpacing(10)
row_counter = 0
col_counter = 0
max_columns = 3
for category, entries in data_paths.items():
if col_counter != 0:
row_counter += 1
col_counter = 0
category_label = QLabel(category, self)
self.button_frame.addWidget(category_label, row_counter, col_counter, 1, max_columns)
row_counter += 1
for label, path in entries.items():
button = QPushButton(label, self)
self.button_frame.addWidget(button, row_counter, col_counter)
button.clicked.connect(lambda _, path=path: self.on_button_click(path))
button.setFixedWidth(150)
menu = QMenu()
menu.addAction("Copy Command Line", lambda _, path=path: self.copy_command_line(path))
def show_context_menu(event):
menu.exec_(event.globalPos())
button.setContextMenuPolicy(Qt.CustomContextMenu)
button.customContextMenuRequested.connect(show_context_menu)
col_counter += 1
if col_counter >= max_columns:
col_counter = 0
row_counter += 1
self.update_window_size()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Up:
self.devices_dropdown.setCurrentIndex((self.devices_dropdown.currentIndex() - 1) % self.devices_dropdown.count())
self.update_buttons()
elif event.key() == Qt.Key_Down:
self.devices_dropdown.setCurrentIndex((self.devices_dropdown.currentIndex() + 1) % self.devices_dropdown.count())
self.update_buttons()
def show_message_toast(self, message):
toast_label = QLabel(message, self)
toast_label.setStyleSheet("background-color: white; border-radius: 5px; padding: 10px;")
toast_label.setAlignment(Qt.AlignCenter)
self.button_frame.addWidget(toast_label, self.button_frame.rowCount(), 0, 1, self.button_frame.columnCount())
self.update_window_size()
devices = {
"Vornado-6303DC": {
"Power": {
"Vornado On/Off": "/home/blu/broadlink/Vornado/6303DC/Vornado-on-off"
},
"Wind": {
"Vornado Wind Down": "/home/blu/broadlink/Vornado/6303DC/Vornado-wind-down",
"Vornado Wind Up": "/home/blu/broadlink/Vornado/6303DC/Vornado-wind-up"
},
"Timer": {
"Vornado Timer": "/home/blu/broadlink/Vornado/6303DC/Vornado-timer"
}
},
"RME-ADI2-DAC-FS": {
"Volume": {
"Volume Up": "/home/blu/broadlink/RME/ADI2-DAC-FS/vol+",
"Volume Down": "/home/blu/broadlink/RME/ADI2-DAC-FS/vol-"
},
"Equalizer": {
"Treble+": "/home/blu/broadlink/RME/ADI2-DAC-FS/treble+",
"Treble-": "/home/blu/broadlink/RME/ADI2-DAC-FS/treble-",
"Bass+": "/home/blu/broadlink/RME/ADI2-DAC-FS/bass+",
"Bass-": "/home/blu/broadlink/RME/ADI2-DAC-FS/bass-"
},
"Switches": {
"Toggle Headphone/Speaker": "/home/blu/broadlink/RME/ADI2-DAC-FS/RME-switch-headphone-speaker",
"Toggle Dim": "/home/blu/broadlink/RME/ADI2-DAC-FS/dim",
"Toggle Loudness": "/home/blu/broadlink/RME/ADI2-DAC-FS/loudness",
"Toggle Power": "/home/blu/broadlink/RME/ADI2-DAC-FS/power",
"Power Held": "/home/blu/broadlink/RME/ADI2-DAC-FS/power-held"
},
"Channel Balance": {
"Left Channel Balance": "/home/blu/broadlink/RME/ADI2-DAC-FS/L-chan-bal",
"Right Channel Balance": "/home/blu/broadlink/RME/ADI2-DAC-FS/r-chan-bal"
},
"Other": {
"1-ana": "/home/blu/broadlink/RME/ADI2-DAC-FS/1-ana",
"2-aes": "/home/blu/broadlink/RME/ADI2-DAC-FS/2-aes",
"3-dark": "/home/blu/broadlink/RME/ADI2-DAC-FS/3-dark",
"Select": "/home/blu/broadlink/RME/ADI2-DAC-FS/sel"
}
},
"IQAir Health Pro 250": {
"Power": {
"IQAir On/Off": "/home/blu/broadlink/IQAir/Health-Pro-250/IQAir-on-off"
},
"Filtration": {
"Filtration Level 1": "/home/blu/broadlink/IQAir/Health-Pro-250/IQAir-HealthPro-filtration-level-1",
"Filtration Level 2": "/home/blu/broadlink/IQAir/Health-Pro-250/IQAir-HealthPro-filtration-level-2",
"Filtration Level 3": "/home/blu/broadlink/IQAir/Health-Pro-250/IQAir-HealthPro-filtration-level-3",
"Filtration Level 4": "/home/blu/broadlink/IQAir/Health-Pro-250/IQAir-HealthPro-filtration-level-4",
"Filtration Level 5": "/home/blu/broadlink/IQAir/Health-Pro-250/IQAir-HealthPro-filtration-level-5",
"Filtration Level 6": "/home/blu/broadlink/IQAir/Health-Pro-250/IQAir-Health