K
K
kramick2021-07-05 15:25:23
Python
kramick, 2021-07-05 15:25:23

How to commit a change to a variable?

Hey! To test my previously gained knowledge about the Tkinter library, I decided to create a small application that displayed to the user his number of VK messages. Implemented, but also decided to supplement the application with a system sound , they say, like in VK itself, the sound when you receive a new message.

But I ran into a problem - the sound is played an infinite number of times until you check the message . Tell me how you can implement playback so that it is played only once with a new message ? How to capture the change in the "number of unread messages" variable
PS core module, which I import in the code, imports a function that returns the number of unread messages

from tkinter import *
import core

'''Создаём GUI для Vk Mini'''

def update_root():    # функция обновления окна
    count_messages.config(text= f'У вас {core.unread_messages()} новых сообщений!')
    root.after(1000, update_root)

root = Tk()

img = PhotoImage(file='path')
lab = Label(image=img)
lab.img = 'path'
lab.pack(side=LEFT)    # выводим на экран иконку вконтакте с левой стороны

count_messages = Label(    # количество сообщений
    text = f'У вас {core.unread_messages()} новых сообщений!',
    width=25,
    height=5,
    fg='black'
)
count_messages.pack(side=TOP) # выводим текст (выше)

root.after(1000, update_root()) # обновляем окно каждую секунду
root.mainloop()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Serg_sel, 2021-07-05
@kramick

Of course, I don’t really know python, but you can try to get rid of the endless sound in something like this, plus this is a check for a change in the number of unread messages (and if there are fewer, but not 0, then nothing will happen again):

old_unread_messages = 0 # если при запуске программы не нужен сразу звук уведомления, использовать нижнюю строчку
# old_unread_messages = core.unread_messages
ping = False
if old_unread_messages - core_unread_messages >= 0 and not ping and core.unread_messages != 0:
    # тут вызов звука уведомления
    ping = True

Of course, this is probably a crutch, but I haven’t come up with anything better yet.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question