Я использую Python3 с TKinter и столкнулся с проблемой, даже после прочтения форумов и веб-сайта TKdocs я все еще не добиваюсь успеха. Я получаю показания температуры через мой com-порт. Моя программа до сих пор имеет стартовую страницу и страницу с графиком, который обновляется при каждом чтении. Так что вопрос в том, как я могу распечатать данные датчика на первой странице, я новичок в 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()