NumPad popup when selecting TextInput app yields TypeError: object.__init__() takes no parameters

瀏覽次數:40 次
跳到第一則未讀訊息

Griffo

未讀,
2017年8月8日 下午3:53:102017/8/8
收件者:Kivy users support
I just have a simple tabbed panel with a TextInput object which when clicked should open up a numpad screen which should populate TextInput once enter is clicked. This did work in Kivy 1.90 by my memory though I've heard some new changes have changed the way arguments are inherited.

main.py

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.tabbedpanel import TabbedPanel,TabbedPanelItem
from kivy.uix.textinput import TextInput

class ValveInput(TextInput):

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

    def my_callback(instance):
        self.text = num_pad.ids.num_label.text
        self.popup.dismiss()

    def on_touch_down(self, touch):
        if self.collide_point(touch.x, touch.y):

            prompt_content = BoxLayout(orientation='vertical')      # Main layout
            num_pad = ValveNumWidget()
            prompt_content.add_widget(num_pad)

            self.popup = Popup(id='num_pad_popup', title='Enter value', content=prompt_content, autodismiss=False)
            num_pad.ids.enter_btn.bind(on_press=my_callback)
            # Open the pop-up:
            self.popup.open()

class RootWidget(FloatLayout):
    def __init__(self):
        super(RootWidget, self).__init__()
        self.button = ValveInput(id='valve_input',text='',values=('0', '1','2','3','4','5'))

        home_window = TabbedPanelItem(text="Pumps")
        home_window.add_widget(self.button)

        main_window = TabbedPanel(do_default_tab=False)
        main_window.add_widget(home_window)

        self.add_widget(main_window)

    def button_clicked(self, button):
        print(self.button.text)


class MainApp(App):
    def build(self):
        return RootWidget()


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

main.kv

<ValveNumWidget>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: 'vertical'
        BoxLayout:
            size_hint_y: 1
            id: num_label
            Label
                id: num_label
                text: ''
        BoxLayout:
            size_hint_y: 5
            orientation: 'vertical'
            BoxLayout:
                BoxLayout:
                    Label:
                        text: 'Switching Valve'
                BoxLayout:
                    NumPadButton:
                        id: num_zerosw
                        text: '1'
                        on_press: num_label.text = '1'
                    NumPadButton:
                        id: num_onesw
                        text: '2'
                        on_press: num_label.text = '2'
                
            BoxLayout:
                BoxLayout:
                    Label:
                        text: 'Multi-Channel Pump'
                BoxLayout:
                    NumPadButton:
                        id: num_O
                        text: 'O'
                        on_press: num_label.text = 'O'
                    NumPadButton:
                        id: num_I
                        text: 'I'
                        on_press: num_label.text = 'I'

            BoxLayout:
                BoxLayout:
                    Label:
                        text: 'Single-Channel'
                BoxLayout:
                    NumPadButton:
                        text: '0'
                        id: num_zero
                        on_press: num_label.text = '0'
                    NumPadButton:
                        text: '1'
                        id: num_one
                        on_press: num_label.text = '1'
                    NumPadButton:
                        text: '2'
                        id: num_two
                        on_press: num_label.text = '2'
                    NumPadButton:
                        text: '3'
                        id: num_three
                        on_press: num_label.text = '3'
                    NumPadButton:
                        text: '4'
                        id: num_four
                        on_press: num_label.text = '4'
                    NumPadButton:
                        text: '5'
                        id: num_five
                        on_press: num_label.text = '5'
                    NumPadButton:
                        text: '6'
                        id: num_six
                        on_press: num_label.text = '6'

            BoxLayout:
                BoxLayout:
                    size_hint_x: 3
                    NumPadButton:
                        id: enter_btn
                        text: 'Enter'

Any help appreciated -- I'm fairly sure it is a quick fix but unsure how to proceed.

Griffo

Griffo

未讀,
2017年8月8日 下午4:00:372017/8/8
收件者:Kivy users support
Apologies; I was missing a few pieces. Here is the update code and I'm fairly sure my issue revolved around how I pass kwargs in this part.

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

-----

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.tabbedpanel import TabbedPanel,TabbedPanelItem
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup

class ValveNumWidget(FloatLayout):
    pass

ZenCODE

未讀,
2017年8月8日 下午5:20:132017/8/8
收件者:Kivy users support
Probably auto_dismiss again?

Griffo

未讀,
2017年8月8日 下午5:39:582017/8/8
收件者:Kivy users support
Indeed - I guess I need to check that the arguments being passed are permissible. 

Apologies for not seeing this earlier -- it works by removing that argument.

Griffo

On Tuesday, 8 August 2017 14:20:13 UTC-7, ZenCODE wrote:
Probably auto_dismiss again?
回覆所有人
回覆作者
轉寄
0 則新訊息