Actually, my question is the same as this question but the answer didn't help too much, tried the same but didn't work. I want to make a slider works in the tray but what I got is a blank field only.
Is it possible to accomplish this in PyQt5? I tried a lot of things but couldn't find a solution. If it is not possible in PyQt5 then which else library can I use for this?
My code:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (
QSlider,
QHBoxLayout,
QLabel,
QApplication,
QWidgetAction,
)
from PyQt5.QtCore import Qt
def change_sound(value):
print(value)
class SliderWidgetAction(QFrame):
pass
class SliderAction(QWidgetAction):
def __init__(self, label="", parent=None):
QWidgetAction.__init__(self, parent)
self._widget = SliderWidgetAction(parent)
self._label = QLabel(label, self._widget)
self._slider = QSlider(Qt.Horizontal, self._widget)
self._slider.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.value_changed = self._slider.valueChanged
def widget(self):
return self._widget
def label(self):
return self._label
def slider(self):
return self._slider
def createWidget(self, menu):
"""
This method is called by the QWidgetAction base class.
"""
actionWidget = self.widget()
actionLayout = QHBoxLayout(actionWidget)
actionLayout.setContentsMargins(0, 0, 0, 0)
actionLayout.addWidget(self.label())
actionLayout.addWidget(self.slider())
actionWidget.setLayout(actionLayout)
return actionWidget
app = QApplication([])
app.setQuitOnLastWindowClosed(False)
icon = QIcon("icon.png")
tray = QSystemTrayIcon()
tray.setIcon(icon)
tray.setVisible(True)
menu = QMenu()
action = SliderAction("Test", menu)
action.slider().setMinimum(10)
action.slider().setMaximum(100)
action.slider().setValue(90)
action.slider().valueChanged.connect(change_sound)
label_widget = QLabel("Hello World")
action.setDefaultWidget(label_widget)
menu.addAction(action)
tray.setContextMenu(menu)
app.exec_()