在tkinter小部件中显示变量值

时间:2012-08-24 13:22:32

标签: python tkinter widget

我正在使用Tkinter编写一个用于在python中进行交互式数据拟合的程序。 我想:

  1. 手动从键盘上更改拟合曲线的起始参数(即猜测参数),并在实验数据上绘制相应的曲线,以便从一个好点开始拟合过程(已知)

  2. 显示小部件中参数的实际值,因为我通过键盘更改了它们(未实现)

  3. 我搜索了网络,发现我的问题与文本小部件或条目小部件之间存在一些联系。

    有人有一个很好的解决方案吗?

    这里修改了代码,因为我们正在拟合一个简单的指数,复制/粘贴运行并尝试(使用'r','t','y','f','g','h'键来修改params)..

    import Tkinter as Tkinter
    from Tkinter import *
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    from matplotlib.figure import Figure
    from numpy import *  #exp,arange,sin,arctan,where
    import sys
    from scipy.optimize import leastsq
    
    class App:
    
    def __init__(self, master,slave,folnam,q_index,tab):
        # Create containers
        self.frame = Tkinter.Frame(master)
        self.frame2 = Tkinter.Text(slave,width=10,height=10)
        self.frame2.pack()
    
        self.q_index=q_index
    
        # Create buttons and bindings
        self.button_quit = Tkinter.Button(self.frame,text="QUIT", command=master.destroy)
        self.button_quit.pack(side="left")
        self.button_fit = Tkinter.Button(self.frame, text= 'fit!')
        self.button_fit.pack()
    
        self.frame.bind_all("<Key>", self.decrease_a,'+')    #################################
    
    
        # Fill with Data
        self.t = arange(1000)*.001
        self.data_to_fit = exp(-self.t)
        self.A=max(self.data_to_fit)-min(self.data_to_fit)
        self.B=min(self.data_to_fit)
    
        # Build Figure
        fig = Figure()
        self.ax = fig.add_subplot(111)
        self.ax.set_ylim( min(self.data_to_fit), max(self.data_to_fit) )      
        self.p = [self.A,self.B,.9,.5,10.,5.]
        self.line, = self.ax.plot(self.t[1:],abs(self.schultz(self.t[1:], 1., self.p)),'.-')   #tuple of a single element
        self.canvas = FigureCanvasTkAgg(fig,master=master)
        self.ax.plot(self.t[1:],self.data_to_fit[1:])
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
        self.frame.pack()
    
    
    
    
    def schultz(self, t, q, p):
        Z=.1
        A, B, alpha, D, vm, sigma = p
        Z = ((sigma/vm)**-2)-1.
        Lambda = q*vm*t/(Z+1)
        g = ((Z+1)/(Z*q*vm*t))*sin(Z*arctan(Lambda))/(1+Lambda**2)**(Z/2.)
        where( abs(t)>0., g, 1.)
        f = exp(-q**2*D*t)*((1.-alpha)+alpha*g)
        y = A*f+B
        return y
    
    
    
    
    def decrease_a(self,event):
    
      # Raise/lower amplitude with 'a', 'q' keys
        if event.char=='a':
            self.ax.get_ylim()          
            self.p[0]-= 1e10
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()  
        if event.char=='q':
            self.ax.get_ylim()          
            self.p[0]+=1e10
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()  
        # Raise/lower baseline with 's', 'w' keys
        if event.char=='s':
            self.ax.get_ylim()          
            self.p[1]-= 1e10
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()      
        if event.char=='w':
            self.ax.get_ylim()          
            self.p[1]+= 1e10
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()
        # Raise/lower alpha with 'd', 'e' keys
        if event.char=='d':
            self.ax.get_ylim()          
            self.p[2]-= .05
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()
        if event.char=='e':
            self.ax.get_ylim()          
            self.p[2]+= .05
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()
        # Raise/lower diffusion coefficient with 'f', 'r' keys
        if event.char=='f':
            self.ax.get_ylim()          
            self.p[3]-= .05
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()
        if event.char=='r':
            self.ax.get_ylim()          
            self.p[3]+= .05
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()
        # Raise/lower average speed with 'g', 't' keys
        if event.char=='g':
            self.ax.get_ylim()          
            self.p[2]-= .05
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()
    
        if event.char=='t':
            self.ax.get_ylim()          
            self.p[2]+= .05
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()
        # Raise/lower variance of speed distribution with 'h', 'y' keys
        if event.char=='h':
            self.ax.get_ylim()          
            self.p[2]-= .01
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()
        if event.char=='y':
            self.ax.get_ylim()          
            self.p[2]+= .01
            self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
            self.line.set_xdata(self.t[1:])           
            self.canvas.draw()
    
    root = Tkinter.Tk()
    root2 = Tkinter.Tk()
    app = App(root,root2,'/home/copo/Scrivania/correlazioni_da_fit',q_index=10, tab=False)
    
    root.mainloop()
    

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解这个问题,但如果您想要做的只是显示self.p的值,您可以通过多种方式实现。例如,您可以使用每次更改参数时更新的标签。例如:

self.p0_label = Tkinter.Label(...)
self.p1_label = Tkinter.Label(...)
...
def decrease_a(self,event):
    if event.char=='a':
        self.ax.get_ylim()          
        self.p[0]-= 1e10
        self.line.set_ydata(self.schultz(self.t[1:], 1, self.p))
        self.line.set_xdata(self.t[1:])           
        self.canvas.draw()  
    ...
    self.update_display()

def update_display(self):
    self.p0_label.configure(text=str(self.p[0]))
    self.p1_label.configure(text=str(self.p[1]))
    ...