2016-08-22 64 views
2

因此,我通過RF收發器(NRF24L01)從Arduino進入了我的Raspberry Pi,我可以在程序運行時顯示整數(Python)。現在我想在我的GUI中顯示那些我用獨立的python腳本編寫的整數值。我遇到麻煩了。我試圖從GUI導入它們,但它不工作,我不知道爲什麼..將一個python腳本的值發送到另一個

所以,現在我已經選擇了將值寫入傳輸腳本中的文本文件然後嘗試從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() 
+1

什麼「不工作」和「仍然不工作」是什麼意思? –

+0

此答案顯示了輪詢傳感器比使用睡眠更好的方法:http://stackoverflow.com/a/37681471/7432 –

+0

您可能想查看是否[pipes](http://stackoverflow.com/a/ 3806342/3901060)會做你想做的。由於您使用兩個獨立的腳本,因此您必須修改該答案中的代碼。 – FamousJameous

回答

1

你應該嘗試接收數據的代碼和數據,顯示代碼threading庫相結合。

在數據接收腳本中的while True循環中,它應該檢查新結果並以某種方式通知GUI線程(例如,將其存儲在全局變量中並使用對象),或者直接更改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() 

(。我沒有測試這個計劃,因爲我沒有環境,但我認爲應該工作,請評論,如果它不工作)

+0

那麼你說什麼,我現在可以使用線程函數從GUI腳本中調用「total」? – George

+0

update_result函數中的'global total',並且GUI代碼應該能夠直接訪問變量 – xmcp

+0

它似乎工作!謝謝! – George

相關問題