我正在爲使用Tkinter編寫python中的交互式數據擬合程序。 我想:顯示tkinter小部件中的變量值
變化的擬合曲線的起始參數(即,猜測參數)手動形成鍵盤和以從良好的點開始的裝配程序,在實驗數據繪製相應曲線(實現)
顯示在插件的參數的實際值,因爲我通過
我搜索網,我發現之間的一些連接鍵盤(未實現)進行更改我的問題和文本部件或輸入部件..
有沒有人有一個很好的解決方案?
這是修改代碼,因爲我們正在擬合一個簡單的指數,複製/粘貼運行並嘗試(使用'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()
你的問題還不清楚。當鍵入「鍵盤」時,他們在控制檯中鍵入內容,還是鍵入文本或輸入窗口小部件? –
@Bryan Oakley:你說得對,我的意思是他們用鍵盤輸入,而焦點放在我用來繪製曲線的畫布上;我希望這些參數出現在另一個小部件中(可能是一個文本?或者是一個包含參數數量的條目?),而焦點仍然在畫布部件中。感謝您的關注 – Copo
爲什麼必須關注焦點畫布?爲什麼不把焦點放在文本小部件或條目上? –