Bonsoir,

Après de longues recherches je capitule encore une fois..

Je cherche simplement à remplacer l'icone de mon SysTray pendant la marche de mon programme. (Icone état de marche/Etat d'arrêt)

Le problème est que quand je fais Hide(), Show() le menu de mon SysTray ne fonctionne plus..

Le but ultime de cette manœuvre est que lorsque l'utilisateur entre la combinaison de touche CTRL + NUMPAD0,

le programme change son état de marche et donc change son icone SysTray.



Voici une partie du code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python
 
from threading import Thread
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import os, sys, time, ctypes, win32con
from ctypes import wintypes
 
fenetre = None
trayIcon = None
statut_on_off = True
 
 
class MaFenetre(QWidget):
 
     def __init__(self, FL=220, FH=100):
        QWidget.__init__(self) 
 
        self.setWindowIcon(QIcon("icone64.ico"))
        self.setWindowTitle("Window")
        self.setFixedSize(FL, FH)
 
     def closeEvent(self, event): 
        global trayIcon
        event.ignore()
        self.hide()       
 
 
 
 
class SystemTrayIcon(QSystemTrayIcon):
 
    def __init__(self, icon, parent=None):
        QSystemTrayIcon.__init__(self, icon, parent)
        self.menu = QMenu(parent)
        self.exitAction = self.menu.addAction("Exit")
        self.MenuPrincipal = self.menu.addAction("Menu principal")
        self.exitAction.triggered.connect(SystemTrayIcon.EXIT)
        self.MenuPrincipal.triggered.connect(SystemTrayIcon.MenuPrincipal)
        self.setContextMenu(self.menu)
 
    def EXIT():
        QApplication.quit()
 
    def MenuPrincipal(self,):
        global trayIcon
        global fenetre
        fenetre.show()
 
    def on_off(self,):
        global statut_on_off
        global trayIcon
 
        if statut_on_off == True:
            statut_on_off = False
            trayIcon.hide()
        else:
            statut_on_off = True
            trayIcon.show()
        return
 
 
def HotKeysS():
 
  byref = ctypes.byref
  user32 = ctypes.windll.user32   
 
  def NUMPAD0():
    SystemTrayIcon.on_off(trayIcon)  
 
 
  HOTKEYS = {
    0 : (win32con.VK_NUMPAD0, win32con.MOD_CONTROL),
  }
 
 
  HOTKEY_ACTIONS = {
    0 : NUMPAD0,
  }
 
  for id, (vk, modifiers) in HOTKEYS.items():
    print ("Registering id", id, "for key", vk)
    if not user32.RegisterHotKey (None, id, modifiers, vk):
      print ("Unable to register id", id)
 
 
  try:
    msg = wintypes.MSG ()
    while user32.GetMessageA (byref (msg), None, 0, 0) != 0:
      if msg.message == win32con.WM_HOTKEY:
        action_to_take = HOTKEY_ACTIONS.get (msg.wParam)
        if action_to_take:
          action_to_take ()
      user32.TranslateMessage (byref (msg))
      user32.DispatchMessageA (byref (msg))
  finally:
    for id in HOTKEYS.keys ():
      user32.UnregisterHotKey (None, id)
 
 
 
def main():
    global trayIcon
    global fenetre  
    a=QApplication(sys.argv)
    TI=QWidget()
    trayIcon = SystemTrayIcon(QIcon("icone64.ico"), TI)
    fenetre=MaFenetre()
    trayIcon.show()
    sys.exit(a.exec_())
 
thread_1 = Thread(target = HotKeysS)
 
if __name__ == '__main__':
    thread_1.start()
    main()
Avant de pousser sur CTRL+NUMPAD0
Nom : Systray1.png
Affichages : 365
Taille : 127,1 Ko

Après..
Nom : Systray2.png
Affichages : 313
Taille : 67,2 Ko


Voilà si quelqu'un a l'explication je lui en serais super reconnaissant !

Merci d'avance,

Bonne journée
Gauthier