Philipp
Philipp

Reputation: 323

How to plot circle with scalable radius?

I wrote a few lines of code that should draw a circle where I can adjust its radius by using a slider. Unfortunately there must be some major mistakes in my code but as I am a beginner it is hard to find them. Can anyone give me some advise to make it work?

A tiny GUI has been set up using tkinter including a Tk.Scale and a canvas. The function drawCircle creates a Circle artist. The essential part is connecting the slider with the function changeRadius but thats where I don't know what to do. See my code below...

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

class PlotCircle():

    def __init__(self, master):
        self.master = master
        master.iconify

        self.f_rad = 2  # initial value

        self.frame = Tk.Frame(master)
        self.frame.pack(side=Tk.TOP, fill=Tk.BOTH, expand=0)
        self.radius_label = Tk.Label(self.frame, text='Radius: ')
        self.radius_label.pack(side=Tk.LEFT)
        self.scroll_radius = Tk.Scale(self.frame, from_=1.0, to=3.0, resolution=0.05,
                                      orient=Tk.HORIZONTAL, command=lambda:self.changeRadius(self.circle))
        self.scroll_radius.set(2.0)
        self.scroll_radius.pack(side=Tk.LEFT)
        self.image_frame = Tk.Frame(master)
        self.image_frame.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

        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)     

    def drawCircle(self):
        self.circle = plt.Circle((0.5, 0.5), self.f_rad*0.1, color='#F97306', fill=False)
        self.ax.add_artist(self.circle)
        self.fig.canvas.draw()

    def changeRadius(self, circ):
        self.f_rad = float(self.scroll_radius.get())
        print(self.f_rad)
        circ.set_radius(self.f_rad)
        self.fig.canvas.draw()

root = Tk.Tk()
PlotCircle(root)
root.mainloop()

By executing this code I get the following error:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\p.schulmeyer\AppData\Local\Continuum\anaconda3\lib\tkinter__init__.py", line 1705, in call return self.func(*args) TypeError: lambda() takes 0 positional arguments but 1 was given

I also tried using lambda e: or not using lambda at all but nothing helped. I guess my mistake must be something more fundamental. I really appreciate any help. Thanks!

Upvotes: 4

Views: 372

Answers (1)

vin
vin

Reputation: 1019

You need to do the following changes to make your script work.

  • Ensure that you call drawCircle in constructor, hence self.circle is set
  • command needs a function that can accept a param

    def __init__(self, master):
        ...
        self.scroll_radius = Tk.Scale(self.frame, from_=1.0, to=3.0, resolution=0.05,
                                      orient=Tk.HORIZONTAL, command=lambda x:self.changeRadius(x))
    
        ...
        self.drawCircle()  
    
    def changeRadius(self, new_radius):
        self.circle.set_radius(float(new_radius)*0.1)
        self.fig.canvas.draw()
    

Upvotes: 1

Related Questions