隐藏数字而不是重叠

时间:2018-05-30 10:20:37

标签: python matplotlib tkinter pip sensor

我有一个应该模拟传感器的功能。对于这个任务,我已经实现了一个函数,它读取鼠标单击并在框架中放置一个小圆圈。 使用按钮,我开始使用此功能模拟生长传感器波:

import matplotlib.pyplot as plt
import matplotlib
import math
import numpy as np
import time
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, 
NavigationToolbar2Tk

from matplotlib.backend_bases import key_press_handler

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

root = Tk.Tk()
root.wm_title("Localization")

x = [0. , 100.]
y = [0. , 100.]

fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.axis([min(x), max(x), min(y), max(y)])

# a tk.DrawingArea
canvas1 = FigureCanvasTkAgg(fig1, master=root)
canvas1.draw()
canvas1.get_tk_widget().pack(side=Tk.LEFT, fill=Tk.X, expand=1)

toolbar = NavigationToolbar2Tk(canvas1, root)
toolbar.update()
canvas1._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

coords = []
distanceToMid = []
timer = []

def onclick(event):
    counter = 0

    if event.button:
        print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
            (event.button, event.x, event.y, event.xdata, event.ydata)) 
        plt.plot(event.xdata, event.ydata, ',')

    #right click sets the black node circles
    if  (event.button == 3 and counter < 9):
        ix,iy = event.xdata, event.ydata
        coords.append((ix,iy))
        circle = plt.Circle((event.xdata,event.ydata), 0.5, color='black', 
alpha=1)
        ax1.add_artist(circle)
        fig1.canvas.draw()
        counter += 1

    #double-click imitates the wave from the middle 
    if event.dblclick:
        for i in range(1, 72):
            circle_mid = plt.Circle((50,50), i ,color='red', alpha=1, fill=1)
            ax1.add_artist(circle_mid)
            fig1.canvas.draw()
        circle_clear = plt.Circle((50,50), 72, color="white", alpha=1, fill=1)
        ax1.add_artist(circle_clear)
        for j in range(0,len(coords)):
            circle_loc = plt.Circle(coords[j],0.5,color="black", alpha=1, fill=1)
            ax1.add_artist(circle_loc)
        fig1.canvas.draw()



cid = fig1.canvas.mpl_connect('button_press_event', onclick)

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

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

#Shows the 9 node coordinates
def callback():
        print (coords)

button2 = Tk.Button(master=root, text="Show node coords", command=callback)
button2.pack(side=Tk.LEFT)

def startpositioning():
    for i in range(0,len(coords)):
        start_time = time.time()
        for j in range(1,142):
            circle_loc = plt.Circle(coords[i], j, color='green', alpha=1, fill=1)
            ax1.add_artist(circle_loc)
            fig1.canvas.draw()
        timer.append(time.time() - start_time)
        circle_clear = plt.Circle((50,50), 72, color="white", alpha=1, fill=1)
        ax1.add_artist(circle_clear)
        for j in range(0,len(coords)):
            circle_loc = plt.Circle(coords[j],0.5,color="black", alpha=1, fill=1)
            ax1.add_artist(circle_loc)
        fig1.canvas.draw()
    print (timer)


button3 = Tk.Button(master=root, text="Start positioning from nodes", 
command=startpositioning)
button3.pack(side=Tk.LEFT)

def distancemid():
    for i in range(0,len(coords)):
        distanceToMid.append(math.hypot(coords[i][0] - 50, coords[i][1] - 50))
    print(distanceToMid)

button4 = Tk.Button(master=root, text="Distance to mid", command=distancemid)
button4.pack(side=Tk.LEFT)

Tk.mainloop()
# If you put root.destroy() here, it will cause an error if
# the window is closed with the window manager.

我的问题是第一点的增长时间比第二点和第三点增长快得多,依此类推,如果定义的话。是否有可能所有的成长速度都相同。 是否可以隐藏第一个生长的圆圈,而不是与设置节点的白色矩形重叠?

0 个答案:

没有答案