2013-10-18 30 views
0

我一直在嘗試使用本教程中的控件http://kivy.org/docs/tutorials/firstwidget.html# 我無法對該控件進行任何操作,它不會將我的點擊識別爲觸摸。我如何才能將它檢測爲觸摸響應? 這是我現在的代碼,Kivy應用程序無法識別我筆記本電腦的觸摸板上的觸摸

from kivy.app import App 
from kivy.uix.widget import Widget 

class MyPaintWidget(Widget): 
    def on_touch_down(self, touch): 
     with self.canvas: 
      Color(1, 1, 0) 
      d = 30. 
      Ellipse(pos=(touch.x - d/2, touch.y - d/2), size=(d, d)) 

class MyPaintApp(App): 
    def buil(self): 
     return MyPaintWidget() 

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

回答

1

1)你有一個錯字,定義方法buil它應該是build。這意味着該方法不會做任何事情,因爲它不會被調用,所以繪製小部件從不創建或顯示。

2)您不導入顏色或橢圓。這會在on_touch_down方法中引發錯誤,即使上述錯誤是正確的。

下面是一個固定的版本,適合我。也許這兩個錯誤只是粘貼到這裏的錯誤,但它們肯定會破壞應用程序 - 第一個錯誤會導致您看到的行爲。

from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.graphics.context_instructions import Color 
from kivy.graphics.vertex_instructions import Ellipse 

class MyPaintWidget(Widget): 
    def on_touch_down(self, touch): 
     with self.canvas: 
      Color(1, 1, 0) 
      d = 30. 
      Ellipse(pos=(touch.x - d/2, touch.y - d/2), size=(d, d)) 

class MyPaintApp(App): 
    def build(self): 
     return MyPaintWidget() 

if __name__ == '__main__': 
    MyPaintApp().run() 
相關問題