There is a program like chat. About 5 users. The exchange of information is not constant, that is, they write as necessary and there may be large temporary breaks. I want to make it so that after someone wrote a message, it was possible to attract the attention of other users who are running the program. Sound without options (no speakers). Ideally, it would be flashing the program on the panel. The program will work on windows server 2008 r2. In Google I can not find anything applicable to Python (I have 3.4). This is my first program.

# -*- coding: utf-8 -*- import tkinter from tkinter import * from tkinter import messagebox as mb import time name = str() fname = str(time.strftime("%d_%m_%Y")) def messange(): global mdname if entry.get() < '0': mb.showerror('Ошибка', 'Π’Ρ‹ Π½Π΅ написали сообщСниС, сообщСниС Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π½Π°Ρ‡ΠΈΠ½Π°Ρ‚ΡŒΡΡ с ΠΏΡ€ΠΎΠ±Π΅Π»Π°') # Ошибка else: mess = entry.get() entry.delete(0, END) f = open(fname + '.txt', 'a') f.write(time.strftime("%H:%M") + ' ' + name + ' ΠΏΠΈΡˆΠ΅Ρ‚: ' + mess + '\n') f.close() def login(event): global name name = entry.get() if name < '0': mb.showerror('Ошибка', 'ΠΠ°ΠΏΠΈΡˆΠΈΡ‚Π΅ вашС имя, имя Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π½Π°Ρ‡ΠΈΠ½Π°Ρ‚ΡŒΡΡ с ΠΏΡ€ΠΎΠ±Π΅Π»Π°') # Ошибка else: event.widget.pack_forget() # Π‘ΠΊΡ€Ρ‹Ρ‚ΡŒ скнопку name = entry.get() entry.delete(0, END) f = open(fname + '.txt', 'a') f.write('ΠŸΡ€ΠΈΠ²Π΅Ρ‚ ' + name + '\n') f.close() button_visible_false.pack() # ΠžΡ‚ΠΎΠ±Ρ€Π°Π·ΠΈΡ‚ΡŒ ΠΊΠ½ΠΎΠΏΠΊΡƒ while True: f = open(fname + '.txt') data = f.read() time.sleep(0.3) f.close() textbox.delete(1.0, END) textbox.insert(1.0, data) textbox.see("end") root.update_idletasks() root.update() root = Tk() root.title('Π“Π°Ρ€ΠΌΠΎΠ½ΠΈΡ‡Π½Ρ‹ΠΉ Ρ‡Π°Ρ‚ v.1') panelFrame = Frame(root, height = 60, bg = 'green') textFrame = Frame(root, height = 340, width = 600) panelFrame.pack(side = 'top', fill = 'x') textFrame.pack(side = 'bottom', fill = 'both', expand = 1) textbox = Text(textFrame, font='Arial 10', wrap='word') scrollbar = Scrollbar(textFrame) scrollbar['command'] = textbox.yview textbox['yscrollcommand'] = scrollbar.set textbox.pack(side = 'left', fill = 'both', expand = 1) scrollbar.pack(side = 'right', fill = 'y') entry = Entry(width = 80) entry.pack(pady = 10) btn1 = Button(panelFrame, text='ΠΠ°ΠΏΠΈΡˆΠΈΡ‚Π΅ вашС имя', width=17, height=3, bg="white", fg="black") btn1.bind('<Button-1>', login) btn1.pack() button_visible_false = tkinter.Button(panelFrame, text='ΠΠ°ΠΏΠΈΡΠ°Ρ‚ΡŒ сообщСниС', width=17, height=3, bg="white", fg="black", command = messange) try: f = open(fname + '.txt') except: f = open(fname + '.txt', 'w') else: pass root.mainloop() 

    1 answer 1

    You can use the FlashWindow function to FlashWindow :

     import ctypes import tkinter as tk root = tk.Tk() def flash_window(): hwnd = int(root.wm_frame(), 16) # wm_frame() Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ хэндл ΠΎΠΊΠ½Π° Π² Π²ΠΈΠ΄Π΅ строки Π² 16-Ρ€ΠΈΡ‡Π½ΠΎΠΉ систСмС, ΠΏΠ΅Ρ€Π΅Π²ΠΎΠ΄ΠΈΠΌ Π² число ctypes.windll.user32.FlashWindow(hwnd, True) root.after(1000, flash_window) root.mainloop() 

    The active window will flash once, the minimized one will be highlighted (at least on Windows7 it works like this).

    It is not necessary to do it after after , it is enough that the flash_window function flash_window called from somewhere in the event handler, after root.mainloop() started.

    To blink several times, you can use FlashWindowEx (this is a bit more complicated due to the fact that the data is passed to the function through the FLASHINFO structure):

     import ctypes import tkinter as tk class FLASHWINFO(ctypes.Structure): _fields_ = [ ("cbSize", ctypes.c_uint), ("hwnd", ctypes.c_uint), ("dwFlags", ctypes.c_uint), ("uCount", ctypes.c_uint), ("dwTimeout", ctypes.c_uint) ] # Codes for dwFlags FLASHW_ALL = 3 # Flash both the window caption and taskbar button. This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. FLASHW_CAPTION = 1 # Flash the window caption. FLASHW_STOP = 0 # Stop flashing. The system restores the window to its original state. FLASHW_TIMER = 4 # Flash continuously, until the FLASHW_STOP flag is set. FLASHW_TIMERNOFG = 0xC # Flash continuously until the window comes to the foreground. FLASHW_TRAY = 2 # Flash the taskbar button. def __init__(self, hwnd, dwFlags, uCount, dwTimeout): super().__init__() self.cbSize = ctypes.sizeof(self) self.hwnd = hwnd self.dwFlags = dwFlags self.uCount = uCount self.dwTimeout = dwTimeout root = tk.Tk() def flash_window(): hwnd = int(root.wm_frame(), 16) flashinfo = FLASHWINFO(hwnd, FLASHWINFO.FLASHW_ALL, 3, 100) ctypes.windll.user32.FlashWindowEx(ctypes.byref(flashinfo)) root.after(1000, flash_window) root.mainloop()