所以我通过RF收发器(NRF24L01)从Arduino到我的Raspberry Pi有一些值,我可以在程序运行时显示整数(Python)。现在我想在我的GUI中显示那些我用单独的python脚本编写的整数值。我这样做有困难。我已经尝试从GUI导入它们,但它无法正常工作,我无法弄清楚为什么......
所以现在我已经选择将值写入传输脚本中的文本文件,然后尝试从GUI脚本中的文本文件中读取值,但它仍然可以完全正常工作。
任何人都可以帮我更新传输脚本中的文本文件并从GUI脚本中读取它吗?你能从一个脚本写入文本文件并同时从另一个脚本中读取文本文件吗?
非常感谢任何帮助。谢谢!
PS。如果我错过了你需要知道的任何事情,那就问问。解释一切都有点困难!
GUI代码
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 6 20:05:30 2016
@author: s
"""
import sys
if sys.version_info[0] < 3:
import Tkinter as tk
else:
import tkinter as tk
def clear():
pass
def exit_():
root.quit()
root.withdraw()
#water_amount = 0
water_cost = 0
total_water_amount = 0
total_water_cost = 0
def show_data():
while True:
text_file = open("Output.txt", "r")
water_amount = text_file.readlines()
text_file.close()
tk.Label(root, text='Water Amount: ' + str(water_amount)).pack()
tk.Label(root, text='Water Cost: ' + str(water_cost)).pack()
separator = tk.Frame(height=2, bd=10, relief=tk.SUNKEN)
separator.pack(fill=tk.X, padx=5, pady=5)
tk.Label(root, text='Total Water Amount: ' + str(total_water_amount)).pack()
tk.Label(root, text='Total Water Cost: ' + str(total_water_cost)).pack()
separator = tk.Frame(height=2, bd=10, relief=tk.SUNKEN)
separator.pack(fill=tk.X, padx=5, pady=5)
#show_data()
def get_rate():
import random
for i in range(100):
flow_rate.append(random.randint(20, 60))
# print(flow_rate)
# def draw_plot(flow_rate):
# import matplotlib.pyplot as plt
# conda install matplotlib
# print(flow_rate)
# plt.plot(flow_rate, label='Flow rate ml/sec')
# plt.xlabel('Time(sec)')
# plt.ylabel('Flow Rate(ml)')
# plt.title("Flow Rate Chart")
#
# plt.legend()
# plt.show()
root = tk.Tk(className='Water')
flow_rate = []
get_rate()
show_data()
tk.Button(root, text='Clear', command=clear).pack(side='left')
tk.Button(root, text='Exit', command=exit_).pack(side='left')
#tk.Button(root, text='Draw', command=draw_plot(flow_rate)).pack_forget()
root.mainloop()
代码接收价值
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0,17)
radio.setPayloadSize(32) #can have maximum 32
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS) #Slower since it is secure
radio.setPALevel(NRF24.PA_MIN) # Minimum to save battery
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload() #Acknowledgement Payload : Can verify if data received
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
radio.startListening()
while True:
# Waits to recieve data, if no data is recieved then goes into sleep mode
while not radio.available(0):
time.sleep(1/100)
receivedMessage = []
#Populates the message
radio.read(receivedMessage, radio.getDynamicPayloadSize())
#-------------------------------------------------------
raw = int(receivedMessage[1]) * 256
total = raw + int(receivedMessage[0])
print ("total equals:" + str(int(total)))
text_file = open("Output.txt", "w")
text_file.write("%s" % total)
text_file.close()
答案 0 :(得分:1)
您应该尝试将数据接收代码和数据显示代码与threading
库结合使用。
在数据接收脚本的while True
循环中,它应检查新结果并以某种方式通知GUI线程(例如,将其存储在全局变量中并使用threading.Condition
对象),或直接更改GUI。
例如:
from tkinter import *
import threading
tk=Tk()
result=StringVar()
Label(tk,textvariable=result).pack()
def update_result():
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0,17)
radio.setPayloadSize(32) #can have maximum 32
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS) #Slower since it is secure
radio.setPALevel(NRF24.PA_MIN) # Minimum to save battery
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload() #Acknowledgement Payload : Can verify if data received
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
radio.startListening()
while True:
while not radio.available(0):
time.sleep(1/100)
receivedMessage = []
#Populates the message
radio.read(receivedMessage, radio.getDynamicPayloadSize())
#-------------------------------------------------------
raw = int(receivedMessage[1]) * 256
total = raw + int(receivedMessage[0])
result.set(total)
threading.Thread(target=update_result).start()
mainloop()
(我没有测试这个程序,因为我没有环境,但我认为这应该有效。请评论它是否无效。)