Bonjour,

Dans un script principal j'explore les mises à jour d'un site internet, à chaque changement sur le site (détecté par l'un des threads) j'appelle la fonction notification() du module qui suit (pygame + win32gui.MoveWindow()) :

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
import pygame, time, threading, os, sys, traceback, multiprocessing
import win32gui
from win32api import GetSystemMetrics
#os.environ['SDL_VIDEODRIVER'] = 'windib'
#os.environ['SDL_VIDEO_CENTERED'] = '1'
 
chemin = os.path.expanduser('~\Notif')
sc_size = (500, 150)
start_pos = (GetSystemMetrics(0),GetSystemMetrics(1))
x,y = start_pos
final_pos = (GetSystemMetrics(0)-510,GetSystemMetrics(1)-215)
#os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % start_pos
#os.environ['SDL_VIDEO_CENTERED'] = '0'
 
pygame.init()
 
pygame.display.set_caption('Notif')
 
bg_color =  57, 59, 61
logo = pygame.image.load(chemin+"\\icone.png")
 
 
default_font = pygame.font.get_default_font()
font1 = pygame.font.SysFont("gabriola", 25, False)
font2 = pygame.font.SysFont("arial", 19, True)
font3 = pygame.font.SysFont("agency fb", 20, False)
 
#Création de la fenêtre
screen = pygame.display.set_mode(sc_size, pygame.NOFRAME)
 
def windowEnumerationHandler(hwnd, top_windows):
    top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
 
def get_hwnd():
    top_windows = []
    win32gui.EnumWindows(windowEnumerationHandler, top_windows)
    for i in top_windows:        
        if 'Notif' in i[1] :
            #print(i[0],i[1])
            return i[0]
 
hwnd = get_hwnd()
win32gui.MoveWindow(hwnd , x, y, sc_size[0], sc_size[1], True)
clock = pygame.time.Clock()
 
def notification(Title='Notification', Subtitle=None, Message1=None, Message2=None, Icone=False, Lien=False):
    title = font1.render(Title, True, (225, 225, 225))
    subtitle = font2.render(Subtitle, True, (255, 255, 255))
    message1 = font3.render(Message1, True, (200, 200, 200))
    message2 = font3.render(Message2, True, (200, 200, 200))
 
    end_time = False    
    done = False
    end_time = False
    retract = False
    icone = Icone
    lien = Lien
 
    while 1:
        screen.fill(bg_color)
 
        screen.blit(logo, (10,5))
        screen.blit(title,(50,9))
        screen.blit(subtitle,(5,40))
        if not icone:            
            screen.blit(message1,(5,70))
            screen.blit(message2,(5,95))
        else:
            img = pygame.image.load(icone)
            screen.blit(img,(5,70))
            screen.blit(message1,(75,70))
            screen.blit(message2,(75,95))
        if not done:
            done = True
            #hwnd = get_hwnd()
            x, y = start_pos
            while x != final_pos[0]:
                x -= 1
                win32gui.MoveWindow(hwnd, x, y, sc_size[0], sc_size[1], True)
                if y > final_pos[1]:
                    y -= 1
                #pygame.display.update()
 
        if not end_time:
            end_time = time.time()+5
 
        if time.time() > end_time and not retract:
            retract = True
            while win32gui.GetWindowRect(hwnd)[0] != start_pos[0]:            
                win32gui.MoveWindow(hwnd, win32gui.GetWindowRect(hwnd)[0]+1, y, sc_size[0], sc_size[1], True)
            win32gui.MoveWindow(hwnd, start_pos[0], start_pos[1], sc_size[0], sc_size[1], True)
            break
        pygame.display.flip()
        clock.tick(60)
 
def exit():
    # Be IDLE friendly
    pygame.quit()
Si je lance le module tout seul et que je fais 15 fois de suite :
multiprocessing.Process(target = notification, kwargs ={'Title':'tutu', 'Subtitle':'toto', 'Message1':'tata', 'Message2':'titi', 'Icone':chemin+'\\new_art.png', 'Lien':False}).start()Ca fonctionne impec !
Le fenêtre de notification glisse de façon fluide et disparaît comme il faut.

Par contre si j'utilise ce code dans le thread de mon script principal là c'est la cata ! (Boucle infinie de créations de processus)
Si j'appelle la fonction avec un thread threading.Thread(target=notification, args=('tutu',...,).start() ou comme ça notification(Title = ...) ça ne marche qu'une fois ou deux voire pas du tout puis la fenêtre de notification freeze et plante.

Je me doute que j'ai loupé un truc basique avec le multiprocessing / multithreading et pygame... Si vous pouvez m'aider à comprendre, je vous en serai reconnaissant !