将值从一个python脚本发送到另一个python脚本

将值从一个python脚本发送到另一个python脚本,python,tkinter,Python,Tkinter,因此,我有一些值通过射频收发器(NRF24L01)从Arduino传入Raspberry Pi,我可以在程序运行时显示整数(Python)。现在,我想在GUI中显示我用单独的python脚本编写的那些整数值。我这样做有困难。我尝试过从GUI导入它们,但它不起作用,我也不知道为什么 因此,现在我选择将值写入传输脚本中的文本文件,然后尝试从GUI脚本中的文本文件中读取值,但仍然无法完全工作 有人能帮我更新传输脚本中的文本文件并从GUI脚本中读取它吗?可以从一个脚本写入文本文件,同时从另一个脚本读取文

因此,我有一些值通过射频收发器(NRF24L01)从Arduino传入Raspberry Pi,我可以在程序运行时显示整数(Python)。现在,我想在GUI中显示我用单独的python脚本编写的那些整数值。我这样做有困难。我尝试过从GUI导入它们,但它不起作用,我也不知道为什么

因此,现在我选择将值写入传输脚本中的文本文件,然后尝试从GUI脚本中的文本文件中读取值,但仍然无法完全工作

有人能帮我更新传输脚本中的文本文件并从GUI脚本中读取它吗?可以从一个脚本写入文本文件,同时从另一个脚本读取文本文件吗

任何帮助都将不胜感激。谢谢

另外,如果我错过了你需要知道的任何事情,尽管问。解释一切都有点难

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()

您应该尝试将数据接收代码和数据显示代码与
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()

(我没有测试这个程序,因为我没有环境,但我认为它应该可以工作。如果它不工作,请发表评论。)

什么是“不工作”和“仍然不工作”的意思?这个答案显示了一种比使用睡眠更好的轮询传感器的方法:你可能想看看是否会做你想做的事情。您将不得不修改该答案中的代码,因为您使用的是两个单独的脚本。因此,您的意思是,我现在可以使用update\u result函数中的线程功能?
global total
,从GUI脚本调用“total”,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()