0
我想在Tkinter中創建一個簡單的GUI來顯示一些xy數據。數據安排在試驗中,目標是建立一個旋轉盒,讓您選擇要查看哪個試驗。我不明白爲什麼,但在下面的代碼文本變量的值沒有更新,所以我總是最終看到相同的審判。有什麼明顯的我失蹤了?代碼如下。 Tk.IntVar被設置在init中,與spinbox關聯(非常底部)並在updateDataToPlot中使用。Tkinter spinbox textvariable
#graphing stuff
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from matplotlib.lines import Line2D
import Tkinter as Tk
import numpy as np
import matplotlib.pyplot as plt
from scipy import randn
from collections import defaultdict
class DataPlotter:
def __init__(self, ax, data):
self.ax = ax
self.canvas = ax.figure.canvas
self.data = data
self.xRaw = []
self.yRaw = []
self.lineRaw = Line2D(self.xRaw, self.yRaw, animated=True, marker = None, color = 'black')
self.ax.add_line(self.lineRaw)
self.background = None
self.canvas.mpl_connect('draw_event', self.updateBackground)
self.ax.set_ylim(-3,3)
self.ax.set_xlim(-3,3)
self.ax.set_xlabel('X Pos (mm)')
self.ax.set_ylabel('Y Pos (mm)')
self.TrialNum = Tk.IntVar()
self.TrialNum.set(0)
def updateBackground(self, event):
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
def updateDataToPlot(self):
trialNum = self.TrialNum.get()
print trialNum
self.xRaw = self.data['polaris_rawPositions'][trialNum][:,0]
self.yRaw = self.data['polaris_rawPositions'][trialNum][:,1]
self.updateView()
def updateView(self):
if self.background is None: return True
self.ax.figure.canvas.draw()
self.canvas.restore_region(self.background)
self.lineRaw.set_data(self.xRaw, self.yRaw)
self.ax.draw_artist(self.lineRaw)
self.canvas.blit(self.ax.bbox)
data = defaultdict(list)
for i in range(100):
data['polaris_rawPositions'].append(randn(200,2))
root = Tk.Tk()
root.wm_title("Data Explorer")
f = Figure()
ax = f.add_subplot(111)
canvas = FigureCanvasTkAgg(f, master=root)
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
canvas.show()
Plotter = DataPlotter(ax, data)
spinbox = Tk.Spinbox(master=root, textvariable = Plotter.TrialNum, command = Plotter.updateDataToPlot, fg= 'black', from_ = 0, to = 100, increment = 1)
spinbox.pack(side=Tk.BOTTOM, anchor='e', padx = 5)
root.mainloop()
'/ data/data'是郵件/帖子的佔位符。在我的電腦上,它是一個真正的字典,充滿了numpy數組。我已經完成了打印(trialNum),它總是給我0(或者我設置變量的初始值)。逗號對於numpy索引是正確的。嗯 – dataman 2012-02-14 22:33:38
我已將代碼更改爲完全獨立的示例 – dataman 2012-02-14 23:05:06
示例在此處看起來不錯。你在哪個平臺上? – 2012-02-19 20:49:21