F.R.D F.R.D - 1 year ago 156
Python Question

python kivy garden knob - how to override on_knob

Using Kivy.garden.knob, and having an issue:
I'm trying to post the knob value to the command line when I move it, but I'm unable to make it work, any suggestions?
Here the whole code, thanks in advance, FD.


from threading import Thread
from cmd import Cmd
import logging
logging.getLogger("kivy").disabled = True

from kivy.app import App
from kivy.core.window import Window
from kivy.garden.knob import Knob
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen

class FirstTimeConnect(Screen):
def __init__(self, **kwargs):
super(FirstTimeConnect, self).__init__(**kwargs)
layout = GridLayout(cols=2)

layout.knob1 = Knob(knobimg_source="/Users/admin/Dropbox/Strumenti/Concatenativa_PyCpp/knob_img/knob_black.png", marker_img="/Users/admin/Dropbox/Strumenti/Concatenativa_PyCpp/knob_img/bline.png")

def on_knob(self, value):
print "hi"


sm = ScreenManager()
sm.add_widget(FirstTimeConnect(name='First Time Connect'))

class MyCmd(Cmd, object):

class MyApp(App):

def build(self):
#return LogScreen()
return sm

if __name__ == '__main__':
app = MyApp()

Answer Source

To override a method in a widget/class you need to inherit from the class. The thing you did is "overriding" on_knob method of Screen and as Screen has no such method as on_knob, it just created a new one.

class MyKnob(Knob):
    def on_knob(self, value):

Also notice that there's no property knob and that it's bound kind of "manually" with _value. In a casual Kivy behavior on_* methods are events and to override them you'd need to do this:

class MyKnob(Knob):
    def on_my_property(self, <arguments if it has any>):
        super(MyKnob, self).on_my_property(<arguments>)

depending on what you want to execute first i.e.

  • old behavior + your print (super + print)
  • your print + old behavior (print + super)

In case of completly rewriting it, just remove super() from the method and it won't do the default behavior e.g. let's say if you override on_touch_down with this:

class MyWidget(Widget):
    def on_touch_down(self, touch):

there will be no touch event if a touch collides with an area of your widget (if you touch the widget).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download