我是Python编程的新手,我正在做这个学习项目。我的问题是,当我启动应用程序时,会显示MainScreen
,但之后没有加载其他框架。这些功能分开工作。 Animate
和texting
函数在自行执行时可以正常工作。我试图将它们放在一个应用程序中。
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
from time import sleep
import serial
from Tkinter import *
FO = open("SensorValues.txt", "w+")
ser = serial.Serial()
ser.baudrate = 9600
ser.port = '/dev/ttyUSB0'
ser.open()
data = ser.readline(5)
FO.write(data + '\n')
FO.flush()
def organizer(frame):
frame.tkraise()
root = Tk()
MainScreen = Frame(root)
LiveGraph = Frame(root)
LiveLabel = Frame(root)
for frame in (MainScreen,LiveGraph,LiveLabel):
frame.grid(row=0,column=0,sticky='news')
Label(MainScreen,text = 'Choose what do you want').pack()
def animate(i):
fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
sleep(1)
dataArray = FO.split('\n')
xar = []
yar = []
for eachLine in dataArray:
if len(eachLine)>1:
x,y = eachLine.split(',')
xar.append(int(x))
yar.append(int(y))
ax1.clear()
ax2.clear()
ax1.plot(xar)
ax2.plot(yar)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
def texting():
LiveLabel.config(text=data)
root.after(1, texting)
organizer(MainScreen)
Button(MainScreen, text = 'Press for starting live graph!', width = 30, command = lambda:organizer(LiveGraph)).pack()
Button(MainScreen, text = 'Press for starting live label!', width = 30, command = lambda:organizer(LiveLabel)).pack()
Label(LiveGraph,text = '1st Table Temperature - 2nd Table Humidity').pack()
Button(LiveGraph,text = 'Stop',width = 10, command = root.destroy ).pack()
Button(LiveLabel,text = 'Stop',width = 10, command = root.destroy ).pack()
plt.show()
FO.close()
ser.close()
root.mainloop()
截图: