简体   繁体   English

如何绘制半径可缩放的圆?

[英]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. 已经使用Tk.Scale设置了一个很小的GUI,其中包括Tk.Scale和画布。 The function drawCircle creates a Circle artist. 函数drawCircle创建一个Circle艺术家。 The essential part is connecting the slider with the function changeRadius but thats where I don't know what to do. 基本部分是将滑块与功能changeRadius连接changeRadius但是那是我不知道该怎么做的地方。 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 Tkinter回调回溯中的异常(最近一次调用最后一次):文件“ C:\\ Users \\ p.schulmeyer \\ AppData \\ Local \\ Continuum \\ anaconda3 \\ lib \\ tkinter__init __。py”,行1705,在调用返回self.func(* args )TypeError:lambda()接受0个位置参数,但给出了1个

I also tried using lambda e: or not using lambda at all but nothing helped. 我也尝试使用lambda e:或完全不使用lambda,但无济于事。 I guess my mistake must be something more fundamental. 我想我的错误一定是更根本的东西。 I really appreciate any help. 我非常感谢您的帮助。 Thanks! 谢谢!

You need to do the following changes to make your script work. 您需要进行以下更改才能使脚本正常工作。

  • Ensure that you call drawCircle in constructor, hence self.circle is set 确保在构造函数中调用drawCircle,因此将self.circle设置为
  • 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() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM