0

I'm trying to generate a tkinter label on PageTwo that will display a thermocouple input every so often (1000 ms). I've gotten the sensor to plot to a live matlibplot on PageThree but I seem to be lost as to where I need to put function readSensor() and where to call the tk.IntVar() for the label to display on PageTwo. My current code throws an error saying "NameError: var is not defined" on PageTwo. I'm assuming that's because the PageTwo class can't see the variables from class thermo? When I tried putting the function in the PageTwo class the variable always printed 0. I found many fixes but none seem to be for multiple page tkinter code.

Any direction would be very helpful. I'd like to try to use this object oriented format for a GUI, but I seem to be struggling with the class definitions.

import time
import board
import digitalio
import adafruit_max31855
import matplotlib
import matplotlib.pyplot as plt
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 tkinter as tk


LARGE_FONT= ("Veranda", 12)
style.use("ggplot")


f = Figure(figsize=(5,5), dpi=100)
a = f.add_subplot(111)
xList = []
yList = []

def animate(i):
    spi = board.SPI()
    cs = digitalio.DigitalInOut(board.D5)
    max31855 = adafruit_max31855.MAX31855(spi, cs)
    dataList = int(max31855.temperature)

    xList.append(int(i))
    yList.append(int(dataList))
    
    a.clear()
    a.plot(xList,yList)

class thermo(tk.Tk):
  
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        
        self.frames = {}
        
        for F in (StartPage, PageOne, PageTwo, PageThree):
        
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        
        self.show_frame(StartPage)
        self.temp1 = tk.IntVar()
        self.TimerInterval = 1000
        self.temp1.set(data2)
        self.var = self.after(self.TimerInterval, self.readSensor)
         
    def readSensor():
        spi = board.SPI()
        cs = digitalio.DigitalInOut(board.D5)
        max31855 = adafruit_max31855.MAX31855(spi, cs)
        data2 = int(max31855.temperature)

        
    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)
        
        label = tk.Label(self, text="Start Page", font = LARGE_FONT)
        label.pack(pady=10, padx=10)
        
        button = tk.Button(self, text="Visit Page 1",
                          command=lambda: controller.show_frame(PageOne))
        
        button.pack()
        
        button2 = tk.Button(self, text="Vist Page 2",
                            command=lambda: controller.show_frame(PageTwo))
        
        button2.pack()
        
        button3 = tk.Button(self, text="Vist Page 3",
                            command=lambda: controller.show_frame(PageThree))
        
        button3.pack()

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page 1", font = LARGE_FONT)
        label.pack(pady=10, padx=10)
        
        button = tk.Button(self, text="Back to Home",
                          command=lambda: controller.show_frame(StartPage))
        button.pack()
        
        button2 = tk.Button(self, text="Vist Page 2",
                            command=lambda: controller.show_frame(PageTwo))
        
        button2.pack()
        
        
class PageTwo(tk.Frame):
    
    def __init__(self, parent, controller):
        
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="PageTwo", font = LARGE_FONT)
        label.pack(pady=10, padx=10)
                     
        button = tk.Button(self, text="Back to Home",
                          command=lambda: controller.show_frame(StartPage))
        button.pack()
        
        button2 = tk.Button(self, text="Vist Page 1",
                            command=lambda: controller.show_frame(PageOne))
        
        button2.pack()
        label2 = tk.Label(self, text='sensor1', font = LARGE_FONT)
        label2.pack(pady=20, padx=10)
        
        label2Display = tk.Label(self,textvariable=var.get())
        label2Display.pack(pady=40, padx=20)
        
        label3 = tk.Label(self, text='test', font = LARGE_FONT)
        label3.pack(pady=30, padx=30)
    
class PageThree(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="PageThree", font = LARGE_FONT)
        label.pack(pady=10, padx=10)
        
        button = tk.Button(self, text="Back to Home",
                          command=lambda: controller.show_frame(StartPage))
        button.pack()
        
        button2 = tk.Button(self, text="Vist Page 1",
                            command=lambda: controller.show_frame(PageOne))
        
        button2.pack()
        
         
        button3 = tk.Button(self, text="Vist Page 2",
                            command=lambda: controller.show_frame(PageOne))
        
        button3.pack()
        
        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 = thermo()
ani = animation.FuncAnimation(f, animate, interval=1000)
app.mainloop()
  • 1
    I recommend you look at all of the linked questions from here: https://stackoverflow.com/a/7557028/7432 – Bryan Oakley Feb 06 '22 at 21:47
  • Thank you so much for the direction. My code has changed significantly to troubleshoot, so now I have a function in my top class to read a .txt file. I was able to set up a shared dictionary in my top class (per your links) to save a variable from the .txt and call it on my PageOne class. It displays the label IntVar. But the `self.label1.after(1000, self.controller.poll)' doesn't update the label. It only updates after closing the program and reopening. – damping_wiggler Apr 15 '22 at 01:31
  • thanks to another one of your replies I was able to resolve the issue [stackoverflow.com/questions/43301371/recursion-error-using-tkinter-root-after](https://stackoverflow.com/questions/43301371/recursion-error-using-tkinter-root-after) – damping_wiggler Apr 16 '22 at 17:13

0 Answers0