arduinoからシリアルデータ配列を取得し、それらをRaspberry Pi 3のPython3でラベルの座標として使用しようとしています。シリアルで配列を取得し、必要に応じてラベルを配置できます。プログラムは動作します。しかし、最初の問題は、ちらつきが見られ、ちらつきの遅延時間が時間とともに最悪になっていることです。2番目の問題は、プログラムが数分後に動作を停止することです。
ser.flushInput()を試しましたが、どこでも配列をクリアしましたが、結果は同じです。
from tkinter import *
import serial
root = Tk()
root.geometry("1024x600")
ser = serial.Serial('/dev/ttyAMA0',115200)
ser.timeout=None
data=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
data1=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
image=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
label=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
x1=[210,210,210,370,370,370,530,530,530,690,690,690,850,850,850,850]
y1=[440,325,75,440,325,75,440,325,75,440,325,75,440,325,200,75]
image_gri=PhotoImage(file="/home/pi/python/gri.gif")
image_kirmizi = PhotoImage(file="/home/pi/python/kirmizi.gif")
image_yesil = PhotoImage(file="/home/pi/python/yesil.gif")
def kontrol():
data=[]
ser.flush()
ser.flushInput()
data = ser.read(32)
#print(data)
for i in range(0,16):
if data[i]==49 and data[i+16]==49:
image[i]=image_yesil
label[i] = Label(root, image=image[i])
label[i].place(x=x1[i],y=y1[i])
elif data[i]==49 and data[i+16]==48:
image[i]=image_kirmizi
label[i] = Label(root, image=image[i])
label[i].place(x=x1[i],y=y1[i])
else :
image[i]=image_gri
label[i] = Label(root, image=image[i])
label[i].place(x=x1[i],y=y1[i])
ser.flush()
ser.flushInput()
root.after(50,kontrol)
root.update()
kontrol()
#root.after(50,kontrol)
mainloop()
プログラムが最大12時間動作し、動作中にちらつきが見られないようにしたい。
問題は、実行ごとに16個のラベルウィジェットを追加していることです。これらのウィジェットはメモリに保持され、毎回処理されます。古いラベルが削除されることはありません。
これを解決するには、を使用して、新しいラベルを作成するときに前のラベルを削除できますlabel[i].destroy()
。さらに良いのは、を使用して新しいラベルを作成するのではなく、既存のラベルを更新することlabel[i].config(image=...)
です。
以下は完全に機能する例です(一部のコードがコメントアウトされ、代わりにランダムデータとダミーイメージが使用されているため、Python 3を使用している人は誰でも外部依存関係なしでこれを実行できます):
import random
from tkinter import *
# import serial
root = Tk()
root.geometry("1024x600")
# ser = serial.Serial('/dev/ttyAMA0', 115200)
# ser.timeout = None
# If you have the images, use this:
#IMAGE_GRI = PhotoImage(file="/home/pi/python/gri.gif")
#IMAGE_KIRMIZI = PhotoImage(file="/home/pi/python/kirmizi.gif")
#IMAGE_YESIL = PhotoImage(file="/home/pi/python/yesil.gif")
# Otherwise, create some dummy images for demonstration purposes:
WIDTH = 64
HEIGHT = 64
DATA = ','.join(['0x00' for i in range(WIDTH * HEIGHT // 8)])
BITMAP = '#define im_width %d\n#define im_height %d\nstatic char im_bits[] = {\n%s\n};' % (WIDTH, HEIGHT, DATA)
IMAGE_GRI = BitmapImage(data=BITMAP, background="grey")
IMAGE_KIRMIZI = BitmapImage(data=BITMAP, background="red")
IMAGE_YESIL = BitmapImage(data=BITMAP, background="green")
# Initialize with grey images (you may choose a different image, of course)
label = [Label(root, image=IMAGE_GRI) for i in range(16)]
x1 = [210, 210, 210, 370, 370, 370, 530, 530, 530, 690, 690, 690, 850, 850, 850, 850]
y1 = [440, 325, 75, 440, 325, 75, 440, 325, 75, 440, 325, 75, 440, 325, 200, 75]
# Place the images only once:
for i in range(16):
label[i].place(x=x1[i], y=y1[i])
def kontrol():
data = []
# ser.flush()
# ser.flushInput()
# data = ser.read(32)
# Create random data for demonstration purposes:
data = [random.choice((48, 49)) for i in range(32)]
#print(data)
for i in range(16):
if data[i] == 49 and data[i+16] == 49:
label[i].config(image=IMAGE_YESIL) # update image!
elif data[i] == 49 and data[i+16] == 48:
label[i].config(image=IMAGE_KIRMIZI) # update image!
else:
label[i].config(image=IMAGE_GRI) # update image!
# ser.flush()
# ser.flushInput()
root.after(50, kontrol)
# root.update() # Not necessary! Use root.update_idletasks() if necessary.
kontrol()
mainloop()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加