我认为这很容易,但我在这里!!
我按下按钮时调用一个程序,并在标签上显示结果
class DSFRSapp(App):
def build(self):
self.root = FloatLayout()
i = Image(source='DSFRSLogo.png',
allow_stretch=True,
pos_hint = ({'center_x':0.5, 'y': .25}))
spinner = Spinner(
text='Pick a Station',
values=('Appledore','Axminster','Bampton','Barnstaple','Bere Alston','Bideford','Bovey Tracey','Braunton','Bridgwater','Brixham','Buckfastleigh','Budleigh Salterton','Burnham on sea','Camels Head','Castle Cary','Chagford','Chard','Cheddar','Chulmleigh','Colyton','Combe Martin','Crediton','Crewkerne','Crownhill','Cullompton','Dartmouth','Dawlish','Exeter Danes Castle','Exeter Middlemoor','Exmouth','Frome','Glastonbury','Greenbank','Hartland','Hatherleigh','Holsworthy','Honiton','Ilfracombe','Ilminster','Ivybridge','Kingsbridge','Kingston','Lundy Island','Lynton','Martock','Minehead','Modbury','Moretonhampstead','Nether Stowey','Newton Abbot','North Tawton','Okehampton','Ottery St Mary','Paignton','Plympton','Plymstock','Porlock','Princetown','Salcombe','Seaton','Shepton Mallet','Sidmouth','Somerton','South Molton','Street','Taunton','Tavistock','Teignmouth','Tiverton','Topsham','Torquay','Torrington','Totnes','USAR','Wellington','Wells','Williton','Wincanton','Witheridge','Wiveliscombe','Woolacombe','Yelverton','Yeovil'),
size_hint=(None, None),
size=(150, 44),
pos_hint = ({'center_x':0.5, 'y': 0.35}))
b = Button(text="Search For Incidents",size_hint=(None, None),
pos_hint =({'center_x':0.5, 'y': 0.25}),
size=(150, 44))
LblRes = Label(text="Results will display here",
pos_hint =({'center_x':0.5, 'y': 0.15}),
size_hint=(600,100),color=(1,1,1,1),font_size=35)
b.bind(on_press=FindIncident(Spinner.text))
self.root.add_widget(spinner)
self.root.add_widget(LblRes)
self.root.add_widget(i)
self.root.add_widget(b)
return
def FindIncident( sStation ):
webpage = request.urlopen("http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35")#main page
soup = BeautifulSoup(webpage)
incidents = soup.find(id="CollapsiblePanel1") #gets todays incidents panel
Links = [] #create list call Links
for line in incidents.find_all('a'): #get all hyperlinks
Links.append("http://www.dsfire.gov.uk/News/Newsdesk/"+line.get('href')) #loads links into Links list while making them full links
n = 0
e = len(Links)
if e == n: #if no links available no need to continue
print("No Incidents Found Please Try Later")
sys.exit(0)
sFound = False
while n < e: #loop through links to find station
if sFound: #if the station has been found stop looking
sys.exit(0)
webpage = request.urlopen(Links[n]) #opens link in list)
soup = BeautifulSoup(webpage) #loads webpage
if soup.find_all('p', text=re.compile(r'{}'.format(sStation))) == []:#check if returned value is found
#do nothing leaving blank gave error
a = "1" #this is pointless but stops the error
else:
print(soup.find_all('p', text=re.compile(r'{}'.format(sStation)))) #output result
WebLink = Links[n]
sFound = True # to avoid un needed goes through the loop process
n=n+1 # moves counter to next in list
if not sFound: #after looping process if nothing has been found output nothing found
print("nothing found please try again later")
return;
if __name__ =="__main__":
DSFRSapp().run()
所以当按下按钮以使用微调文本作为字符串调用FindIncident时。这样它就可以搜索电台而不是打印我会将结果放在标签上,也可能带有指向网站的链接。
任何帮助都会很棒!
RAIF
答案 0 :(得分:0)
b.bind(on_press = FindIncident(Spinner.text))
您需要将函数作为参数传递给bind
。你没有通过FindIncident,你正在调用并传递结果......这是无。
尝试
from functools import partial
b.bind(on_press=partial(FindIncident, spinner.text))
同时将FindIncident声明为def FindIncident( sStation, *args ):
,因为bind会自动传递额外的参数。你也可以用lambda函数做同样的事情。
在您创建变量Spinner.text
时,请谨慎使用代码案例 - 当您使用spinner.text
时,可能意味着spinner
。