Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:14:42+00:00 2026-06-14T20:14:42+00:00

I read it is possible to embed a pyplot in tkinter, but I’m having

  • 0

I read it is possible to embed a pyplot in tkinter, but I’m having a problem:

I need to display some frames, among which a pyplot in a frame of the main Tkinter window, and it has to react to my keypresses.

According to http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html
this works:

#!/usr/bin/env python
from Tkinter import *
import matplotlib
matplotlib.use('TkAgg')    
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
import matplotlib.backend_bases       
from matplotlib.figure import Figure    
import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

root = Tk.Tk()
root.wm_title("Embedding in TK")


f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)

a.plot(t,s)

canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

def on_key_event(event):
    print('you pressed %s'%event.key)

canvas.mpl_connect('key_press_event', on_key_event)

def _quit():
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
                    # Fatal Python Error: PyEval_RestoreThread: NULL tstate

button = Tk.Button(master=root, text='Quit', command=_quit)
button.pack(side=Tk.BOTTOM)

Tk.mainloop()

And I was happy… but if I try, as an example, to have a Text frame on top of it:

t=Text(root)
t.pack()

(before the canvas variable definition) as soon as I click on the Text frame & write into it, there is no way to click on the pyplot window and make it catch my keypresses again!

Any hints?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:14:42+00:00Added an answer on June 14, 2026 at 8:14 pm

    In tkinter, key events are routed to the widget that get the focus, thus your problem is to set focus back to the canvas. This is possible through binding on click on canvas to set the focus.

    For instance:

    canvas.mpl_connect('button_press_event', lambda event:canvas._tkcanvas.focus_set())
    #or canvas._tkcanvas.bind('<Button1>', ...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible to read mongodb data with hadoop connector but save output as
Possible Duplicate: Read file which is in use? I am trying to access a
I want to avoid locking on read if possible. But this feels like double-checked
i've read all possible blogs and SO post on the subject - but still
I know that it is possible read and write data from mongodb via hadoop.
Is possible to read from a txt file in loop like this? files names:
Possible Duplicate: Read Post Data submitted to ASP.Net Form I have a google checkout
Is it possible to read alphanumeric characters only from the given input line ignoring
Is it possible to read intermittently-sent data through a named pipe using redirection to
Is it possible to read WPF ResourceDictionaries from WinForms? If yes, how?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.