我尝试用Python中的按钮激活函数dataBridge
使用Tkinter,但是当我按下按钮但没有任何反应并且没有运行该函数。想法是当按下按钮ON时函数处于活动状态,但是当按下OFF按钮时,功能将停用。功能dataBridge
将发送请求并实时捕获数据。
from datetime import datetime
import serial
from xbee import XBee, ZigBee
from Tkinter import *
import tkMessageBox
import sys
import Tkinter
PORT = '/dev/ttyUSB0'
BAUD_RATE = 38400
# Apertura del puerto serial
ser = serial.Serial(PORT, BAUD_RATE, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=1, rtscts=1, dsrdtr=1)
# Creacion de objeto API para lectura del puerto serial
xbee = XBee(ser)
#xbee = XBee(ser,escaped=True)
#Etiquetas de direccion para cada Xbee
Addr_sensor1= "\x00\x13\xA2\x00\x40\x69\x18\xF5"
Addr_sensor2= "\x00\x13\xA2\x00\x40\x69\x18\xF8"
Addr_sensor3= "\x00\x13\xA2\x00\x40\x69\x16\x64"
a=bool
top = Tkinter.Tk()
top.title("Red ")
top.geometry('500x250')
def dataBridge( ):
while True:
try:
xbee.tx_long_addr(frame= '0x81',dest_addr=Addr_sensor1,data='A')
response = xbee.wait_read_frame()
receive= response['rf_data']
my_set= receive.split(",")
if my_set[0]=="nodo_3":
ele=0
f= open("sensor_3.txt.","a")
for ele in my_set:
f.write('%s,' % ele)
elif my_set[0]=="nodo_1":
el=0
f= open("sensor_1.txt.","a")
for el in my_set:
f.write('%s,' % el)
elif my_set[0]=="nodo_2":
e=0
f= open("sensor_2.txt.","a")
for e in my_set:
f.write('%s,' % e)
print my_set
time.sleep(0.03)
except:
break
def apertura():
dataBridge()
tkMessageBox.showinfo( "ON", "Sistema iniciado")
def cierre():
tkMessageBox.showinfo( "OFF", "Sistema apagado")
while True:
try:
break
except:
break
R = Text(top, height=20, width=55)
T = Label(top, text="Proyecto ")
T.pack()
R.insert(INSERT, "Oprima el boton de Activar para encender el sistema\nde lo contrario oprima Apagar\n")
R.pack()
B = Tkinter.Button(top, text ="Activar", command = apertura)
C = Tkinter.Button(top, text ="Apagar", command = cierre)
B.pack()
B.place(bordermode=OUTSIDE, height=100, width=50,x=150,y=100)
C.pack()
C.place(bordermode=OUTSIDE, height=100, width=50,x=300,y=100)