У меня возникли проблемы с отображением панели инструментов навигации, встроенной в tkinter. - PullRequest
0 голосов
/ 11 октября 2011

Я написал эту программу, которая использует графический интерфейс tkinter для построения орбит с помощью matplotlib. Все работает, кроме панели инструментов на графике. У меня это там, но не активно. Программа просто ничего не делает, когда у меня активна панель инструментов.

вот программа:

import matplotlib as plt
plt.use('TkAgg')

import math as mt
import numpy as np
import tkinter as tk
import pylab as pb

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure

#class that creates GUI to take input and fills coresponding arrays
#for a paticular ode method
class Application(tk.Frame):
    #initialize tkinter gui
    def __init__(self, master):
        super(Application,self).__init__(master)
        self.grid()
        self.GUItrajectory()
    #create tkinter gui
    def GUItrajectory(self):
        """Creates GUI for solving trajectory problems"""
        #create plot
        self.f = Figure(figsize=(5,4), dpi=100)
        self.a = self.f.add_subplot(111)
        self.canvas = FigureCanvasTkAgg(self.f, self)
        self.canvas.get_tk_widget().grid(row = 0, column = 5, columnspan = 6, rowspan = 7)

        #self.toolbar = NavigationToolbar2TkAgg(self.canvas, self)
        #self.toolbar.update()
        #self.canvas._tkcanvas.grid(row = 1, column = 5, columnspan = 6, rowspan = 7)
        #take input
        self.mass1lbl = tk.Label(self, text = "Mass 1 [kg]")
        self.mass1lbl.grid(row = 0, column = 0, columnspan = 1)
        self.mass1ent = tk.Entry(self)
        self.mass1ent.grid(row = 0, column = 1, columnspan = 1, sticky = "e")

        self.mass2lbl = tk.Label(self, text = "Mass 2 [kg]")
        self.mass2lbl.grid(row = 1, column = 0, columnspan = 1)
        self.mass2ent = tk.Entry(self)
        self.mass2ent.grid(row = 1, column = 1, columnspan = 1, sticky = "e")

        self.seperationlbl = tk.Label(self, text = "Seperation distance [m]")
        self.seperationlbl.grid(row = 2, column = 0, columnspan = 1)
        self.seperationent = tk.Entry(self)
        self.seperationent.grid(row = 2, column = 1, columnspan = 1, sticky = "e")

        self.periodlbl = tk.Label(self, text = "Period [hours]")
        self.periodlbl.grid(row = 3, column = 0, columnspan = 1)
        self.periodent = tk.Entry(self)
        self.periodent.grid(row = 3, column = 1, columnspan = 1, sticky = "e")

        #submit button to retrieve input values
        self.btn0 = tk.Button(self, text = "Submit")
        self.btn0.grid(row = 4, column = 1, columnspan = 1)
        self.btn0.bind('<Button-1>',self.submit)

        self.btn3 = tk.Button(self, text = "Calculate Orbit")
        self.btn3.grid(row = 5, column = 1, columnspan = 2)
        self.btn3.bind('<Button-1>', self.btn3array)

    def submit(self, parameters):
        """retreives inputs from GUI"""
        m1 = float(self.mass1ent.get())
        m2 = float(self.mass2ent.get())
        r0 = float(self.seperationent.get())
        period = float(self.periodent.get())
        dr0 = 0
        G = 8.64929e-4
        mu = m1*m2/(m1+m2)
        theta0 = 0
        dtheta0 = (2)*mt.pi*pow(period,-1)
        l = mu*pow(r0,2)*dtheta0

        self.parameters = [G, mu, r0, dr0, theta0, dtheta0, l, period, m2, m1]

    def btn3array(self, parameters):
        """does lots of stuff, probably to much"""
        parameters = self.parameters
        G = parameters[0]
        mu = parameters[1]
        r0 = parameters[2]
        dr0 = parameters[3]
        theta0 = parameters[4]
        dtheta0 = parameters[5]
        l = parameters[6]
        period = parameters[7]
        m1 = parameters[8]
        m2 = parameters[9]

        #time(global)
        ti = 0
        tf = period
        dt = .1
        t = np.arange(ti, tf, dt)

        r = []
        dr = []
        theta = []
        dtheta = []
        x = []
        y = []

        r.append(r0)
        dr.append(dr0)
        theta.append(theta0)
        dtheta.append(dtheta0)

        def orbit101(r,dr,mu,G,M,m,l):
            """equation for orbit"""
            dr0=dr
            dr1=(pow(l,2)/(pow(mu,2)*pow(r,3)))-((G*m1*m2)/(mu*(pow(r,2))))
            dr=[dr0,dr1]

            return dr

        def rk4(x, v, dt, mu, G, m1, m2, l):
            """Returns final (position, velocity) tuple after time dt has passed."""
            x1 = x
            v1 = v
            f1 = orbit101(x1, v1, mu, G, m1, m2, l)
            a1 = f1[1]

            x2 = x + 0.5*v1*dt
            v2 = v + 0.5*a1*dt
            f2 = orbit101(x2, v2, mu, G, m1, m2, l)
            a2 = f2[1]

            x3 = x + 0.5*v2*dt
            v3 = v + 0.5*a2*dt
            f3 = orbit101(x3, v3, mu, G, m1, m2, l)
            a3 = f3[1]

            x4 = x + v3*dt
            v4 = v + a3*dt
            f4 = orbit101(x4, v4, mu, G, m1, m2, l)
            a4 = f4[1]

            xf = x + (dt/6.0)*(v1 + 2*v2 + 2*v3 + v4)
            vf = v + (dt/6.0)*(a1 + 2*a2 + 2*a3 + a4)

            return xf, vf


        for i in range(0,len(t)-1):
            g = orbit101(r[i], dr[i], mu,G, m1, m2,l)
            v = g[0]
            a = g[1]
            h = rk4(r[i], v, dt, mu, G, m1, m2, l)
            p = h[0]
            v = h[1]
            r.append(p)
            dr.append(v)
            dtheta.append(l/(mu*pow(p,2)))
            T = theta[i]+dtheta[i]*dt
            theta.append(T)

            x.append(p*mt.cos(T))
            y.append(p*mt.sin(T))
        self.a.plot(x,y)
        self.canvas.draw()


#Main
root = tk.Tk()
root.title("BINARY ORBIT CALCULATOR")
root.geometry("750x550")
Application(root)
root.mainloop()
...