kivy Text Input从Slider继承行为

时间:2017-11-29 21:56:20

标签: python inheritance kivy

我希望在kivy中创建一个微调器小部件,它包含一个带有两个小按钮的文本条目。文本条目中显示的值将增加或减少,具体取决于按下的按钮。

这是一项简单的任务,但此外我希望用鼠标滚轮更改值(当光标位于文本条目内时向上和向下滚动)。

由于文本条目没有这样的行为,是否有可能以某种方式从另一个小部件继承行为,如滑块?如果是这样,那将如何实现?

修改

根据要求,这是迄今为止的代码:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Input 0 is incompatible with layer lstm_37: expected ndim=3, found ndim=4

1 个答案:

答案 0 :(得分:1)

#Here is an example.

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.app import App

class Example(BoxLayout):

    def __init__(self, **kwargs):
        super(Example, self).__init__(**kwargs)

        self.orientation = 'horizontal'
        self.layout = BoxLayout(orientation = 'horizontal', size_hint = (1,1))
        self.text = MyText(text= '500', font_size=40, size_hint = (0.6,0.5), multiline=True)

        self.layout.add_widget(self.text)

        self.add_widget(self.layout)


class MyText(TextInput): # MyText inherits from TextInput class
    def on_touch_down(self, touch): # method to see if mouse is down/moving
        if self.collide_point(*touch.pos): # only works if in the textinput region
            if touch.button == 'scrollup': #check mouse wheel up
                self.calc_plus()
            elif touch.button == 'scrolldown': #check mouse wheel down
                self.calc_minus()
    def calc_plus(self):
        Q = int(self.text)
        self.text = str(Q + 25)
    def calc_minus(self):
        Q = int(self.text)
        self.text = str(Q - 25)

class Test(App):
    def build(self):
        return Example()

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