0

I wrote a code that allows to click inside an entry widget and activate a function that returns the mouse coordinates if clicked on a figure. The problem is that I want to click somewhere else than the figure or entry widget to deactivate the function but I don't know how to do that.

What I tried so far is binding (with bind) a callback function that deactivates the select_marker function to master (what obviously makes no sense) or to a certain Frame (didn't help). I couldn't find any solution by browsing SO or the web.

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np

class Application():

    def __init__(self, master):

        self.master = master
        master.iconify

        self.entry_frame = Tk.Frame(master)
        self.entry_frame.pack(side=Tk.TOP, fill=Tk.BOTH, expand=0)

        self.m1_label = Tk.Label(self.entry_frame, text='Mouse Coordinates: ')
        self.m1_label.pack(side=Tk.LEFT)
        self.m1_entry = Tk.Entry(self.entry_frame, width=10)
        self.m1_entry.pack(side=Tk.LEFT)
        self.m1_entry.bind('<Button-1>', lambda e:self.callback(1))

        self.image_frame = Tk.Frame(master)
        self.image_frame.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
        self.image_frame.bind('<Button-1>', lambda e:self.callback(0))  # something like this

        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        self.ax.set_aspect('equal')

        self.canvas = FigureCanvasTkAgg(self.fig, self.image_frame)
        self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
        self.canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)  

        self.widget_active = 0
        self.fig.canvas.mpl_connect('button_press_event', self.select_marker)


    def callback(self, state):
        self.widget_active = state
        print(state)

    def select_marker(self, event):
        if self.widget_active == 1:
            if event.button == 1:
                x = np.round(event.xdata,2)
                y = np.round(event.ydata,2)
                print(x,y)
                self.m1_entry.delete(0,'end')
                self.m1_entry.insert(0,(str(x)+', '+str(y)))
            else:
                pass
        if self.widget_active == 0:
            pass

root = Tk.Tk()
Application(root)
root.mainloop()

I would really appreciate if someone knows a way to get a callback if clicked somewhere except the entry widget or the figure. Thanks a lot!

Philipp
  • 323
  • 4
  • 19
  • You could write a call back for a click anywhere and have it do nothing if the click occurred inside the figure. Or maybe onblur event for the figure. – lurker Feb 14 '19 at 11:31
  • That's exactly what I am trying to do but as I am a beginner I don't know which python methods to use. Onblur must be something like the opposite of focus in Python but unfortunately I don't understand how it works. – Philipp Feb 14 '19 at 11:49
  • Try this [identify-object-on-click](https://stackoverflow.com/questions/38982313/python-tkinter-identify-object-on-click) – stovfl Feb 14 '19 at 11:59
  • @Philipp I don't know if Python Tkinter has onblur which case use the other method I suggested. – lurker Feb 14 '19 at 12:28
  • @stovfl `FigureCanvasTkAgg` seems not to be compatible with `find_closest` only with `Tk.Canvas` but the idea goes in the same direction as @lurker suggestion. I just have to figure out how to implement it. – Philipp Feb 14 '19 at 12:40
  • *"not to be compatible with find_closest"*: Try to implement yourself, does `self.canvas.figure.bbox` return any result? – stovfl Feb 14 '19 at 17:06
  • Does `self.canvas.get_tk_widget().bind('', self.on_click)` work? – stovfl Feb 14 '19 at 18:03
  • `self.canvas.get_tk_widget().bind('', self.on_click)` is pretty helpful! Nevertheless I added an OK button whose callback function sets the focus to the whole application (`self.master.focus_set()`). Not exactly what I wanted but it works without problems. – Philipp Feb 18 '19 at 08:58

0 Answers0