Domanda

Mi piacerebbe creare un programma che disegna un grafico cosinus nell'intervallo orderd.Ma c'è un errore che non sono in grado di riparare.Messaggio di errore:"AttributeError:l'istanza del programma non ha attributo " mp "" Ecco il mio codice:

# -*- coding: utf-8 -*-

from Tkinter import Tk, W, E
from ttk import Label, Button, Frame, Entry,Style
import math
import sys
import matplotlib as mp

class program(Frame):

    def __init__(self,main):

        Frame.__init__(self,main)        
        self.main = main       
        self.initUI()

    def initUI(self):

        self.main.title('COSINUSEK')
        Style().configure('TFrame', background = 'black')
        Style().configure('TLabel', background = 'black', foreground = 'blue')
        Style().configure("TButton", background = 'red', foreground = 'blue')


        self.rowconfigure(0, pad = 3)
        self.rowconfigure(1, pad = 3)
        self.rowconfigure(2, pad = 3)
        self.rowconfigure(3, pad = 3)
        self.rowconfigure(4, pad = 3)

        self.columnconfigure(0,pad =3)
        self.columnconfigure(1,pad =3)
        self.columnconfigure(2,pad =3)
        self.columnconfigure(3,pad =3)
        self.columnconfigure(4,pad =3)



        label = Label(self, text = 'Give me range in degrees ').grid(row = 0,column = 3)
        od = Label(self, text = '             From').grid(row = 1, column =0)
        do = Label(self, text = '             To').grid(row = 1, column =4 )
        self.entry = Entry(self, justify = 'center')
        self.entry.grid(row = 2,column = 0,columnspan = 2 ,sticky = E+ W)
        self.entry1 = Entry(self, justify = 'center')
        self.entry1.grid(row = 2,column = 4,columnspan = 2, sticky = E)
        button =  Button(self, text = 'Ok',command = self.ok).grid(row = 3,column = 3)
        button1 = Button(self, text = 'Draw', command = self.dra).grid(row = 4, column = 3)

        self.pack()

    def run(self):
        self.main.mainloop()

    def ok(self):
        x = []
        y = []
        z = int(self.entry.get())
        w = int(self.entry1.get())
        i = w
        while i in range(w,z):
            x.append(i)
            for a in x:
                y[a] = math.cos((x[a]*math.pi)/180)
            i = i + 0.01
    def dra(self):
        mp.ion() 
        mp.plot(self.x,self.y)
        mp.title('Wykres')
        mp.xlabel('x')
        mp.ylabel('y')
        mp.draw()  



program(Tk()).run()
È stato utile?

Soluzione

È necessario utilizzare un FigureCanvasTkAgg widget per incorporare figure matplotlib in una GUI Tkinter.C'è un esempio nei documenti, qui.

Ecco una leggera modifica del tuo codice, che mostra come potresti incorporare le modifiche necessarie:

from Tkinter import Tk, W, E
from ttk import Label, Button, Frame, Entry, Style
import sys
import matplotlib.figure as mplfig
import matplotlib.backends.backend_tkagg as tkagg
import numpy as np


class program(Frame):

    def __init__(self, main):

        Frame.__init__(self, main)
        self.main = main
        self.initUI()

    def initUI(self):

        self.main.title('COSINUSEK')
        Style().configure('TFrame', background='black')
        Style().configure('TLabel', background='black', foreground='blue')
        Style().configure("TButton", background='red', foreground='blue')

        self.rowconfigure(0, pad=3)
        self.rowconfigure(1, pad=3)
        self.rowconfigure(2, pad=3)
        self.rowconfigure(3, pad=3)
        self.rowconfigure(4, pad=3)

        self.columnconfigure(0, pad=3)
        self.columnconfigure(1, pad=3)
        self.columnconfigure(2, pad=3)
        self.columnconfigure(3, pad=3)
        self.columnconfigure(4, pad=3)

        label = Label(self, text='Give me range in degrees ').grid(
            row=0, column=3)
        od = Label(self, text='             From').grid(row=1, column=0)
        do = Label(self, text='             To').grid(row=1, column=4)
        self.entry = Entry(self, justify='center')
        self.entry.grid(row=2, column=0, columnspan=2, sticky=E + W)
        self.entry1 = Entry(self, justify='center')
        self.entry1.grid(row=2, column=4, columnspan=2, sticky=E)
        button = Button(
            self, text='Ok', command=self.ok).grid(row=3, column=3)
        button1 = Button(
            self, text='Draw', command=self.dra).grid(row=4, column=3)

        self.fig = mplfig.Figure(figsize=(5, 4), dpi = 100)
        self.ax = self.fig.add_subplot(111)
        self.canvas = tkagg.FigureCanvasTkAgg(self.fig, self.main)
        self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
        self.line = None
        self.pack()

    def run(self):
        self.main.mainloop()

    def ok(self):
        z = int(self.entry.get())
        w = int(self.entry1.get())
        self.x = np.arange(z, w, 0.01)
        self.y = np.cos((self.x * np.pi) / 180)

    def dra(self):
        self.ok()
        if self.line is not None:
            self.line.set_xdata(self.x)
            self.line.set_ydata(self.y) 
        else:
            self.line = self.ax.plot(self.x, self.y)[0]
        self.ax.set_xlim(self.x.min(), self.x.max())
        self.ax.relim()
        self.ax.autoscale_view(True, True, True)

        self.fig.canvas.draw()


program(Tk()).run()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top