我想将序列值读入我的Tkinter GUI。无论是进入文本窗口还是最终进入文本标签小部件,每小时更新一次。我遇到的问题是队列类。我得到的错误是: AttributeError:'Applcation'对象没有属性'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中运行。我怀疑我的缩进可能有误,但我不确定。
我正在使用以下链接中的代码进行序列阅读课程:
答案 0 :(得分:0)
问题是self.queue
中的read_sensor_values
属于SerialThread
个对象。当您在self.queue
对象的process_serial
方法中说Application
时,它会引用queue
对象的不存在的Application
属性。也许您应该将SerialThread
对象作为Application
对象的属性。然后process_serial
可以引用self.serial.queue
或其他任何名称。