我想添加一組按鈕到網格佈局滾動視圖使用for循環。但即使在按下按鈕之前,所有按鈕都會觸發按下事件。我怎樣才能解決這個問題?按鈕被按下之前按下按
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.app import runTouchApp
import webbrowser
def btnsclicked(id, url):
print 'btn id '+id+' clicked'
webbrowser.open(url)
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
layout.bind(minimum_height=layout.setter('height'))
for i in range(5):
url= 'https://www.google.com'
btn = Button(text=str(i), size_hint_y=None, height=40, id='b'+str(i))
btn.bind(on_press =btnsclicked('b'+str(i), url))
layout.add_widget(btn)
root = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
root.add_widget(layout)
runTouchApp(root)
喜讓,我怎麼能傳遞參數,而不調用該函數? – SaikiHanee
通過使用lamba表達式這是一箇中繼函數將回調連接到您的自定義回調。 –
非常感謝你讓:) – SaikiHanee