简体   繁体   English

tkinter无效的命令名称

[英]tkinter invalid command name

I get an error similar to: invalid command name ".91418712.91418792" when I click the Quit button in my program. 单击程序中的退出按钮时,收到类似于以下错误: invalid command name ".91418712.91418792" I have googled this but an unsure how to fix the issue. 我已经用谷歌搜索,但是不确定如何解决该问题。

The issue apparently is that my thread is trying to update GUI elements that no longer exists which is why I put a sleep in before doing the destroy. 问题显然是我的线程正在尝试更新不再存在的GUI元素,这就是为什么在进行销毁之前要进行睡眠。 The following is closely related to my issue _tkinter.TclError: invalid command name ".4302957584" 以下与我的问题_tkinter.TclError密切相关:无效的命令名称“ .4302957584”

A condensed version of the code: 代码的精简版:

#!/usr/bin/python
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.FT232H as FT232H
from time import sleep
from threading import Thread
import Tkinter as tk
import tkFont

# Temporarily disable the built-in FTDI serial driver on Mac & Linux platforms.
FT232H.use_FT232H()

# Create an FT232H object that grabs the first available FT232H device found.
device = FT232H.FT232H()

d0 = 0
d1 = 1

device.setup(d0, GPIO.IN)
device.setup(d1, GPIO.IN)

class Application():
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Show Voltage")
        self.root.grid()
        self.createWidgets()
        self.stop = False
        self.watcher = Thread(target = self.watcher, name="watcher")
        self.watcher.start()
        self.root.mainloop()

    def changeBackground(self, cell):
        if cell == 0:
            self.la_d0.config(bg="green")
        elif cell == 1:
            self.la_d1.config(bg="green")

    def returnBackground(self, cell):
        if cell == 0:
            self.la_d0.config(bg="black")
        elif cell == 1:
            self.la_d1.config(bg="black")

    def quit(self):
        self.stop = True
        self.watcher.join()
        sleep(0.3)
        self.root.destroy()

    def watcher(self):
        while not self.stop:
            self.wa_d0 = device.input(d0)
            self.wa_d1 = device.input(d1)

            if self.wa_d0 == GPIO.HIGH:
                self.changeBackground(0)
            else:
                self.returnBackground(0)

            if self.wa_d1 == GPIO.HIGH:
                self.changeBackground(1)
            else:
                self.returnBackground(1)
            sleep(0.0125)

    def createWidgets(self):
        self.font = tkFont.Font(family='Helvetica', size=50, weight='bold')
        self.bt_font = tkFont.Font(family='Helvetica', size=18, weight='bold')

        self.fr_d0 = tk.Frame(height=100, width=100)
        self.la_d0 = tk.Label(self.fr_d0, text="d0", bg="black", fg="red", font=self.font)
        self.fr_d1 = tk.Frame(height=100, width=100)
        self.la_d1 = tk.Label(self.fr_d1, text="d1", bg="black", fg="red", font=self.font)
        self.fr_quit = tk.Frame(height=40, width=200)
        self.bt_quit = tk.Button(self.fr_quit, text="Quit!", bg="white", command=self.quit, font=self.bt_font)

        self.fr_d0.grid(row=0, column=0)
        self.fr_d0.pack_propagate(False)
        self.la_d0.pack(fill="both", expand=1)
        self.fr_d1.grid(row=0, column=2)
        self.fr_d1.pack_propagate(False)
        self.la_d1.pack(fill="both", expand=1)
        self.fr_quit.grid(row=1, column=0, columnspan=3)
        self.fr_quit.pack_propagate(False)
        self.bt_quit.pack(fill="both", expand=1)

app = Application()

not sure exactly what fixed it but this no longer creates the error 不知道到底是什么修复了它,但这不再产生错误

#!/usr/bin/python
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.FT232H as FT232H
from time import sleep
from threading import Thread
import Tkinter as tk
import tkFont

# Temporarily disable the built-in FTDI serial driver on Mac & Linux platforms.
FT232H.use_FT232H()

# Create an FT232H object that grabs the first available FT232H device found.
device = FT232H.FT232H()

d0 = 0
d1 = 1

device.setup(d0, GPIO.IN)
device.setup(d1, GPIO.IN)

class Application():
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Show Voltage")
        self.root.grid()
        self.createWidgets()
        self.stop = False
        self.watcherThread = Thread(target = self.watcher, name="watcher")
        self.watcherThread.start()
        self.root.mainloop()

    def changeBackground(self, cell):
        if cell == 0:
            self.la_d0.config(bg="green")
        elif cell == 1:
            self.la_d1.config(bg="green")

    def returnBackground(self, cell):
        if cell == 0:
            self.la_d0.config(bg="black")
        elif cell == 1:
            self.la_d1.config(bg="black")

    def destroy(self):
        self.root.destroy()

    def quit(self):
        self.stop = True
        self.watcherThread.join()
        self.root.after(300, self.destroy)

    def watcher(self):
        while not self.stop:
            self.wa_d0 = device.input(d0)
            self.wa_d1 = device.input(d1)

            if self.wa_d0 == GPIO.HIGH:
                self.changeBackground(0)
            else:
                self.returnBackground(0)

            if self.wa_d1 == GPIO.HIGH:
                self.changeBackground(1)
            else:
                self.returnBackground(1)
            sleep(0.0125)

    def createWidgets(self):
        self.font = tkFont.Font(family='Helvetica', size=50, weight='bold')
        self.bt_font = tkFont.Font(family='Helvetica', size=18, weight='bold')

        self.fr_d0 = tk.Frame(height=100, width=100)
        self.la_d0 = tk.Label(self.fr_d0, text="d0", bg="black", fg="red", font=self.font)
        self.fr_d1 = tk.Frame(height=100, width=100)
        self.la_d1 = tk.Label(self.fr_d1, text="d1", bg="black", fg="red", font=self.font)
        self.fr_quit = tk.Frame(height=40, width=200)
        self.bt_quit = tk.Button(self.fr_quit, text="Quit!", bg="white", command=self.quit, font=self.bt_font)

        self.fr_d0.grid(row=0, column=0)
        self.fr_d0.pack_propagate(False)
        self.la_d0.pack(fill="both", expand=1)
        self.fr_d1.grid(row=0, column=2)
        self.fr_d1.pack_propagate(False)
        self.la_d1.pack(fill="both", expand=1)
        self.fr_quit.grid(row=1, column=0, columnspan=3)
        self.fr_quit.pack_propagate(False)
        self.bt_quit.pack(fill="both", expand=1)

app = Application()

thank you all for all the input!! 谢谢大家的投入!! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM