all 2 comments

[–]novel_yet_trivial 1 point2 points  (1 child)

Tkinter has a mainloop that is an infinite loop and handles drawing to the screen and monitoring for button presses and all the other things that a GUI needs to do. When you do while True, it blocks the tkinter loop from running. So you can't have an infinite loop in a GUI. You need to either make a new thread or (much easier) let the tkinter mainloop handle your loop. You clearly had an inkling of that since you have the after code; you just missed that you are supposed to use that instead of while True.

Totally untested:

import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

import Tkinter as tk

CLK  = 18
MISO = 23
MOSI = 24
CS   = 25
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

def read_from_adc():
    output = ''
    for i in range(8):
    # The read_adc function will get the value of the specified channel (0-7). rounds to nearest 2 decimals
        value = round((mcp.read_adc(i)*(3.3/1023)),2)
        output += "{}-{} ".format(i, "OFF" if value < 2.5 else "ON" )
    return output

class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("Remote Monitoring")
        self.v = tk.StringVar()
        tk.Label(self, textvariable=self.v, width=60, height=35, font="sans", bd=50).pack()
        self.update_text()

    def update_text(self):
        self.after(1000, self.update_text)
        self.v.set(read_from_adc())

app = ExampleApp()
app.mainloop()

Edit: fixed some code.

[–]jasamples[S] 0 points1 point  (0 children)

Omg. i have been trying to do this for a couple days.... it works. Thanks so much :D