8000 Switch tk pan/zoom to use togglable buttons. by anntzer · Pull Request #17102 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Switch tk pan/zoom to use togglable buttons. #17102

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

Merged
merged 1 commit into from
Apr 20, 2020

Conversation

anntzer
Copy link
Contributor
@anntzer anntzer commented Apr 11, 2020

... consistently with other backends.

The code for syncing button state is similar to the one for gtk3.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@anntzer anntzer added this to the v3.3.0 milestone Apr 11, 2020
@timhoffm
Copy link
Member
timhoffm commented Apr 12, 2020

Seems to work, but the toggle state is hardly visible for me:

Nothing toggled:
image

Pan toggled:
image

Is there a way to make this more obvious?

Also, if you look closely, the toogle buttons have a smaller edge than the regular buttons:

[toggled pan] [untoggled zoom] [regular button]

grafik

which makes the whole toolbar look a bit uneven

image

@anntzer
Copy link
Contributor Author
anntzer commented Apr 12, 2020

I think you'd need ttk (https://docs.python.org/3/library/tkinter.ttk.html#module-tkinter.ttk) for that, not sure how widely available it is.

diff --git i/lib/matplotlib/backends/_backend_tk.py w/lib/matplotlib/backends/_backend_tk.py
index d8bf524f2..065b78766 100644
--- i/lib/matplotlib/backends/_backend_tk.py
+++ w/lib/matplotlib/backends/_backend_tk.py
@@ -4,6 +4,7 @@ import math
 import os.path
 import sys
 import tkinter as tk
+from tkinter import ttk
 from tkinter.simpledialog import SimpleDialog
 import tkinter.filedialog
 import tkinter.messagebox
@@ -539,8 +540,8 @@ class NavigationToolbar2Tk(NavigationToolbar2, tk.Frame):
         else:
             im = None
         if not toggle:
-            b = tk.Button(master=self, text=text, padx=2, pady=2, image=im,
-                          command=command)
+            b = ttk.Button(master=self, text=text, padding=0, image=im,
+                           command=command)
         else:
             # There is a bug in tkinter included in some python 3.6 versions
             # that without this variable, produces a "visual" toggling of
@@ -548,10 +549,12 @@ class NavigationToolbar2Tk(NavigationToolbar2, tk.Frame):
             # https://bugs.python.org/issue29402
             # https://bugs.python.org/issue25684
             var = tk.IntVar()
-            b = tk.Checkbutton(master=self, text=text, padx=2, pady=2,
-                               image=im, indicatoron=False,
-                               command=command,
-                               variable=var)
+            # Matplotlib.Toolbutton "inherits" from Toolbutton.
+            ttk.Style().configure("Matplotlib.Toolbutton", relief="raised")
+            b = ttk.Checkbutton(master=self, text=text, padding=2,
+                                image=im, style="Matplotlib.Toolbutton",
+                                command=command,
+                                variable=var)
             b.var = var
         b._ntimage = im
         b.pack(side=tk.LEFT)

works nicely here. (Not sure things work well on a dark theme though.)

@timhoffm
Copy link
Member

Works reasonably on dark mode:
image

Tooltips are not readable, but that was the same with tk.Button.

@@ -717,7 +754,8 @@ def __init__(self, toolmanager, window):
def add_toolitem(
self, name, group, position, image_file, description, toggle):
frame = self._get_groupframe(group)
button = self._Button(name, image_file, toggle, frame)
button = NavigationToolbar2Tk._Button(self, name, image_file, toggle,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well make this an un-bound top level function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been redoing most of the MEP22 implementation to redirect to NavigationToolbar2 everywhere instead of having duplicate implementations; I'd rather keep things there and if we ever switch to MEP22-by-default (which I would rather not, I have deep misgivings about that API design discussed at length elsewhere, but no need to relitigate that here), move everything to the MEP22 classes and point NavigationToolbar2 to them. IOW: I don't think it's worth turning this into a free function "just because" MEP22 also uses it.

else:
im = None
if not toggle:
b = tk.Button(master=self, text=text, padx=2, pady=2, image=im,
Copy link
Member
@timhoffm timhoffm Apr 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that padx/y do not have any effect on images with buttons:
https://www.tcl.tk/man/tcl8.6/TkCmd/options.htm#M-padx

Most widgets only use this option for padding text: if they are displaying a bitmap or image, then they usually ignore padding options.

These options could be removed (also below for Checkbutton).

@timhoffm
Copy link
Member
timhoffm commented Apr 19, 2020

I'm a bit reluctant of using ttk. It changes at least the visual impression and maybe would also be an API change. Therefore I tried to achieve same-size buttons in pure tk by tuning options.

The border for Checkbuttons is drawn differently and seems to be smaller than for regular buttons (see screenshots).

I've played around with Checkbutton borderwidth but thid did not give satisfying results. The best solution I found for getting equal-sized buttons is manually increasing the image size:

            # workaround for making the Checkbutton equally large to a Button
            im = tk.PhotoImage(master=self, file=image_file,
                               width=im.width() + 2, height=im.height() + 2)

grafik

But with the large background one can see that the image is rendered in the top left.

grafik

Probably the visually best solution would be to have a 1px transparent border around the original image. I'm not an Tk expert and the docs are rather cryptic, so I did not manage this. If anybody knows please let me know.

@anntzer
Copy link
Contributor Author
anntzer commented Apr 19, 2020

Got rid of the padding. Didn't play more with the styling.

@timhoffm
Copy link
Member
timhoffm commented Apr 19, 2020

Did you already see my last message on styling?

@anntzer
Copy link
Contributor Author
anntzer commented Apr 19, 2020

Yes. I didn't do any of it but can manually add the two pixels to the size if you prefer (I personally think having checkbuttons, even with a small discrepancy in button size (tbh I didn't even notice it before you mentioned it) is already an improvement).

Copy link
Member
@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditional on CI. (seems like a crossed PR issue).

Toggle buttons are an improvement. Improving the style can also be done later if you don't want to dig into it.

Copy link
Member
@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditional on CI. (seems like a crossed PR issue).

Toggle buttons are an improvement. Improving the style can also be done later if you don't want to dig into it.

... consistently with other backends.

The code for syncing button state is similar to the one for gtk3.
@anntzer
Copy link
Contributor Author
anntzer commented Apr 20, 2020

good to go?

@QuLogic QuLogic merged commit 3a2c8f3 into matplotlib:master Apr 20, 2020
@anntzer anntzer deleted the tkactions branch April 20, 2020 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0