-2

我编写了一个代码来从我的 arduino 读取模拟值并在一个简单的 Python 应用程序中监视结果,但是当我在我的应用程序中点击“开始”按钮时,出现了这个错误:Tkinter 回调 Traceback 中的异常(最近一次调用最后一次)

并且编译器在此指令中找到了错误: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()

请我需要帮助!非常感谢

4

2 回答 2

0

第一次通过你的循环一切都很好。然后销毁根窗口。下次通过循环时,您尝试配置标签小部件,但它已被破坏。

您确定要销毁循环内的根窗口吗?

于 2015-07-10T19:39:15.073 回答
-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()
于 2015-09-01T09:27:19.727 回答