Search code examples
pythonpython-2.7tkinterarduinofirmata

Exception in Tkinter callback


I wrote a code to read an analog value from my arduino and monitor the results in a simple Python application, but when I hit the button "start" in my app, this error appeared: Exception in Tkinter callback Traceback (most recent call last)

and the the compiler located the error in this instraction: analoglabel.config(text=str(pin.read()))

sorry I coudn't fill all the error message

and this is my code:

import pyfirmata
from pyfirmata import util
import Tkinter
from time import sleep

def press():
  it = util.Iterator(board)
  it.start()
  while True:
    if flag.get():
        analoglabel.config(text=str(pin.read()))
        analoglabel.update_idletasks()
        root.update()
    else:
        break
    board.exit()
    root.destroy()

def exit_command():
 flag.set(False)

port = '/dev/ttyACM0'
board = pyfirmata.Arduino(port)
sleep(5)
pin = board.get_pin('a:0:i')

root = Tkinter.Tk()
root.title("Analog read by hamzawi")
root.minsize(300, 50)

startbutton = Tkinter.Button(root,
                          text="START",
                          command=press)
startbutton.grid(column=1, row=2)

exitbutton = Tkinter.Button(root,
                        text="EXIT",
                        command=exit_command)
exitbutton.grid(column=2, row=2)

label = Tkinter.Label(root,
                   text="The value is: ")
label.grid(column=1, row=1)

analoglabel = Tkinter.Label(root, text=" ")
analoglabel.grid(column=2, row=1)

flag = Tkinter.BooleanVar()
flag.set(True)

root.mainloop()

please I need help!! and thanks a lot


Solution

  • from tkinter import *
    
    import sys
    
    import tkinter
    
    from time import sleep
    
    import pyfirmata # not in wondows
    
    from pyfirmata import util # not in wondows
    
    def press():
    
      it = util.Iterator(board)
    
      it.start()
    
      while True:
    
        if flag.get():
    
            analoglabel.config(text=str(pin.read()))
    
            analoglabel.update_idletasks()
    
            root.update()
    
        else:
    
            break
    
        board.exit()
    
        root.destroy()
    
    def exit_command():
    
     flag.set(False)
    
    port = '/dev/ttyACM0'
    
    board = pyfirmata.Arduino(port) # not in wondows
    
    sleep(5)
    
    pin = board.get_pin('a:0:i') # not in wondows
    
    root = tkinter.Tk()
    
    root.title("Analog read by hamzawi")
    
    root.minsize(300, 50)
    
    start_button = tkinter.Button(root, text='start', command=press)
    
    start_button.grid(column=1, row=2)
    
    exit_button = tkinter.Button(root, text="EXIT", command=exit_command)
    
    exit_button.grid(column=2, row=2)
    
    label = tkinter.Label(root,
    
                       text="The value is: ")
    
    label.grid(column=1, row=1)
    
    analoglabel = tkinter.Label(root, text=" ")
    
    analoglabel.grid(column=2, row=1)
    
    flag = tkinter.BooleanVar()
    
    flag.set(True)
    
    root.mainloop()