Tkinter 多个页面上的串行数据



我正在使用Python3和TKinter,遇到了一个问题,即使在阅读了论坛和TKdocs网站之后,我仍然没有取得任何进展。我正在通过我的通讯端口接收温度读数。到目前为止,我的程序有一个开始页和一个带有图表的页面,该图表会随着每次阅读而更新。所以问题是我如何在第一页上打印传感器数据,我是tkinter的新手。

我将在下面发布代码,欢迎任何建议。

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, 
NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
import random
import sys
import time
import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *
import urllib
import json
import serial # import Serial Library
import numpy  # Import numpy
import pandas as pd
import numpy as np

LARGE_FONT= ("Verdana", 12)
style.use("ggplot") #ggplot...dark_background
do = []
tempF= []
f = Figure(figsize=(10,6), dpi=100)
a = f.add_subplot(111)
arduinoData = serial.Serial('com3', 115200) #Creating our serial object 

def animate(i):
if(arduinoData.inWaiting()>0):
#read serial data
arduinoString = arduinoData.readline()
xList = []
yList = []

#Parse serial data
arduinoString.split()
['', '', '', '', '', '', '', '']
words = arduinoString.split()
reading = words[3]
if words[1] == (b'TEMP') :
print (words[0])
print (words[1])
print (words[3])
tempF.append(reading)                  #Build our tempF array by appending temp readings
a.clear()
a.plot(*yList, *yList)   

title = "    D.O : "+str(do) + "n Temp : " + str(tempF)
a.set_title(title)


arduinoData.flushInput()
arduinoData.flushOutput()


class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Stylibleue Dashboard")

# the container is where we'll stack a bunch of frames on top each other
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
#Switch through pages
self.frames = {}
for F in (StartPage, Page1,):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()

class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#Page Labels
label = tk.Label(self, text=("""     D.O : 
"""), font=LARGE_FONT)
label.grid(row=100, column=20, sticky="nsew")
label = tk.Label(self, text=("""<Sensor reading here>
"""), font=LARGE_FONT)
label.grid(row=100, column=30, sticky="nsew")
label = tk.Label(self, text=(""" TEMP : 
"""), font=LARGE_FONT)
label.grid(row=100, column=40, sticky="nsew")
label = tk.Label(self, text=("""<Sensor reading here>        
"""), font=LARGE_FONT)
label.grid(row=100, column=50, sticky="nsew") 

#Go to Page1 button
button1 = ttk.Button(self, text="Page1",
command=lambda: controller.show_frame(Page1))
button1.grid(row=100, column=60, sticky="nsew")

class Page1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Bassin 2!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
#Return home button
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()

#This is the embedded matplotlib graph
canvas = FigureCanvasTkAgg(f, self)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2Tk(canvas, self)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)


app = Application()
ani = animation.FuncAnimation(f, animate, interval=1000)
app.mainloop()

起初我误解了这个问题,所以现在我正在重写答案。如果您仍然有任何疑问,或者这不是您所期望的,请在下面发表评论。我会尽力帮忙的。另外,我没有arduino来检查这个。

我对您的代码进行了以下更改:

class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#Page Labels
label = tk.Label(self, text=("""     D.O : 
"""), font=LARGE_FONT)
label.grid(row=100, column=20, sticky="nsew")
label = tk.Label(self, text=("""<Sensor reading here>
"""), font=LARGE_FONT)
label.grid(row=100, column=30, sticky="nsew")
label = tk.Label(self, text=(""" TEMP : 
"""), font=LARGE_FONT)
label.grid(row=100, column=40, sticky="nsew")
label = tk.Label(self, text=("""<Sensor reading here>        
"""), font=LARGE_FONT)
label.grid(row=100, column=50, sticky="nsew") 
# Reading data from the arduino
def DataRead():
msg = arduinoData.read(arduinoData.inWaiting()) # read everything in the input buffer
print ("Message from arduino: ")
print (msg)
button1 = ttk.Button(self, text="Print arduino data",
command=lambda: DataRead())
button1.grid()
#Go to Page1 button
button1 = ttk.Button(self, text="Page1",
command=lambda: controller.show_frame(Page1))
button1.grid(row=100, column=60, sticky="nsew")

最新更新