Python:用于重新绘制数据的Tkinter和pyplot

时间:2014-03-04 14:05:19

标签: python matplotlib tkinter

我正在尝试编写一个简单的程序,它读取三个不同小部件的值,并根据输入值绘制函数图。我遇到了以下问题:

1)按下按钮我得到错误" App_Window实例没有属性' refreshFigure'"

2)在退出ipython时(我将ipython-listener与gedit插件结合使用)我得到错误" Exception RuntimeError:'主线程不在主循环中'在德尔

我主要基于这个帖子 - Interactive plot based on Tkinter and matplotlib和这个帖子 - How do I refresh a matplotlib plot in a Tkinter window?

#!/usr/bin/python
from Tkinter import Tk, Frame
from Tkinter import *
import Tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
#this bit I added just because the NVC13 module is in another directory
import sys
for i in range(len(sys.path)):
    if sys.path[i]!='/home/captain-pants/NV_centri':
        if i==len(sys.path)-1:
            sys.path.append('/home/captain-pants/NV_centri')
        continue        
    else:
        break

import multiprocessing as mp 
import numpy as np
import matplotlib.pyplot as plt
#This module contains all the code necessary to return a list of values that depend on the input parameters
import NVC13_1lev_advanced_ex_nocoh as C13
from matplotlib.figure import Figure

#this function takes a list of parameters and returns a list of values dependant on those parameters.
def plotfunc(c13,Eg,Bz):
    Epam=[]
    nv = C13.NVC13(c13,[2580,1423],[Eg,Eg],0,0,Bz,0)
    evals = nv.intH()
    for i in range(len(evals[0])):
        Epam.append(np.real_if_close(evals[0][i]))
    return Epam

class App_Window:
    def __init__(self, parent):
        frame = Tkinter.Frame(parent)
        self.Egdata=DoubleVar()
        self.c13input=IntVar()
        self.Bzdata=DoubleVar()
        parent.title("Simple")
        Tkinter.Button(text='lulz',command=self.main1).grid(row=3,column=0)
        Tkinter.Label(textvariable=self.Egdata).grid(row=5,column=0)
        self.c13 = Tkinter.Entry(textvariable=self.c13input)
        self.Eg = Tkinter.Scale(orient='horizontal')
        self.Bz = Tkinter.Scale(orient='horizontal')
        self.c13.grid(row=6,column=0)
        self.Bz.grid(row=5,column=0)
        self.Eg.grid(row=4,column=0)
        Fig = Figure()
        FigSubPlot = Fig.add_subplot(111)
        self.line, = FigSubPlot.plot(range(10),'bo')
        x=[]
        y=[]
        self.canvas = FigureCanvasTkAgg(Fig, master=parent)
        self.canvas.show()
        self.canvas.get_tk_widget().grid(row=0,column=0,columnspan=2)
        frame.grid(row=0,column=0)
    def refreshFigure(self,y):
        x=np.arange(len(y))
        self.line.set_xdata(x)
        self.line.set_ydata(y)
        self.canvas.draw()
    def main1(self):
        self.c13input=int(self.c13.get())
        self.Egdata=self.Eg.get()
        self.Bzdata=self.Bz.get()
        values = plotfunc(self.c13input,self.Egdata,self.Bzdata)
            #Just to see whether I actually get the right thing in the input.
        print self.c13input
        self.refreshFigure(values)

root = Tkinter.Tk()
app = App_Window(root)
root.mainloop()

0 个答案:

没有答案