I'm trying to create a simple desktop app that gets an IP address and show it's location using a REST API (including country, city, isp, regionName and timezone). I have 5 checkboxes in my form so that the user can decide what data will be shown (city, country, isp etc).
The way i did it was by using 5 If statements for each checkbox which don't seem a good way of doing it. Also in order to set the returned values from API to the lineEdits, again i had to use 5 if statements for each lineEdit.
Is there any other way of checking which checkboxes are checked so that i can call the API with the data related to those checkboxes? My snippet:
def set_params(self,IP):
try:
socket.inet_aton("%s" %IP)
except:
pass #for now
else:
if self.ui.checkBox_City.isChecked():
city = "city"
else:
city = None
if self.ui.checkBox_Country.isChecked():
country = "country"
else:
country = None
if self.ui.checkBox_Rname.isChecked():
Rname = "regionName"
else:
Rname = None
if self.ui.checkBox_ISP.isChecked():
ISP = "isp"
else:
ISP = None
if self.ui.checkBox_Time.isChecked():
time = "timezone"
else:
time = None
param = {"fields": "{},{},{},{},{}".format(country,city,ISP,Rname,time)}
r = requests.get("http://ip-api.com/json/{}".format(IP), params=param)
resdict = r.json()
#print(resdict)
if "country" in resdict.keys():
self.ui.lineEdit_Country.setText(resdict["country"])
if "city" in resdict.keys():
self.ui.lineEdit_City.setText(resdict["city"])
if "isp" in resdict.keys():
self.ui.lineEdit_ISP.setText(resdict["isp"])
if "timezone" in resdict.keys():
self.ui.lineEdit_Time.setText(resdict["timezone"])
if "regionName" in resdict.keys():
self.ui.lineEdit_Country.setText(resdict["regionName"])
Aucun commentaire:
Enregistrer un commentaire