Dynamic system tray text (Python 3)How to build a SystemTray app for Windows?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?
Safely hang a mirror that does not have hooks
Would Taiwan and China's dispute be solved if Taiwan gave up being the Republic of China?
Examples of "unsuccessful" theories with afterlives
A drug that allows people to survive on less food
Worms crawling under skin
What is the need of methods like GET and POST in the HTTP protocol?
Counting most common combination of values in dataframe column
How does IBM's 53-bit quantum computer compare to classical ones for cryptanalytic tasks?
The 100 soldier problem
Was there a trial by combat between a man and a dog in medieval France?
Is there any reason nowadays to use a neon indicator lamp instead of an LED?
Do we know the situation in Britain before Sealion (summer 1940)?
Algorithm that spans orthogonal vectors: Python
Can this word order be rearranged?
Do the villains know Batman has no superpowers?
Late 1970's and 6502 chip facilities for operating systems
Will Proving or Disproving of any of the following have effects on Chemistry in general?
How to make interviewee comfortable interviewing in lounge chairs
Does wetting a beer glass change the foam characteristics?
What is the meaning of "heutig" in this sentence?
What can a pilot do if an air traffic controller is incapacitated?
Is it impolite to ask for halal food when traveling to and in Thailand?
Can the U.S. president make military decisions without consulting anyone?
Hiking with a mule or two?
Dynamic system tray text (Python 3)
How to build a SystemTray app for Windows?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to get a dynamic text displayed in the system tray (this will be 2 numbers (from 1 to 100) changing every 2 minutes).
I found this script as a starting point (but I am not commited to it!).
But I get this error :
TypeError: Image.SetData(): arguments did not match any overloaded call:
overload 1: argument 1 has unexpected type 'str'
overload 2: argument 1 has unexpected type 'str'
OnInit returned false, exiting...
The relevant part of the code is:
def Get(self,l,r):
s=""+self.s_line
for i in range(5):
if i<(5-l):
sl = self.sl_off
else:
sl = self.sl_on
if i<(5-r):
sr = self.sr_off
else:
sr = self.sr_on
s+=self.s_border+sl+self.s_point+sr+self.s_point
s+=self.s_border+sl+self.s_point+sr+self.s_point
s+=self.s_line
image = wx.EmptyImage(16,16)
image.SetData(s)
bmp = image.ConvertToBitmap()
bmp.SetMask(wx.Mask(bmp, wx.WHITE)) #sets the transparency colour to white
icon = wx.EmptyIcon()
icon.CopyFromBitmap(bmp)
return icon
I add to update the script by adding import wx.adv
and by replacing the 2 wx.TaskBarIcon
by wx.adv.TaskBarIcon
.
I am on Windows 10 with Python 3.6
python python-3.x wxpython system-tray
add a comment
|
I am trying to get a dynamic text displayed in the system tray (this will be 2 numbers (from 1 to 100) changing every 2 minutes).
I found this script as a starting point (but I am not commited to it!).
But I get this error :
TypeError: Image.SetData(): arguments did not match any overloaded call:
overload 1: argument 1 has unexpected type 'str'
overload 2: argument 1 has unexpected type 'str'
OnInit returned false, exiting...
The relevant part of the code is:
def Get(self,l,r):
s=""+self.s_line
for i in range(5):
if i<(5-l):
sl = self.sl_off
else:
sl = self.sl_on
if i<(5-r):
sr = self.sr_off
else:
sr = self.sr_on
s+=self.s_border+sl+self.s_point+sr+self.s_point
s+=self.s_border+sl+self.s_point+sr+self.s_point
s+=self.s_line
image = wx.EmptyImage(16,16)
image.SetData(s)
bmp = image.ConvertToBitmap()
bmp.SetMask(wx.Mask(bmp, wx.WHITE)) #sets the transparency colour to white
icon = wx.EmptyIcon()
icon.CopyFromBitmap(bmp)
return icon
I add to update the script by adding import wx.adv
and by replacing the 2 wx.TaskBarIcon
by wx.adv.TaskBarIcon
.
I am on Windows 10 with Python 3.6
python python-3.x wxpython system-tray
add a comment
|
I am trying to get a dynamic text displayed in the system tray (this will be 2 numbers (from 1 to 100) changing every 2 minutes).
I found this script as a starting point (but I am not commited to it!).
But I get this error :
TypeError: Image.SetData(): arguments did not match any overloaded call:
overload 1: argument 1 has unexpected type 'str'
overload 2: argument 1 has unexpected type 'str'
OnInit returned false, exiting...
The relevant part of the code is:
def Get(self,l,r):
s=""+self.s_line
for i in range(5):
if i<(5-l):
sl = self.sl_off
else:
sl = self.sl_on
if i<(5-r):
sr = self.sr_off
else:
sr = self.sr_on
s+=self.s_border+sl+self.s_point+sr+self.s_point
s+=self.s_border+sl+self.s_point+sr+self.s_point
s+=self.s_line
image = wx.EmptyImage(16,16)
image.SetData(s)
bmp = image.ConvertToBitmap()
bmp.SetMask(wx.Mask(bmp, wx.WHITE)) #sets the transparency colour to white
icon = wx.EmptyIcon()
icon.CopyFromBitmap(bmp)
return icon
I add to update the script by adding import wx.adv
and by replacing the 2 wx.TaskBarIcon
by wx.adv.TaskBarIcon
.
I am on Windows 10 with Python 3.6
python python-3.x wxpython system-tray
I am trying to get a dynamic text displayed in the system tray (this will be 2 numbers (from 1 to 100) changing every 2 minutes).
I found this script as a starting point (but I am not commited to it!).
But I get this error :
TypeError: Image.SetData(): arguments did not match any overloaded call:
overload 1: argument 1 has unexpected type 'str'
overload 2: argument 1 has unexpected type 'str'
OnInit returned false, exiting...
The relevant part of the code is:
def Get(self,l,r):
s=""+self.s_line
for i in range(5):
if i<(5-l):
sl = self.sl_off
else:
sl = self.sl_on
if i<(5-r):
sr = self.sr_off
else:
sr = self.sr_on
s+=self.s_border+sl+self.s_point+sr+self.s_point
s+=self.s_border+sl+self.s_point+sr+self.s_point
s+=self.s_line
image = wx.EmptyImage(16,16)
image.SetData(s)
bmp = image.ConvertToBitmap()
bmp.SetMask(wx.Mask(bmp, wx.WHITE)) #sets the transparency colour to white
icon = wx.EmptyIcon()
icon.CopyFromBitmap(bmp)
return icon
I add to update the script by adding import wx.adv
and by replacing the 2 wx.TaskBarIcon
by wx.adv.TaskBarIcon
.
I am on Windows 10 with Python 3.6
python python-3.x wxpython system-tray
python python-3.x wxpython system-tray
edited Mar 27 at 16:08
MagTun
asked Mar 27 at 15:34
MagTunMagTun
2,4662 gold badges27 silver badges51 bronze badges
2,4662 gold badges27 silver badges51 bronze badges
add a comment
|
add a comment
|
2 Answers
2
active
oldest
votes
I found another way using Pillow
and infi.systray
# text to image : Pillow (https://pillow.readthedocs.io/en/latest/handbook/tutorial.html - simple code sample: https://code-maven.com/create-images-with-python-pil-pillow)
# icon in systray : infi.systray (https://github.com/Infinidat/infi.systray and https://stackoverflow.com/a/54082417/3154274)
# inspired by https://www.reddit.com/r/learnpython/comments/a7utd7/pystray_python_system_tray_icon_app/
# install PIL : pip install Pillow
# install infi.systray : pip install infi.systray
from infi.systray import SysTrayIcon
from PIL import Image, ImageDraw,ImageFont
import time
image= "pil_text.ico"
n=1
while True:
# create image
img = Image.new('RGBA', (50, 50), color = (255, 255, 255, 90)) # color background = white with transparency
d = ImageDraw.Draw(img)
d.rectangle([(0, 40), (50, 50)], fill=(39, 112, 229), outline=None) # color = blue
#add text to the image
font_type = ImageFont.truetype("arial.ttf", 25)
a= n*10
b = n*20
d.text((0,0), f"anb", fill=(255,255,0), font = font_type)
img.save(image)
# display image in systray
if n==1:
systray = SysTrayIcon(image, "Systray")
systray.start()
else:
systray.update(icon=image)
time.sleep(5)
n+=1
systray.shutdown()
add a comment
|
The following uses the current minute and second as a replacement for your line numbers but can be easily adapted.
import wx
import wx.adv
import datetime
from wx.lib.embeddedimage import PyEmbeddedImage
#
# A white box 28x28 pixels
#
toggletest = PyEmbeddedImage(
b'iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAIAAAD9b0jDAAAACXBIWXMAAAsTAAALEwEAmpwY'
b'AAAAB3RJTUUH4wMfCgElTFaeRQAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJ'
b'TVBkLmUHAAAAKElEQVRIx2P8//8/A7UBEwMNwKiho4aOGjpq6Kiho4aOGjpq6OAzFADRYgM1'
b'8cIRtgAAAABJRU5ErkJggg==')
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
self.toggle = 0
wx.adv.TaskBarIcon.__init__(self)
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.OnToggle)
self.font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
self.font.SetPointSize(8)
self.OnSetIcon(self.NewIcon())
def CreatePopupMenu(self):
menu = wx.Menu()
togglem = wx.MenuItem(menu, wx.NewId(), 'Toggle Icon')
menu.Bind(wx.EVT_MENU, self.OnToggle, id=togglem.GetId())
menu.Append(togglem)
menu.AppendSeparator()
flashm = wx.MenuItem(menu, wx.NewId(), 'Flashing Icon')
menu.Bind(wx.EVT_MENU, self.OnTimer, id=flashm.GetId())
menu.Append(flashm)
menu.AppendSeparator()
quitm = wx.MenuItem(menu, wx.NewId(), 'Quit')
menu.Bind(wx.EVT_MENU, self.OnQuit, id=quitm.GetId())
menu.Append(quitm)
return menu
def NewIcon(self):
bitmap = wx.Bitmap(toggletest.Bitmap)
dc = wx.MemoryDC(bitmap)
# Use current time as text, for want of something useful
now = datetime.datetime.today()
text = str(now.minute)+"n"+str(now.second)
dc.SetFont(self.font)
dc.DrawText(text, 2, 2)
del dc
return bitmap
def OnSetIcon(self, bitmap):
icon = wx.Icon()
icon.CopyFromBitmap(bitmap)
self.SetIcon(icon)
def OnToggle(self, event):
bitmap = self.NewIcon()
self.OnSetIcon(bitmap)
def OnTimer(self,event):
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnUseTimer)
self.timer.Start(1000)
def OnUseTimer(self,event):
self.OnToggle(None)
def OnQuit(self, event):
self.RemoveIcon()
wx.CallAfter(self.Destroy)
self.frame.Close()
if __name__ == '__main__':
app = wx.App()
frame=wx.Frame(None)
TaskBarIcon(frame)
app.MainLoop()
The result is the white box below with 22 above 33
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381039%2fdynamic-system-tray-text-python-3%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I found another way using Pillow
and infi.systray
# text to image : Pillow (https://pillow.readthedocs.io/en/latest/handbook/tutorial.html - simple code sample: https://code-maven.com/create-images-with-python-pil-pillow)
# icon in systray : infi.systray (https://github.com/Infinidat/infi.systray and https://stackoverflow.com/a/54082417/3154274)
# inspired by https://www.reddit.com/r/learnpython/comments/a7utd7/pystray_python_system_tray_icon_app/
# install PIL : pip install Pillow
# install infi.systray : pip install infi.systray
from infi.systray import SysTrayIcon
from PIL import Image, ImageDraw,ImageFont
import time
image= "pil_text.ico"
n=1
while True:
# create image
img = Image.new('RGBA', (50, 50), color = (255, 255, 255, 90)) # color background = white with transparency
d = ImageDraw.Draw(img)
d.rectangle([(0, 40), (50, 50)], fill=(39, 112, 229), outline=None) # color = blue
#add text to the image
font_type = ImageFont.truetype("arial.ttf", 25)
a= n*10
b = n*20
d.text((0,0), f"anb", fill=(255,255,0), font = font_type)
img.save(image)
# display image in systray
if n==1:
systray = SysTrayIcon(image, "Systray")
systray.start()
else:
systray.update(icon=image)
time.sleep(5)
n+=1
systray.shutdown()
add a comment
|
I found another way using Pillow
and infi.systray
# text to image : Pillow (https://pillow.readthedocs.io/en/latest/handbook/tutorial.html - simple code sample: https://code-maven.com/create-images-with-python-pil-pillow)
# icon in systray : infi.systray (https://github.com/Infinidat/infi.systray and https://stackoverflow.com/a/54082417/3154274)
# inspired by https://www.reddit.com/r/learnpython/comments/a7utd7/pystray_python_system_tray_icon_app/
# install PIL : pip install Pillow
# install infi.systray : pip install infi.systray
from infi.systray import SysTrayIcon
from PIL import Image, ImageDraw,ImageFont
import time
image= "pil_text.ico"
n=1
while True:
# create image
img = Image.new('RGBA', (50, 50), color = (255, 255, 255, 90)) # color background = white with transparency
d = ImageDraw.Draw(img)
d.rectangle([(0, 40), (50, 50)], fill=(39, 112, 229), outline=None) # color = blue
#add text to the image
font_type = ImageFont.truetype("arial.ttf", 25)
a= n*10
b = n*20
d.text((0,0), f"anb", fill=(255,255,0), font = font_type)
img.save(image)
# display image in systray
if n==1:
systray = SysTrayIcon(image, "Systray")
systray.start()
else:
systray.update(icon=image)
time.sleep(5)
n+=1
systray.shutdown()
add a comment
|
I found another way using Pillow
and infi.systray
# text to image : Pillow (https://pillow.readthedocs.io/en/latest/handbook/tutorial.html - simple code sample: https://code-maven.com/create-images-with-python-pil-pillow)
# icon in systray : infi.systray (https://github.com/Infinidat/infi.systray and https://stackoverflow.com/a/54082417/3154274)
# inspired by https://www.reddit.com/r/learnpython/comments/a7utd7/pystray_python_system_tray_icon_app/
# install PIL : pip install Pillow
# install infi.systray : pip install infi.systray
from infi.systray import SysTrayIcon
from PIL import Image, ImageDraw,ImageFont
import time
image= "pil_text.ico"
n=1
while True:
# create image
img = Image.new('RGBA', (50, 50), color = (255, 255, 255, 90)) # color background = white with transparency
d = ImageDraw.Draw(img)
d.rectangle([(0, 40), (50, 50)], fill=(39, 112, 229), outline=None) # color = blue
#add text to the image
font_type = ImageFont.truetype("arial.ttf", 25)
a= n*10
b = n*20
d.text((0,0), f"anb", fill=(255,255,0), font = font_type)
img.save(image)
# display image in systray
if n==1:
systray = SysTrayIcon(image, "Systray")
systray.start()
else:
systray.update(icon=image)
time.sleep(5)
n+=1
systray.shutdown()
I found another way using Pillow
and infi.systray
# text to image : Pillow (https://pillow.readthedocs.io/en/latest/handbook/tutorial.html - simple code sample: https://code-maven.com/create-images-with-python-pil-pillow)
# icon in systray : infi.systray (https://github.com/Infinidat/infi.systray and https://stackoverflow.com/a/54082417/3154274)
# inspired by https://www.reddit.com/r/learnpython/comments/a7utd7/pystray_python_system_tray_icon_app/
# install PIL : pip install Pillow
# install infi.systray : pip install infi.systray
from infi.systray import SysTrayIcon
from PIL import Image, ImageDraw,ImageFont
import time
image= "pil_text.ico"
n=1
while True:
# create image
img = Image.new('RGBA', (50, 50), color = (255, 255, 255, 90)) # color background = white with transparency
d = ImageDraw.Draw(img)
d.rectangle([(0, 40), (50, 50)], fill=(39, 112, 229), outline=None) # color = blue
#add text to the image
font_type = ImageFont.truetype("arial.ttf", 25)
a= n*10
b = n*20
d.text((0,0), f"anb", fill=(255,255,0), font = font_type)
img.save(image)
# display image in systray
if n==1:
systray = SysTrayIcon(image, "Systray")
systray.start()
else:
systray.update(icon=image)
time.sleep(5)
n+=1
systray.shutdown()
edited Mar 28 at 16:12
answered Mar 27 at 16:07
MagTunMagTun
2,4662 gold badges27 silver badges51 bronze badges
2,4662 gold badges27 silver badges51 bronze badges
add a comment
|
add a comment
|
The following uses the current minute and second as a replacement for your line numbers but can be easily adapted.
import wx
import wx.adv
import datetime
from wx.lib.embeddedimage import PyEmbeddedImage
#
# A white box 28x28 pixels
#
toggletest = PyEmbeddedImage(
b'iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAIAAAD9b0jDAAAACXBIWXMAAAsTAAALEwEAmpwY'
b'AAAAB3RJTUUH4wMfCgElTFaeRQAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJ'
b'TVBkLmUHAAAAKElEQVRIx2P8//8/A7UBEwMNwKiho4aOGjpq6Kiho4aOGjpq6OAzFADRYgM1'
b'8cIRtgAAAABJRU5ErkJggg==')
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
self.toggle = 0
wx.adv.TaskBarIcon.__init__(self)
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.OnToggle)
self.font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
self.font.SetPointSize(8)
self.OnSetIcon(self.NewIcon())
def CreatePopupMenu(self):
menu = wx.Menu()
togglem = wx.MenuItem(menu, wx.NewId(), 'Toggle Icon')
menu.Bind(wx.EVT_MENU, self.OnToggle, id=togglem.GetId())
menu.Append(togglem)
menu.AppendSeparator()
flashm = wx.MenuItem(menu, wx.NewId(), 'Flashing Icon')
menu.Bind(wx.EVT_MENU, self.OnTimer, id=flashm.GetId())
menu.Append(flashm)
menu.AppendSeparator()
quitm = wx.MenuItem(menu, wx.NewId(), 'Quit')
menu.Bind(wx.EVT_MENU, self.OnQuit, id=quitm.GetId())
menu.Append(quitm)
return menu
def NewIcon(self):
bitmap = wx.Bitmap(toggletest.Bitmap)
dc = wx.MemoryDC(bitmap)
# Use current time as text, for want of something useful
now = datetime.datetime.today()
text = str(now.minute)+"n"+str(now.second)
dc.SetFont(self.font)
dc.DrawText(text, 2, 2)
del dc
return bitmap
def OnSetIcon(self, bitmap):
icon = wx.Icon()
icon.CopyFromBitmap(bitmap)
self.SetIcon(icon)
def OnToggle(self, event):
bitmap = self.NewIcon()
self.OnSetIcon(bitmap)
def OnTimer(self,event):
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnUseTimer)
self.timer.Start(1000)
def OnUseTimer(self,event):
self.OnToggle(None)
def OnQuit(self, event):
self.RemoveIcon()
wx.CallAfter(self.Destroy)
self.frame.Close()
if __name__ == '__main__':
app = wx.App()
frame=wx.Frame(None)
TaskBarIcon(frame)
app.MainLoop()
The result is the white box below with 22 above 33
add a comment
|
The following uses the current minute and second as a replacement for your line numbers but can be easily adapted.
import wx
import wx.adv
import datetime
from wx.lib.embeddedimage import PyEmbeddedImage
#
# A white box 28x28 pixels
#
toggletest = PyEmbeddedImage(
b'iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAIAAAD9b0jDAAAACXBIWXMAAAsTAAALEwEAmpwY'
b'AAAAB3RJTUUH4wMfCgElTFaeRQAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJ'
b'TVBkLmUHAAAAKElEQVRIx2P8//8/A7UBEwMNwKiho4aOGjpq6Kiho4aOGjpq6OAzFADRYgM1'
b'8cIRtgAAAABJRU5ErkJggg==')
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
self.toggle = 0
wx.adv.TaskBarIcon.__init__(self)
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.OnToggle)
self.font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
self.font.SetPointSize(8)
self.OnSetIcon(self.NewIcon())
def CreatePopupMenu(self):
menu = wx.Menu()
togglem = wx.MenuItem(menu, wx.NewId(), 'Toggle Icon')
menu.Bind(wx.EVT_MENU, self.OnToggle, id=togglem.GetId())
menu.Append(togglem)
menu.AppendSeparator()
flashm = wx.MenuItem(menu, wx.NewId(), 'Flashing Icon')
menu.Bind(wx.EVT_MENU, self.OnTimer, id=flashm.GetId())
menu.Append(flashm)
menu.AppendSeparator()
quitm = wx.MenuItem(menu, wx.NewId(), 'Quit')
menu.Bind(wx.EVT_MENU, self.OnQuit, id=quitm.GetId())
menu.Append(quitm)
return menu
def NewIcon(self):
bitmap = wx.Bitmap(toggletest.Bitmap)
dc = wx.MemoryDC(bitmap)
# Use current time as text, for want of something useful
now = datetime.datetime.today()
text = str(now.minute)+"n"+str(now.second)
dc.SetFont(self.font)
dc.DrawText(text, 2, 2)
del dc
return bitmap
def OnSetIcon(self, bitmap):
icon = wx.Icon()
icon.CopyFromBitmap(bitmap)
self.SetIcon(icon)
def OnToggle(self, event):
bitmap = self.NewIcon()
self.OnSetIcon(bitmap)
def OnTimer(self,event):
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnUseTimer)
self.timer.Start(1000)
def OnUseTimer(self,event):
self.OnToggle(None)
def OnQuit(self, event):
self.RemoveIcon()
wx.CallAfter(self.Destroy)
self.frame.Close()
if __name__ == '__main__':
app = wx.App()
frame=wx.Frame(None)
TaskBarIcon(frame)
app.MainLoop()
The result is the white box below with 22 above 33
add a comment
|
The following uses the current minute and second as a replacement for your line numbers but can be easily adapted.
import wx
import wx.adv
import datetime
from wx.lib.embeddedimage import PyEmbeddedImage
#
# A white box 28x28 pixels
#
toggletest = PyEmbeddedImage(
b'iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAIAAAD9b0jDAAAACXBIWXMAAAsTAAALEwEAmpwY'
b'AAAAB3RJTUUH4wMfCgElTFaeRQAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJ'
b'TVBkLmUHAAAAKElEQVRIx2P8//8/A7UBEwMNwKiho4aOGjpq6Kiho4aOGjpq6OAzFADRYgM1'
b'8cIRtgAAAABJRU5ErkJggg==')
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
self.toggle = 0
wx.adv.TaskBarIcon.__init__(self)
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.OnToggle)
self.font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
self.font.SetPointSize(8)
self.OnSetIcon(self.NewIcon())
def CreatePopupMenu(self):
menu = wx.Menu()
togglem = wx.MenuItem(menu, wx.NewId(), 'Toggle Icon')
menu.Bind(wx.EVT_MENU, self.OnToggle, id=togglem.GetId())
menu.Append(togglem)
menu.AppendSeparator()
flashm = wx.MenuItem(menu, wx.NewId(), 'Flashing Icon')
menu.Bind(wx.EVT_MENU, self.OnTimer, id=flashm.GetId())
menu.Append(flashm)
menu.AppendSeparator()
quitm = wx.MenuItem(menu, wx.NewId(), 'Quit')
menu.Bind(wx.EVT_MENU, self.OnQuit, id=quitm.GetId())
menu.Append(quitm)
return menu
def NewIcon(self):
bitmap = wx.Bitmap(toggletest.Bitmap)
dc = wx.MemoryDC(bitmap)
# Use current time as text, for want of something useful
now = datetime.datetime.today()
text = str(now.minute)+"n"+str(now.second)
dc.SetFont(self.font)
dc.DrawText(text, 2, 2)
del dc
return bitmap
def OnSetIcon(self, bitmap):
icon = wx.Icon()
icon.CopyFromBitmap(bitmap)
self.SetIcon(icon)
def OnToggle(self, event):
bitmap = self.NewIcon()
self.OnSetIcon(bitmap)
def OnTimer(self,event):
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnUseTimer)
self.timer.Start(1000)
def OnUseTimer(self,event):
self.OnToggle(None)
def OnQuit(self, event):
self.RemoveIcon()
wx.CallAfter(self.Destroy)
self.frame.Close()
if __name__ == '__main__':
app = wx.App()
frame=wx.Frame(None)
TaskBarIcon(frame)
app.MainLoop()
The result is the white box below with 22 above 33
The following uses the current minute and second as a replacement for your line numbers but can be easily adapted.
import wx
import wx.adv
import datetime
from wx.lib.embeddedimage import PyEmbeddedImage
#
# A white box 28x28 pixels
#
toggletest = PyEmbeddedImage(
b'iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAIAAAD9b0jDAAAACXBIWXMAAAsTAAALEwEAmpwY'
b'AAAAB3RJTUUH4wMfCgElTFaeRQAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJ'
b'TVBkLmUHAAAAKElEQVRIx2P8//8/A7UBEwMNwKiho4aOGjpq6Kiho4aOGjpq6OAzFADRYgM1'
b'8cIRtgAAAABJRU5ErkJggg==')
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
self.toggle = 0
wx.adv.TaskBarIcon.__init__(self)
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.OnToggle)
self.font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
self.font.SetPointSize(8)
self.OnSetIcon(self.NewIcon())
def CreatePopupMenu(self):
menu = wx.Menu()
togglem = wx.MenuItem(menu, wx.NewId(), 'Toggle Icon')
menu.Bind(wx.EVT_MENU, self.OnToggle, id=togglem.GetId())
menu.Append(togglem)
menu.AppendSeparator()
flashm = wx.MenuItem(menu, wx.NewId(), 'Flashing Icon')
menu.Bind(wx.EVT_MENU, self.OnTimer, id=flashm.GetId())
menu.Append(flashm)
menu.AppendSeparator()
quitm = wx.MenuItem(menu, wx.NewId(), 'Quit')
menu.Bind(wx.EVT_MENU, self.OnQuit, id=quitm.GetId())
menu.Append(quitm)
return menu
def NewIcon(self):
bitmap = wx.Bitmap(toggletest.Bitmap)
dc = wx.MemoryDC(bitmap)
# Use current time as text, for want of something useful
now = datetime.datetime.today()
text = str(now.minute)+"n"+str(now.second)
dc.SetFont(self.font)
dc.DrawText(text, 2, 2)
del dc
return bitmap
def OnSetIcon(self, bitmap):
icon = wx.Icon()
icon.CopyFromBitmap(bitmap)
self.SetIcon(icon)
def OnToggle(self, event):
bitmap = self.NewIcon()
self.OnSetIcon(bitmap)
def OnTimer(self,event):
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnUseTimer)
self.timer.Start(1000)
def OnUseTimer(self,event):
self.OnToggle(None)
def OnQuit(self, event):
self.RemoveIcon()
wx.CallAfter(self.Destroy)
self.frame.Close()
if __name__ == '__main__':
app = wx.App()
frame=wx.Frame(None)
TaskBarIcon(frame)
app.MainLoop()
The result is the white box below with 22 above 33
answered Mar 31 at 11:01
Rolf of SaxonyRolf of Saxony
11.9k2 gold badges23 silver badges40 bronze badges
11.9k2 gold badges23 silver badges40 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381039%2fdynamic-system-tray-text-python-3%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown