FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Lib/tkinter/systray.py at main · python/cpython · GitHub
python
cpython
Repository navigation
Code
Issues
5k+
(5k+)
Pull requests
2.7k
(2.7k)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Lib
/
tkinter
/
systray.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
130 lines (109 loc) · 4.81 KB
Breadcrumbs
cpython
/
Lib
/
tkinter
/
systray.py
Copy path
File metadata and controls
130 lines (109 loc) · 4.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""Interface to the system tray icon and desktop notifications.
The SysTrayIcon class gives access to the "tk systray" command and
the notify() function gives access to the "tk sysnotify" command.
They require Tk 8.7/9.0 or newer.
Only one system tray icon is supported per Tcl interpreter.
On Windows, sending a notification requires that the system tray icon
has been created first; the icon is also displayed in the
notification.
"""
import
tkinter
__all__
=
[
"SysTrayIcon"
,
"notify"
]
class
SysTrayIcon
:
"""The system tray icon.
Only one system tray icon is supported per Tcl interpreter.
With exists false (the default) a new icon is created, and creating
a second one raises TclError. With exists true this refers to the
already-existing icon instead of creating one.
Supported configuration options are:
image: the image displayed in the system tray (required when
creating an icon; it must be a photo image on Windows)
text: the text displayed in the tooltip of the icon
button1: a callback that is called when the icon is clicked
with the left mouse button
button3: a callback that is called when the icon is clicked
with the right mouse button
"""
def
__init__
(
self
,
master
=
None
,
*
,
exists
=
False
,
**
options
):
if
master
is
None
:
master
=
tkinter
.
_get_default_root
(
'use the system tray icon'
)
self
.
master
=
master
self
.
_command_names
=
{}
if
exists
:
# Refer to the already-existing icon, reconfiguring it if
# any options are given.
if
options
:
self
.
_call
(
'configure'
,
options
)
else
:
if
options
.
get
(
'image'
)
is
None
:
raise
TypeError
(
"the 'image' argument is required to create an icon"
)
self
.
_call
(
'create'
,
options
)
def
_call
(
self
,
subcommand
,
cnf
):
# Call "tk systray" with the given options, registering and
# unregistering the callback commands as needed.
master
=
self
.
master
cnf
=
dict
(
cnf
)
new_names
=
{}
for
key
in
(
'button1'
,
'button3'
):
if
key
in
cnf
:
command
=
cnf
[
key
]
if
command
is
None
:
cnf
[
key
]
=
''
elif
callable
(
command
):
new_names
[
key
]
=
cnf
[
key
]
=
master
.
_register
(
command
)
try
:
master
.
tk
.
call
(
'tk'
,
'systray'
,
subcommand
,
*
master
.
_options
(
cnf
))
except
tkinter
.
TclError
:
for
name
in
new_names
.
values
():
master
.
deletecommand
(
name
)
raise
for
key
in
(
'button1'
,
'button3'
):
if
key
in
cnf
:
old_name
=
self
.
_command_names
.
pop
(
key
,
None
)
if
old_name
is
not
None
:
master
.
deletecommand
(
old_name
)
if
key
in
new_names
:
self
.
_command_names
[
key
]
=
new_names
[
key
]
def
configure
(
self
,
cnf
=
None
,
**
kw
):
"""Query or modify the options of the system tray icon.
With no arguments, return a dict of all option values. With a
string argument, return the value of that option. Otherwise,
set the given options.
"""
if
kw
:
cnf
=
tkinter
.
_cnfmerge
((
cnf
,
kw
))
elif
cnf
:
cnf
=
tkinter
.
_cnfmerge
(
cnf
)
tk
=
self
.
master
.
tk
if
cnf
is
None
:
items
=
tk
.
splitlist
(
tk
.
call
(
'tk'
,
'systray'
,
'configure'
))
return
{
items
[
i
][
1
:]:
items
[
i
+
1
]
for
i
in
range
(
0
,
len
(
items
),
2
)}
if
isinstance
(
cnf
,
str
):
return
tk
.
call
(
'tk'
,
'systray'
,
'configure'
,
'-'
+
cnf
)
self
.
_call
(
'configure'
,
cnf
)
config
=
configure
def
cget
(
self
,
option
):
"""Return the value of the given option of the system tray icon."""
return
self
.
master
.
tk
.
call
(
'tk'
,
'systray'
,
'configure'
,
'-'
+
option
)
def
exists
(
self
):
"""Return whether the system tray icon exists."""
tk
=
self
.
master
.
tk
return
tk
.
getboolean
(
tk
.
call
(
'tk'
,
'systray'
,
'exists'
))
def
destroy
(
self
):
"""Destroy the system tray icon."""
self
.
master
.
tk
.
call
(
'tk'
,
'systray'
,
'destroy'
)
for
name
in
self
.
_command_names
.
values
():
self
.
master
.
deletecommand
(
name
)
self
.
_command_names
.
clear
()
def
notify
(
self
,
title
,
message
):
"""Send a desktop notification with the given title and message."""
self
.
master
.
tk
.
call
(
'tk'
,
'sysnotify'
,
title
,
message
)
def
notify
(
title
,
message
,
*
,
master
=
None
):
"""Send a desktop notification with the given title and message.
On Windows, the system tray icon must have been created first.
"""
if
master
is
None
:
master
=
tkinter
.
_get_default_root
(
'send a notification'
)
master
.
tk
.
call
(
'tk'
,
'sysnotify'
,
title
,
message
)
Back
|
FazBrowse Home
|
New Git URL