如何解决该错误? 'NoneType'对象没有属性'_root'

时间:2018-11-16 06:53:20

标签: python python-3.x matplotlib tkinter

我不知道如何解决这个问题,也不知道她为什么出现。

该如何解决?

其他人的类似问题与work.tpl变量有关,我认为这段代码不是相同的错误。

StringVar

完整错误:

import matplotlib
from math import *
import pylab
from matplotlib import mlab
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import tkinter as tk
from tkinter import ttk
from tkinter import *

g = 9.81

class MyData():
    v = IntVar()
    angle = IntVar()
    x0 = IntVar()
    y0 = IntVar()
    v2 = IntVar()
    angle2 = IntVar()
    x02 = IntVar()
    y02 = IntVar()
    v3 = IntVar()
    angle3 = IntVar()
    x03 = IntVar()
    y03 = IntVar()


class Fly(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "Fly")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        data = MyData

        for F in (StartPage, PageOne):
            frame = F(container, self, data)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller, data):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Начальная страница")
        label.pack(pady=10, padx=10)

        button = ttk.Button(self, text="Visit Page 1",
                            command=lambda: controller.show_frame(PageOne))
        button.pack()

        frame2 = Frame(self, borderwidth=4, relief=GROOVE)
        frame2.pack()

        first = Label(self, text='Данные для первого графика')
        first.pack()

        second = Label(self, text='Начальная скорость')
        second.pack()
        entry = Entry(self, width=10, textvariable=data.v)
        entry.pack()

        third = Label(self, text='Угол выстрела')
        third.pack()
        entry = Entry(self, width=10, textvariable=data.angle)
        entry.pack()

        fourth = Label(self, text='Начальная координата x')
        fourth.pack()
        entry = Entry(self, width=10, textvariable=data.x0)
        entry.pack()

        fifth = Label(self, text='Начальная координата y')
        fifth.pack()
        entry = Entry(self, width=10, textvariable=data.y0)
        entry.pack()

        frame3 = Frame(self, borderwidth=4, relief=GROOVE)
        frame3.pack()

        first = Label(self, text='Данные для второго графика')
        first.pack()

        second = Label(self, text='Начальная скорость')
        second.pack()
        entry = Entry(self, width=10, textvariable=data.v2)
        entry.pack()

        third = Label(self, text='Угол выстрела')
        third.pack()
        entry = Entry(self, width=10, textvariable=data.angle2)
        entry.pack()

        fourth = Label(self, text='Начальная координата x')
        fourth.pack()
        entry = Entry(self, width=10, textvariable=data.x02)
        entry.pack()

        fifth = Label(self, text='Начальная координата y')
        fifth.pack()
        entry = Entry(self, width=10, textvariable=data.y02)
        entry.pack()

        first = Label(self, text='Данные для третьего графика')
        first.pack()

        second = Label(self, text='Начальная скорость')
        second.pack()
        entry = Entry(self, width=10, textvariable=data.v3)
        entry.pack()

        third = Label(self, text='Угол выстрела')
        third.pack()
        entry = Entry(self, width=10, textvariable=data.angle3)
        entry.pack()

        fourth = Label(self, text='Начальная координата x')
        fourth.pack()
        entry = Entry(self, width=10, textvariable=data.x03)
        entry.pack()

        fifth = Label(self, text='Начальная координата y')
        fifth.pack()
        entry = Entry(self, width=10, textvariable=data.y03)
        entry.pack()


class PageOne(tk.Frame):

    def __init__(self, parent, controller, data):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Generate")
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Поменять значения",
                             command=lambda: controller.show_frame(StartPage))
        button1.pack()

        f = Figure(figsize=(5, 5), dpi=100)

        canvas = FigureCanvasTkAgg(f, self)
        canvas.show()
        canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

        toolbar = NavigationToolbar2TkAgg(canvas, self)
        toolbar.update()
        canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

        t = ((2 * data.v * sin(data.angle)) / g)
        vx = data.v * cos(data.angle)

        t2 = ((2 * data.v2 * sin(data.angle2)) / g)
        vx2 = data.v2 * cos(data.angle2)

        t3 = ((2 * data.v3 * sin(data.angle3)) / g)
        vx3 = data.v3 * cos(data.angle3)

        dx = 0.01
        xmin = data.x0
        xmax = vx * t * cos(data.angle)
        xmin2 = data.x02
        xmax2 = vx2 * t2 * cos(data.angle2)
        xmin3 = data.x03
        xmax3 = vx3 * t3 * cos(data.angle3)

        def func(x0, v, y0, angle):
            y = x0 * tan(angle) - (1 / (2 * v ** 2)) * ((g * x0 ** 2) / (cos(angle) ** 2)) + y0
            if y == 0:
                return 0.0
            return y


        def func2(x02, v2, y02, angle2):
            y2 = x02 * tan(angle2) - (1 / (2 * v2 ** 2)) * ((g * x02 ** 2) / (cos(angle2) ** 2)) + y02
            if y2 == 0:
                return 0.0
            return y2


        def func3(x03, v3, y03, angle3):
            y3 = x03 * tan(angle3) - (1 / (2 * v3 ** 2)) * ((g * x03 ** 2) / (cos(angle3) ** 2)) + y03
            if y3 == 0:
                return 0.0
            return y3


        xlist = mlab.frange(xmin, xmax, dx)
        xlist2 = mlab.frange(xmin2, xmax2, dx)
        xlist3 = mlab.frange(xmin3, xmax3, dx)

        ylist = [func(x0, data.v, data.y0, data.angle) for x0 in xlist]
        ylist2 = [func2(x02, data.v2, data.y02, data.angle2) for x02 in xlist2]
        ylist3 = [func3(x03, data.v3, data.y03, data.angle3) for x03 in xlist3]

        pylab.plot(xlist, ylist)
        pylab.plot(xlist2, ylist2)
        pylab.plot(xlist3, ylist3)

        pylab.show()


app = Fly()
app.mainloop()

此错误是否已解决?还是我需要制定其他决策算法?

昨天,我又有一个problemTraceback (most recent call last): File "C:/Users/Максим/PycharmProjects/test/test1.py", line 12, in <module> class MyData(): File "C:/Users/Максим/PycharmProjects/test/test1.py", line 13, in MyData v = IntVar() File "D:\Anaconda3\lib\tkinter\__init__.py", line 499, in __init__ Variable.__init__(self, master, value, name) File "D:\Anaconda3\lib\tkinter\__init__.py", line 314, in __init__ self._root = master._root() AttributeError: 'NoneType' object has no attribute '_root' global

1 个答案:

答案 0 :(得分:0)

您需要先创建根窗口小部件,然后再创建IntVar对象,请检查here