我编写了一个代码,用于从我的arduino中读取模拟值,并在一个简单的Python应用程序中监视结果,但当我点击我的应用程序中的“start”按钮时,出现此错误:Tkinter回调中的异常 追溯(最近的呼叫最后一次)
并且编译器找到了此instraction中的错误:analoglabel.config(text = str(pin.read()))
抱歉,我没有填写所有错误消息这是我的代码:
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()
请我帮忙!非常感谢
答案 0 :(得分:0)
第一次通过你的循环一切都很顺利。然后,您销毁根窗口。下次循环时,您尝试配置标签小部件,但它已被销毁。
您确定要销毁循环内的根窗口吗?
答案 1 :(得分:-3)
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()