Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd79cdeb

Browse files
committed
Change colour of Tk toolbar icons on dark backgrounds
Also check the background colour of Checkbuttons in the selected state
1 parent5de27aa commitd79cdeb

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

‎lib/matplotlib/backends/_backend_tk.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,24 @@ def _set_image_for_button(self, button):
652652
# Use the high-resolution (48x48 px) icon if it exists and is needed
653653
withImage.open(path_largeif (size>24andpath_large.exists())
654654
elsepath_regular)asim:
655+
colors= ["background","activebackground"]
656+
ifisinstance(button,tk.Checkbutton):
657+
colors.append("selectcolor")
658+
forcolorincolors:
659+
ifbutton.cget(color)=="":
660+
continue
661+
# Get the color as an (r, g, b) tuple in the range from 0 to
662+
# 65535, and check if its HSV V is under 50%.
663+
ifmax(button.winfo_rgb(button.cget(color)))<65535/2:
664+
# Change the colour of the icon to be visible against
665+
# the dark background.
666+
foreground= (255/65535)*np.array(
667+
button.winfo_rgb(button.cget("foreground")))
668+
image_data=np.asarray(im)
669+
black_mask= (image_data[..., :3]==0).all(axis=-1)
670+
image_data[black_mask, :3]=foreground
671+
im=Image.fromarray(image_data,mode="RGBA")
672+
break
655673
image=ImageTk.PhotoImage(im.resize((size,size)),master=self)
656674
button.configure(image=image,height='18p',width='18p')
657675
button._ntimage=image# Prevent garbage collection.

‎lib/matplotlib/tests/test_backend_tk.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,42 @@ class Toolbar(NavigationToolbar2Tk):
185185
print("success")
186186
Toolbar(fig.canvas,fig.canvas.manager.window)# This should not raise.
187187
print("success")
188+
189+
190+
@pytest.mark.backend('TkAgg',skip_on_importerror=True)
191+
@_isolated_tk_test(success_count=2)
192+
deftest_embedding():# pragma: no cover
193+
importtkinterastk
194+
frommatplotlib.backends.backend_tkaggimport (
195+
FigureCanvasTkAgg,NavigationToolbar2Tk)
196+
frommatplotlib.backend_basesimportkey_press_handler
197+
frommatplotlib.figureimportFigure
198+
199+
root=tk.Tk()
200+
201+
deftest_figure(master):
202+
fig=Figure()
203+
ax=fig.add_subplot()
204+
ax.plot([1,2,3])
205+
206+
canvas=FigureCanvasTkAgg(fig,master=master)
207+
canvas.draw()
208+
canvas.mpl_connect("key_press_event",key_press_handler)
209+
canvas.get_tk_widget().pack(expand=True,fill="both")
210+
211+
toolbar=NavigationToolbar2Tk(canvas,master,pack_toolbar=False)
212+
toolbar.pack(expand=True,fill="x")
213+
214+
canvas.get_tk_widget().forget()
215+
toolbar.forget()
216+
217+
test_figure(root)
218+
print("success")
219+
220+
# Test with a dark button color. Doesn't actually check whether the icon
221+
# color becomes lighter, just that the code doesn't break.
222+
223+
root.tk_setPalette(background="sky blue",selectColor="midnight blue",
224+
foreground="white")
225+
test_figure(root)
226+
print("success")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp