Plot aktualisieren in Tkinter

Fragen zu Tkinter.
Antworten
maxge
User
Beiträge: 3
Registriert: Sonntag 4. Februar 2018, 11:18

Hallo,

ich versuche gerade mit tkinter eine GUI zu erstellen, die mir über die Eingabe von Länge und Radius eine Strecke zusammenbastelt.
Dazu würde ich gerne die Strecke in x,y nach jedem Klick auf Add in der Figure aktualisieren / neu plotten. Ich habe gehofft, das funktioniert, indem ich meine Funktion gui_plot, die am Anfang funktioniert, einfach erneut mit neuem input aufrufe, aber da habe ich mich leider getäuscht.

Wenn ich dazu recherchiere finde ich eigentlich nur Lösungen, die mit classes, __init__ und self arbeiten, wovon ich leider nicht so viel verstehe. (Komm ich da auch langfristig drum herum oder sollte ich mich da besser durchbeißen?)
Bekommt man die Aktualisierung des Plots auch so irgendwie hin?
Danke für Eure Hilfe!

Code: Alles auswählen

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *

root = Tk()
root.title('Track')
root.geometry('800x500')

distance = [0]
curvature = [0]

global c
c = 0

def add():
    global c
    dist = float(length.get())
    i = 0
    while i < dist:
        distance.append(distance[c + i] + 1)
        i += 1
    if tkvar.get() == 'Straight':
        j = 0
        while j < dist:
            curvature.append(0)
            j+=1

    elif tkvar.get() == 'Radius':
        rad = float(radius.get())
        j = 0
        while j < dist:
            curvature.append(1/rad)
            j+=1

    print(distance)
    print(curvature)
    c+=int(dist)
    global x
    global y
    x,y = calc_xy(curvature)
    gui_plot(x,y)


def calc_xy(curvature):
    x_re = [0] * len(curvature)
    y_re = [0] * len(curvature)
    phi = [0] * len(curvature)
    stepsize = 1

    for i in range(0, len(curvature) - 1):
        x_re[i + 1] = x_re[i] - (np.sin(curvature[i + 1] * stepsize) * stepsize) * np.cos(phi[i]) - (np.cos(
            curvature[i + 1] * stepsize) * stepsize) * np.sin(phi[i])
        y_re[i + 1] = y_re[i] + (np.cos(curvature[i + 1] * stepsize) * stepsize) * np.cos(phi[i]) - (np.sin(
            curvature[i + 1] * stepsize) * stepsize) * np.sin(phi[i])
        phi[i + 1] = phi[i] + curvature[i + 1] * stepsize
    return x_re, y_re


# Add a grid
mainframe = Frame(root)
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
mainframe.pack(pady=20, padx=20)

# Create a Tkinter variable
tkvar = StringVar(root)

# Dictionary with options
choices = {'Straight', 'Radius'}
tkvar.set('Straight')  # set the default option

popupMenu = OptionMenu(mainframe, tkvar, *choices)
Label(mainframe, text="Select").grid(row=1, column=1)
popupMenu.grid(row=2, column=1)

Label(mainframe, text='Length').grid(row=1, column = 2)
length = Entry(mainframe)
length.grid(row=2, column=2)
Label(mainframe, text='Radius').grid(row=1, column =3)
radius = Entry(mainframe)
radius.grid(row=2, column = 3)
add_button = Button(mainframe, text='Add', command=add).grid(row=2, column=4)
save_button = Button(mainframe, text='Save Track', command = save_track).grid(row=2, column=5)


def gui_plot(x,y):
    fig = Figure(figsize=(6, 6))
    a = fig.add_subplot(111)
    a.plot(x, y)
    a.axis('equal')
    canvas = FigureCanvasTkAgg(fig, master=root)
    canvas.get_tk_widget().pack()
    canvas.draw()
    print('Plot refreshed')

gui_plot(0,0)



# on change dropdown value
def change_dropdown(*args):
    print(tkvar.get())


# link function to change dropdown
tkvar.trace('w', change_dropdown)



root.mainloop()
Antworten