tkinter命令名称无效

时间:2018-03-27 20:13:06

标签: python tkinter

当我点击程序中的退出按钮时,出现类似于invalid command name ".91418712.91418792"的错误。我用谷歌搜索了这个,但不确定如何解决这个问题。

问题显然是我的线程正在尝试更新不再存在的GUI元素,这就是我在进行销毁之前进入睡眠状态的原因。 以下内容与我的问题_tkinter.TclError: invalid command name ".4302957584"

密切相关

代码的精简版本:

#!/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()

1 个答案:

答案 0 :(得分:0)

不确定究竟修复了什么,但这不再产生错误

#!/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()

谢谢大家的所有投入!! :)