2014-10-28 41 views
0

我認爲這很容易,但在這裏我是!按下按鈕時調用一個程序

我是在按下按鈕時要調用的程序和一個標籤

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

b.bind(on_press = FindIncident(Spinner.text))

您需要傳遞函數作爲參數bind。你沒有通過FindIncident,你是調用它並傳遞結果......這是None。

嘗試

from functools import partial 
b.bind(on_press=partial(FindIncident, spinner.text)) 

還宣佈FindIncident爲def FindIncident(sStation, *args):,如綁定會自動傳遞額外的參數。你也可以用lambda函數做同樣的事情。

請注意您的代碼案例 - 當您使用Spinner.text時,如果您的意思可能是spinner.text,那麼您創建的變量爲spinner

+0

非常感謝!直到星期四我都不會去嘗試它,但我相信它會很棒!只是很快有什麼想法,爲什麼我不能看到上面的代碼標籤?再次感謝! – 2014-10-28 22:54:22

相關問題