0

The python code below was provided by NMech here.

import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
import numpy as np
class Application(tk.Frame):
 def __init__(self, master):
 tk.Frame.__init__(self,master)
 self.createWidgets()
 
 def createWidgets(self):
 fig = plt.figure(figsize=(8,8))
 ax = fig.add_axes([0.1,0.1,0.8,0.8], polar=True)
 canvas = FigureCanvasTkAgg(fig, master=root)
 canvas.get_tk_widget().grid(row=0,column=1)
 self.plotbutton = tk.Button(master=root, text="plot", command=lambda: self.plot(canvas, ax))
 self.plotbutton.grid(row=0, column=0)
 def plot(self, canvas, ax):
 c= ['r','b','g']
 ax.clear()
 for i in range(3):
 theta = np.random.uniform(0,360,10)
 r = np.random.uniform(0,1,10)
 ax.plot(theta,r,linestyle="None",marker='o',color=c[i])
 canvas.draw()
root=tk.Tk()
app=Application(master=root)
app.mainloop()

The code displays two similar windows shown below. One window is the app labeled 'tk', and the second window is labeled 'Figure 1'. How do I modify the code so it only displays the app 'tk'?

two-windows

Bryan Oakley
388k53 gold badges580 silver badges738 bronze badges
asked Jan 8, 2024 at 15:47
2
  • 1
    Hi there @TedErsek, I just tried your code, mine is just showing a single window with label tk? Commented Jan 8, 2024 at 16:28
  • I also don't get the "Figure 1" but I found the python process does not end when I close the tk window. I tried replacing fig = plt.figure with fig = matplotlib.figure.Figure, then it worked as expected. As I understand it, pyplot always does window management for you but in this case you are organising that for yourself so you don't want to use pyplot at all. Commented Jan 8, 2024 at 17:23

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.