2013-11-03 59 views
0

我正在通過Kivy教程programming guide,並發現下面的代碼實際上並不是在任何地方打印按鈕位置,據我所知---也就是說,btn_pressed( )方法似乎沒有做任何事情。Kivy教程按鈕不打印

from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.uix.button import Button 
from kivy.uix.boxlayout import BoxLayout 
from kivy.properties import ListProperty 

class RootWidget(BoxLayout): 

    def __init__(self, **kwargs): 
     super(RootWidget, self).__init__(**kwargs) 
     self.add_widget(Button(text='btn 1')) 
     cb = CustomBtn() 
     cb.bind(pressed=self.btn_pressed) 
     self.add_widget(cb) 
     self.add_widget(Button(text='btn 2')) 

    def btn_pressed(self, instance, pos): 
     print ('pos: printed from root widget: {pos}'.format(pos=pos)) 

class CustomBtn(Widget): 

    pressed = ListProperty([0, 0]) 

    def on_touch_down(self, touch): 
     if self.collide_point(*touch.pos): 
      self.pressed = touch.pos 
      # we consumed the touch. return False here to propagate 
      # the touch further to the children. 
      return True 
     return super(CustomBtn, self).on_touch_down(touch) 

    def on_pressed(self, instance, pos): 
     print ('pressed at {pos}'.format(pos=pos)) 

class TestApp(App): 

    def build(self): 
     return RootWidget() 


if __name__ == '__main__': 
    TestApp().run() 

有沒有人有任何提示或想法,爲什麼這是行不通的?這是預期的行爲,我錯過了一些東西,或者教程中有錯誤嗎?

具體地,雖然可以單擊和閃光上述農產品按鈕指令---似乎沒有爲對應於所述方法的任何輸出:

def btn_pressed(self, instance, pos): 
    print ('pos: printed from root widget: {pos}'.format(pos=pos)) 

在黑色也許它打印黑色?

+2

代碼正常工作。你也可以提供控制檯的日誌輸出嗎? – Nykakin

+0

嗯,我沒有看到日誌顯示,當我運行這個,只是一堆信息,調試,警告---但他們不會改變點擊。你在哪裏看到「從根部件打印出來」的輸出結果? – Mittenchops

+0

更新上述---我認爲我不清楚什麼是不工作。 – Mittenchops

回答

0

中間看似空白,未標記的點是接受位置並將位置打印到控制檯的按鈕。我點擊標有「btn」的按鈕,並沒有注意到這個地方存在。

本教程的這一部分將演示如何使自定義按鈕完成這樣的工作。這個標籤會更清楚,但通過查看API應該是可行的。

無論如何,代碼工作正常。