获取API数据并将其显示在GUI窗口中

时间:2018-09-11 22:45:30

标签: python api

我一般是python和编程新手。 我有两个问题: 1.我正在尝试构建一个GUI,向用户询问输入详细信息,以通过IP直接连接到设备并订阅其API。我已经设法编写了工作控制台代码,但是希望在GUI中使其更加用户友好。 2.我得到的输出需要正确解析并显示在输入窗口的底框中。

这是我到目前为止所拥有的...

import urllib.parse
import requests
import json
from websocket import create_connection

IP=input('Enter camera IP address:    ')
Type=input('Data or Event ? (Case sensitive):   ')
Data ={
  "messageType":"Subscription",
  "subscription":{
  "type":"Data",
"action":"Subscribe"
  }
}
Event={
  "messageType":"Subscription",
  "subscription":{
    "type":"Event",
    "action":"Subscribe"
  }
}
main_api = 'ws://'
API= '/api/subscriptions'
ws = create_connection(main_api + IP + API)
ws.connect(main_api + IP + API)
print ('Connected !')
if Type =='Data':
  ws.send(json.dumps(Data))
if Type =='Event':
  ws.send(json.dumps(Event))

while True:

    result = ws.recv()    
    jsondata = json.loads(result)
    print ("Web Socket data received : \n'%s'" % result)
    print('')

    ws.close()

结果:

输入摄像机IP地址:192.168.16.119 数据还是事件? (区分大小写):数据 连接的 ! 收到的Web套接字数据: '{'messageType':'Subscription','subscription':{'returnValue':'OK','type':'Data'}}'

收到的Web套接字数据: '{'dataNumber':'15751','intervalTime':'10','messageType':'Data','time':'2018-09-12T10:02:00.001 + 01:00','type': 'PresenceData','zone':[{'numVeh':'0','zoneId':'1','zoneOccupancy':'0'},{'numVeh':'0','zoneId':'2 ','zoneOccupancy':'0'}]}'

收到的Web套接字数据: '{'dataNumber':'15752','intervalTime':'10','messageType':'Data','time':'2018-09-12T10:02:10.030 + 01:00','type': 'PresenceData','zone':[{'numVeh':'1','zoneId':'1','zoneOccupancy':'6'},{'numVeh':'0','zoneId':'2 ','zoneOccupancy':'0'}]}'

收到的Web套接字数据: '{'dataNumber':'15753','intervalTime':'10','messageType':'Data','time':'2018-09-12T10:02:20.059 + 01:00','type': 'PresenceData','zone':[{'numVeh':'0','zoneId':'1','zoneOccupancy':'0'},{'numVeh':'0','zoneId':'2 ','zoneOccupancy':'0'}]}'

2。这是我用于GUI的代码:

import urllib.parse
import requests
import json
import sys
import tkinter
from tkinter import *
from websocket import create_connection
from tkinter.constants import *

def ipinput ():
    iptext = ipent.get()
    iplabel2 = Label (ipGui, text=iptext).pack()
    return

def connect ():
ws = create_connection('ws://'+ ipinput + API)
ws.connect('ws://' + ipinput + API)
return

def result ():
     result = ws.recv()    
     jsondata = json.loads(result)
     print ("Web Socket data received : \n'%s'" % jsondata)
     print('')
     return

ipGui = Tk()
ipGui.geometry('450x450+500+300')
ipGui.title('Flir API Interface')

ipent = StringVar()
Apient = StringVar()

iplabel = Label (ipGui, text='Enter IP Address:').pack()
mEntry = Entry(ipGui, textvariable=ipent).pack()
Apilabel = Label (ipGui, text='Data or Event ?  CaSe Sensitive!!!').pack()
ApiEntry = Entry(ipGui, textvariable=Apient).pack()

connectbutton = Button(ipGui, text ='Connect',command = connect).pack()
Connectlabel = Label (ipGui, text='Connection output:').pack()
resultbutton = Button(ipGui, text ='Result',command = result).pack()
Connectlabel = Label (ipGui, text='Result output:').pack()
xbutton = Button(ipGui, text ="Exit",command = ipGui.destroy).pack()

mainloop()


Type=Apient

def connect ():
     main_api = 'ws://'
     API= '/api/subscriptions'
     ws = create_connection(main_api + ipent + API)
     ws.connect(main_api + ipent + API)

if Type =='Data':
         ws.send(json.dumps(Data))
if Type =='Event':
         ws.send(json.dumps(Event))

Data ={
  "messageType":"Subscription",
  "subscription":{
    "type":"Data",
    "action":"Subscribe"
  }
}
Event={
  "messageType":"Subscription",
  "subscription":{
    "type":"Event",
    "action":"Subscribe"
  }
}

while True:

    result = ws.recv()    
    jsondata = json.loads(result)
    print ("Web Socket data received : \n'%s'" % jsondata)
    print('')

ws.close()

感谢您阅读这篇长文章。希望该信息对您有所帮助。

0 个答案:

没有答案