我想把串行值读入我的Tkinter图形用户界面。或者进入一个文本窗口,或者最终到一个每隔一秒更新一次的文本标签小部件。我遇到的问题是队列类。我得到的错误是:
AttributeError:“application”对象没有属性“queue”
这是我的代码:#!/usr/bin/env python
from tkinter import *
from tkinter import messagebox
from time import sleep
import picamera
import os
import serial
import sys
import RPi.GPIO as GPIO
import _thread
import threading
import random
import queue
# Setup GPIO pin(s)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, False)
#==============================================================
# Declaration of Constants
# none used
#==============================================================
class SerialThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue.Queue()
def read_sensor_values(self):
ser = serial.Serial('/dev/ttyUSB0', 9600)
while True:
if ser.inWaiting:
text = ser.readline(s.inWaiting)
self.queue.put(text)
self.pressures_txt.insert(0.0,values)
class Application(Frame):
""" GUI Application for taking photos. """
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
self.setup_camera()
def create_widgets(self):
Checkbutton( self, text = "Read Pressure Values", variable = self.mode2, command = self.process_serial, bg = 'white').grid(row = 4, column = 0, sticky = W+E+N+S)
# create text field to display pressure values from arduino
self.pressures_txt = Text(self, height = 3, wrap = WORD)
self.pressures_txt.grid(row=9, column = 0, columnspan =3)
def process_serial(self):
#self.text.delete(1.0, END)
while self.queue.qsize():
try:
self.text.insert(END, self.queue.get())
self.pressures_txt.insert(0.0, self.queue.get())
except queue.Empty:
pass
self.after('1000', self.process_serial)
#................. end of method: read_sensor_values ................
#=================================================================
# main
#=================================================================
root = Tk() # Create the GUI root object
root.title("Control V1.0")
app = Application(root) # Create the root application window
root.mainloop()
我发布的代码是整个程序的缩写版本。我删除了那些被认为不相关的部分。我穿着Python3跑。我怀疑我的缩进可能有错误,但我不确定。在
我正在使用以下链接中的代码进行我的系列阅读课: