New ICON_ACTIVATED event for Tray Icon, better Tray/Window interaction management

This commit is contained in:
MikeTheWatchGuy 2018-11-25 16:08:48 -05:00
parent 2f29ada293
commit ddfae6131a
1 changed files with 50 additions and 22 deletions

View File

@ -6,32 +6,50 @@ import textwrap
import pickle import pickle
import base64 import base64
import calendar import calendar
try:
from PySide2.QtWidgets import QApplication, QLabel, QWidget, QLineEdit, QComboBox, QFormLayout, QVBoxLayout, \ FORCE_PYQT5 = True
QHBoxLayout, QListWidget, QDial, QTableWidget
from PySide2.QtWidgets import QSlider, QCheckBox, QRadioButton, QSpinBox, QPushButton, QTextEdit, QMainWindow, QDialog, QAbstractItemView if not FORCE_PYQT5:
from PySide2.QtWidgets import QSpacerItem, QFrame, QGroupBox, QTextBrowser, QPlainTextEdit, QButtonGroup, QFileDialog, QTableWidget, QTabWidget, QTabBar, QTreeWidget, QTreeWidgetItem, QLayout, QTreeWidgetItemIterator, QProgressBar try:
from PySide2.QtWidgets import QTableWidgetItem, QGraphicsView, QGraphicsScene, QGraphicsItemGroup, QMenu, QMenuBar, QAction, QSystemTrayIcon from PySide2.QtWidgets import QApplication, QLabel, QWidget, QLineEdit, QComboBox, QFormLayout, QVBoxLayout, \
from PySide2.QtGui import QPainter, QPixmap, QPen, QColor, QBrush, QPainterPath, QFont, QImage, QIcon QHBoxLayout, QListWidget, QDial, QTableWidget
from PySide2.QtCore import Qt,QProcess, QEvent from PySide2.QtWidgets import QSlider, QCheckBox, QRadioButton, QSpinBox, QPushButton, QTextEdit, QMainWindow, QDialog, QAbstractItemView
import PySide2.QtGui as QtGui from PySide2.QtWidgets import QSpacerItem, QFrame, QGroupBox, QTextBrowser, QPlainTextEdit, QButtonGroup, QFileDialog, QTableWidget, QTabWidget, QTabBar, QTreeWidget, QTreeWidgetItem, QLayout, QTreeWidgetItemIterator, QProgressBar
import PySide2.QtCore as QtCore from PySide2.QtWidgets import QTableWidgetItem, QGraphicsView, QGraphicsScene, QGraphicsItemGroup, QMenu, QMenuBar, QAction, QSystemTrayIcon
import PySide2.QtWidgets as QtWidgets from PySide2.QtGui import QPainter, QPixmap, QPen, QColor, QBrush, QPainterPath, QFont, QImage, QIcon
using_pyqt5 = False from PySide2.QtCore import Qt,QProcess, QEvent
except: import PySide2.QtGui as QtGui
import PySide2.QtCore as QtCore
import PySide2.QtWidgets as QtWidgets
using_pyqt5 = False
except:
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QLineEdit, QComboBox, QFormLayout, QVBoxLayout, \
QHBoxLayout, QListWidget, QDial, QTableWidget
from PyQt5.QtWidgets import QSlider, QCheckBox, QRadioButton, QSpinBox, QPushButton, QTextEdit, QMainWindow, QDialog, QAbstractItemView
from PyQt5.QtWidgets import QSpacerItem, QFrame, QGroupBox, QTextBrowser, QPlainTextEdit, QButtonGroup, QFileDialog, QTableWidget, QTabWidget, QTabBar, QTreeWidget, QTreeWidgetItem, QLayout, QTreeWidgetItemIterator, QProgressBar
from PyQt5.QtWidgets import QTableWidgetItem, QGraphicsView, QGraphicsScene, QGraphicsItemGroup, QMenu, QMenuBar, QAction, QSystemTrayIcon
from PyQt5.QtGui import QPainter, QPixmap, QPen, QColor, QBrush, QPainterPath, QFont, QImage, QIcon
from PyQt5.QtCore import Qt,QProcess, QEvent
import PyQt5.QtGui as QtGui
import PyQt5.QtCore as QtCore
import PyQt5.QtWidgets as QtWidgets
using_pyqt5 = True
else:
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QLineEdit, QComboBox, QFormLayout, QVBoxLayout, \ from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QLineEdit, QComboBox, QFormLayout, QVBoxLayout, \
QHBoxLayout, QListWidget, QDial, QTableWidget QHBoxLayout, QListWidget, QDial, QTableWidget
from PyQt5.QtWidgets import QSlider, QCheckBox, QRadioButton, QSpinBox, QPushButton, QTextEdit, QMainWindow, QDialog, QAbstractItemView from PyQt5.QtWidgets import QSlider, QCheckBox, QRadioButton, QSpinBox, QPushButton, QTextEdit, QMainWindow, \
from PyQt5.QtWidgets import QSpacerItem, QFrame, QGroupBox, QTextBrowser, QPlainTextEdit, QButtonGroup, QFileDialog, QTableWidget, QTabWidget, QTabBar, QTreeWidget, QTreeWidgetItem, QLayout, QTreeWidgetItemIterator, QProgressBar QDialog, QAbstractItemView
from PyQt5.QtWidgets import QTableWidgetItem, QGraphicsView, QGraphicsScene, QGraphicsItemGroup, QMenu, QMenuBar, QAction from PyQt5.QtWidgets import QSpacerItem, QFrame, QGroupBox, QTextBrowser, QPlainTextEdit, QButtonGroup, QFileDialog, \
QTableWidget, QTabWidget, QTabBar, QTreeWidget, QTreeWidgetItem, QLayout, QTreeWidgetItemIterator, QProgressBar
from PyQt5.QtWidgets import QTableWidgetItem, QGraphicsView, QGraphicsScene, QGraphicsItemGroup, QMenu, QMenuBar, \
QAction, QSystemTrayIcon
from PyQt5.QtGui import QPainter, QPixmap, QPen, QColor, QBrush, QPainterPath, QFont, QImage, QIcon from PyQt5.QtGui import QPainter, QPixmap, QPen, QColor, QBrush, QPainterPath, QFont, QImage, QIcon
from PyQt5.QtCore import Qt,QProcess, QEvent from PyQt5.QtCore import Qt, QProcess, QEvent
import PyQt5.QtGui as QtGui import PyQt5.QtGui as QtGui
import PyQt5.QtCore as QtCore import PyQt5.QtCore as QtCore
import PyQt5.QtWidgets as QtWidgets import PyQt5.QtWidgets as QtWidgets
using_pyqt5 = True using_pyqt5 = True
""" """
The QT version if PySimpleGUI. The QT version if PySimpleGUI.
Still being developed. Very limited features. Been in development for less than 2 days so don't expect much!! Still being developed. Very limited features. Been in development for less than 2 days so don't expect much!!
@ -206,6 +224,7 @@ TIMEOUT_KEY = '__TIMEOUT__'
# Key indicating should not create any return values for element # Key indicating should not create any return values for element
WRITE_ONLY_KEY = '__WRITE ONLY__' WRITE_ONLY_KEY = '__WRITE ONLY__'
EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED = '__DOUBLE_CLICKED__' EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED = '__DOUBLE_CLICKED__'
EVENT_SYSTEM_TRAY_ICON_ACTIVATED = '__ACTIVATED__'
EVENT_SYSTEM_TRAY_MESSAGE_CLICKED = '__MESSAGE_CLICKED__' EVENT_SYSTEM_TRAY_MESSAGE_CLICKED = '__MESSAGE_CLICKED__'
# Meny key indicator character / string # Meny key indicator character / string
@ -2672,13 +2691,13 @@ class SystemTray:
self.Menu = menu self.Menu = menu
self.TrayIcon = None self.TrayIcon = None
self.Shown = False self.Shown = False
self.MenuItemChosen = None self.MenuItemChosen = TIMEOUT_KEY
self.Tooltip = tooltip self.Tooltip = tooltip
global _my_windows global _my_windows
if _my_windows.QTApplication is None: if _my_windows.QTApplication is None:
_my_windows.QTApplication = QApplication() _my_windows.QTApplication = QApplication(sys.argv)
self.App = _my_windows.QTApplication self.App = _my_windows.QTApplication
self.QWidget = QWidget() self.QWidget = QWidget()
@ -2723,9 +2742,13 @@ class SystemTray:
def doubleClicked(self, reason): def doubleClicked(self, reason):
if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.DoubleClick): # print(reason)
if reason == QSystemTrayIcon.DoubleClick:
self.MenuItemChosen = EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED self.MenuItemChosen = EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED
self.App.exit() self.App.exit()
if reason == QSystemTrayIcon.Trigger:
self.MenuItemChosen = EVENT_SYSTEM_TRAY_ICON_ACTIVATED
self.App.exit()
def Read(self, timeout=None): def Read(self, timeout=None):
''' '''
@ -2738,13 +2761,15 @@ class SystemTray:
self.TrayIcon.show() self.TrayIcon.show()
if timeout is None: if timeout is None:
self.App.exec_() self.App.exec_()
else:
self.App.processEvents()
else: else:
if timeout is None: if timeout is None:
self.App.exec_() self.App.exec_()
else: else:
self.App.processEvents() self.App.processEvents()
item = self.MenuItemChosen item = self.MenuItemChosen
self.MenuItemChosen = None self.MenuItemChosen = TIMEOUT_KEY
return item return item
@ -3068,6 +3093,9 @@ class Window:
else: else:
if not self.XFound and self.Timeout != 0 and self.Timeout is not None and self.ReturnValues[0] is None: # Special Qt case because returning for no reason so fake timeout if not self.XFound and self.Timeout != 0 and self.Timeout is not None and self.ReturnValues[0] is None: # Special Qt case because returning for no reason so fake timeout
self.ReturnValues = self.TimeoutKey, self.ReturnValues[1] # fake a timeout self.ReturnValues = self.TimeoutKey, self.ReturnValues[1] # fake a timeout
elif not self.XFound: # TODO HIGHLY EXPERIMENTAL... added due to tray icon interaction
# print("*** Faking timeout ***")
self.ReturnValues = self.TimeoutKey, self.ReturnValues[1] # fake a timeout
return self.ReturnValues return self.ReturnValues
def _ReadNonBlocking(self): def _ReadNonBlocking(self):