天天看點

python tkinter怎麼讀_Python Tkinter:讀取序列值

我想把串行值讀入我的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跑。我懷疑我的縮進可能有錯誤,但我不确定。在

我正在使用以下連結中的代碼進行我的系列閱讀課: