-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update/change Icon.image while it is running #68
Comments
Thank you for your report. Yes, this is possible. Simply assign to import sys
from PIL import Image, ImageDraw
from pystray import Icon, Menu as menu, MenuItem as item
clicks = []
def image(color1, color2, width=64, height=64):
image = Image.new('RGB', (width, height), color1)
dc = ImageDraw.Draw(image)
dc.rectangle((width // 2, 0, width, height // 2), fill=color2)
dc.rectangle((0, height // 2, width // 2, height), fill=color2)
return image
def on_activate(icon):
clicks.append(icon)
if len(clicks) == 5:
icon.stop()
else:
icon.icon = images[len(clicks) % len(images)]
images = (image('white', 'black'), image('black', 'white'))
icon = Icon(
'test',
icon=images[0],
menu=menu(item('Toggle ', on_activate)))
icon.run() |
Hi, Thank for good example!!! I want to change tasktray icon by time. Every 1 seconds , example.ico change to example2.ico, and change to example3.ico and so on. I made TCP/IP connection Server and i want to cancel TCP connection by putting tasktray ico, while connecting Client and Server, icon change every 1 seconds. Is there any way to solve this question???
Thank you |
It's an old thread but I found a workaround by using recursion to call a function that checks a condition periodically (10 seconds): if true it restarts the icon with an image , if false it restarts with another one. #!/usr/bin/env python
from PIL import Image
import pystray
from time import sleep
import subprocess
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
connected_img = Image.open(dir_path+'/connected.png') # the "connected" image path
disconnected_img = Image.open(dir_path+'/disconnected.png') # the "disconnected" image path
def get_file_content():
## retrieves the content of /etc/resolv.conf
output = subprocess.check_output(['cat','/etc/resolv.conf']).decode('utf-8')
return output
def change_img(ico):
#checks if /etc/resolv.conf contains the string ProtonVpn, if true changes the visible icon
ico.visible=False
ico.icon=(connected_img if "ProtonVPN" in get_file_content() else disconnected_img)
ico.visible=True
sleep(10) # interval between checks if connected to vpn
ico.run(setup=change_img) #this is used for recursively call the change_img function
def on_clicked(icon,item):
#exits the application, this is used to stop the application from within a thread.
os._exit(0)
exit_menu = pystray.MenuItem('exit vpn checker',on_clicked)
ico = pystray.Icon("vpn connection checker",icon=disconnected_img,
menu=pystray.Menu(exit_menu)
)
ico.run(setup=change_img) if you get a error saying |
Hi,
Thank you for your great work! I was trying to figure out how to update/refresh/change icon image while
run()
has already been called on Icon (i.e. while it is running). Ideally, I would love to do this periodically (say, every 5 minutes), but in the worst case, triggering it from a MenuItem callback is also okay.Is this possible with pystray?
Thanks again!
The text was updated successfully, but these errors were encountered: