Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pystray help
#1
I'm looking into using Pystray because it is cross platform.
I find the docs pretty confusing.

I have the basic system tray up and running with an icon and menu that calls functions.
Now I need separator bars.

Separators are supported but there is no code example anywhere
that I can find.

I think a separator is used like this,but I'm not 100% sure:

from pystray import MenuItem as item
import pystray
from PIL import Image

def action():
    pass

def exit_prg():
    icon.stop()

SEPARATOR = item('- - - -', None)
image = Image.open("corona.png")

menu = (item('Help', action),
        item('About', action),
        SEPARATOR(), None)
        item('Quit', exit_prg)
        )

icon = pystray.Icon("huh?", image, "Pysystray Demo", menu)
icon.run()
I get a "unexpected indent" error with this code, now I am totally lost.

The only mention in the docs about separator is:
here

Can anyone point me in right direction please?
Reply
#2
doc: https://pystray.readthedocs.io/en/latest...stray.Menu
example: https://pystray.readthedocs.io/en/latest....html#Menu
Reply
#3
I should of said no useable code examples anywhere,
I should of linked those two that yo found, as I did find them too,
but as I said Lars, I couldn't turn that info
into useable code.

Do you know why my code example doesn't work?
The code works if you rem out the SEPARATOR(), None) line.

(Apr-27-2020, 05:18 AM)Larz60+ Wrote: doc: https://pystray.readthedocs.io/en/latest...stray.Menu
example: https://pystray.readthedocs.io/en/latest....html#Menu
Reply
#4
First of all I must confess In know nothing about Pystray, but perhaps I spotted some syntactic anomalies.
(Apr-27-2020, 09:05 AM)steve_shambles Wrote: The code works if you rem out the SEPARATOR(), None) line.

SEPARATOR = item('- - - -', None)
image = Image.open("corona.png")
 
menu = (item('Help', action),
        item('About', action),
        SEPARATOR(), None)
        item('Quit', exit_prg)
        )
The line with "SEPARATOR":
  1. has a spurious close-bracket,
  2. misses a comma
  3. has a spurious "None"
As you defined SEPARATOR, one may also leave this out and write directly:
menu = (item('Help', action),
        item('About', action),
        item('- - - -', None),
        item('Quit', exit_prg)
        )
This looks like syntactically correct.
Reply
#5
Thanks for your reply Ibreeden.
I tried your version, it does get rid of the syntax error
but the separator simply comes out as the dashes
so I am pretty sure the line must be something along the lines of:

SEPARATOR = item('- - - -', None)
image = Image.open("corona.png")

menu = (item('Help', action),
        item('About', action),
        item (SEPARATOR, None),
        item('Quit', exit_prg)
       )
icon = pystray.Icon("huh?", image, "Pysystray Demo", menu)
icon.run()
But this gives:
TypeError: 'NoneType' object is not callable.

I think I have tried every permutation of that item to no vail so far.


(Apr-27-2020, 09:47 AM)ibreeden Wrote: First of all I must confess In know nothing about Pystray, but perhaps I spotted some syntactic anomalies.
(Apr-27-2020, 09:05 AM)steve_shambles Wrote: The code works if you rem out the SEPARATOR(), None) line.

SEPARATOR = item('- - - -', None)
image = Image.open("corona.png")
 
menu = (item('Help', action),
        item('About', action),
        SEPARATOR(), None)
        item('Quit', exit_prg)
        )
The line with "SEPARATOR":
  1. has a spurious close-bracket,
  2. misses a comma
  3. has a spurious "None"
As you defined SEPARATOR, one may also leave this out and write directly:
menu = (item('Help', action),
        item('About', action),
        item('- - - -', None),
        item('Quit', exit_prg)
        )
This looks like syntactically correct.

I tried mixing up the SEPARATOR definition line and got lucky guys:
This code works and inserts a proper separator bar:

SEPARATOR = item('- - - -', action)
image = Image.open("corona.png")

menu = (item('Help', action),
        item('About', action),
        item(SEPARATOR, action),
        item('Quit', exit_prg)
       )
icon = pystray.Icon("huh?", image, "Pysystray Demo", menu)
icon.run()
Thanks for help.
Solved.
Reply
#6
Duh! But this code does not work on Linux Mint, which was the whole
point of using Pystray.

I give up.There's only so much time one can give to a problem,
moving on.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020