【问题标题】:Exception in Tkinter callbackTkinter 回调中的异常
【发布时间】:2015-07-10 18:02:18
【问题描述】:

我编写了一个代码来从我的 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()

我需要帮助!!非常感谢

【问题讨论】:

  • 您好,欢迎来到 StackOverflow。请花一些时间阅读帮助页面,尤其是名为"What topics can I ask about here?""What types of questions should I avoid asking?" 的部分。更重要的是,请阅读the Stack Overflow question checklist。您可能还想了解Minimal, Complete, and Verifiable Examples
  • 您必须告诉我们实际的错误,我们才能为您提供帮助。
  • 这里是第 1 部分,因为它很长:Tkinter 回调回溯中的异常(最近一次调用最后一次):文件“/usr/lib/python2.7/lib-tk/Tkinter.py”,第 1489 行,在 call 中返回 self.func(*args) 文件“/home/hamzawi/Documents/Python_projects/potentioPython/pot.py”,第 13 行,按analoglabel.config(text=str(val) )
  • 这是第 2 部分:文件“/usr/lib/python2.7/lib-tk/Tkinter.py”,第 1274 行,在配置中返回 self._configure('configure', cnf, kw)文件“/usr/lib/python2.7/lib-tk/Tkinter.py”,第 1265 行,在 _configure self.tk.call(_flatten((self._w, cmd)) + self._options(cnf) ) TclError: 无效的命令名“.3065381132L”
  • @HamzaBoughraira:请编辑您的问题以包含该信息。无法在评论中阅读。

标签: python python-2.7 tkinter arduino firmata


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    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()
    

    【讨论】:

    • 请考虑编辑您的帖子,以添加更多关于您的代码的作用以及它为何能解决问题的说明。一个大部分只包含代码的答案(即使它正在工作)通常不会帮助 OP 理解他们的问题。另外请查看重新格式化您的代码,似乎缩进可能已关闭,但那里还有很多不必要的空行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多