【发布时间】:2016-11-16 05:49:27
【问题描述】:
我正在创建一个 GUI,以使用 tkinter 绘制来自温度传感器的实时图表,以及来自 matplotlib 的动画功能。我得到了漂亮的实时图表。但我面临的问题是,实时图表在我运行应用程序后立即开始。我已经创建了一个按钮来调用 class StartPage 中的命令,但它不起作用。我希望我的应用程序在单击 StartPage 类中的 Start Monitoring 时开始收集数据和绘制图表
如果有人能帮我解决这个问题,那就太好了。
比你。
def animate(i):
ipcon = IPConnection() # Create IP connection
ptc1 = BrickletPTC(UID1, ipcon) # S3
ptc2 = BrickletPTC(UID2, ipcon) # S7
ptc3 = BrickletPTC(UID3, ipcon) # S7
ptc4 = BrickletPTC(UID4, ipcon) # S7
ipcon.connect(HOST, PORT) # Connect to brickd
temperature1 = ptc1.get_temperature() - 20
temperature2 = ptc2.get_temperature() + 6
temperature3 = ptc3.get_temperature() + 3
temperature4 = ptc4.get_temperature() + 29
l = [temperature1, temperature2]
avgTemp = np.mean(l)
avgTempM = str(avgTemp/100)
dataArrayavg = avgTempM.split(',')
tempavg = float(dataArrayavg[0])
tempCavg.append(tempavg)
avg.set_xlabel('Time [s]', fontsize = 10)
avg.set_ylabel('Temperature [°C]', fontsize = 10)
y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
avg.yaxis.set_major_formatter(y_formatter)
titleavg = "Average Room Temperature: " + str(temperature1/100) + " °C"
avg.set_title(titleavg, fontsize = 12)
avg.plot(tempCavg, "#483D8B")
f1.savefig('RoomTemperature.png')
f2.savefig('ObjectTemperature.png')
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
# Adding a logo to the startpage
logo = Image.open("Gemeinschaftslogo(5).jpg")
Image1 = ImageTk.PhotoImage(logo)
# defining the image label
imageLabel = Label(self, image = Image1)
imageLabel.image = Image1
imageLabel.pack(pady=30, padx=30)
#defining labels
label = ttk.Label(self, text="Zustandsüberwachung (Condition Monitoring) von industriellen Computertomographie-Systemen", font = LARGE_FONT,)
label.pack(pady=10, padx=10)
#creating button
#if agree then move to temperature graph
button1 = ttk.Button(self, text="Start Monitoring",
command=lambda: animate()) # with lambda we are moving from start page to page 1
button1.pack(pady=30, padx=30)
button2 = ttk.Button(self, text="Enter",
command=lambda: controller.show_frame(RoomTempGraph)) # with lambda we are moving from start page to page 1
ani1 = animation.FuncAnimation(f1,animate, interval = 1000)
app.mainloop()
【问题讨论】:
-
您的缩进显示已损坏。不清楚
def animate(i):里面有多少代码 -
@BryanOakley 感谢您的回复,可能在复制文本时出错。 animate(i) 里面的缩进很好。
-
我想这是触发动画的那一行:
ani1 = animation.FuncAnimation(f1,animate, interval = 1000)。那应该在按钮命令中移动。而在 button1 命令中,您的lambda:animate()似乎错过了i参数。 -
@Jean-FrançoisFabre 我几乎尝试了所有方法。是的 (i) 也不返回任何东西。我尝试将整个命令移动到按钮命令中,但没有运气.. 有什么方法可以在按钮命令中移动 'ani1 = animation.FuncAnimation(f1,animate, interval = 1000)' 以便它开始打印图形只有当它被要求时..?
标签: python user-interface animation matplotlib tkinter