Kivy 'object has no attribute' Error

前端 未结 1 685
梦如初夏
梦如初夏 2021-01-17 05:43

I\'m new to python and Kivy programming so getting trouble and may be asking simple question here, but its a big hurdle for me now. I am developing a GUI with kivy. I have s

相关标签:
1条回答
  • 2021-01-17 06:21

    Problem

    You are getting AttributeError: 'MyLayout' object has no attribute 'print_something' because it cannot find the function, print_something.

    Solution

    Please refer to the explanations, example and output for details.

    Explanations

    jwelkreator.py

    1. Add from simpleForm import MyLayout
    2. Remove Builder.load_file('simpleForm.kv')

    jwelkreator.kv

    Add #:include simpleForm.kv to include an external kivy file.

    include <file> - Kivy Language

    Includes an external kivy file. This allows you to split complex widgets into their own files.

    simpleForm.py

    You don't have to define the dynamic class, LblTxt(BoxLayout) since you have it defined in your kv file.

    Dynamic Classes - Programming Guide » Kv language

    This class, created just by the declaration of this rule, inherits from the Button class and allows us to change default values and create bindings for all its instances without adding any new code on the Python side.

    simpleform.kv

    Since in the Python script, simpleForm.py, you have already defined class MyLayout is of BoxLayout, you don't to inherit it in the kv file. Replace <MyLayout@BoxLayout> with <MyLayout>

    Example

    jwealkreator.py

    from kivy.app import App
    from kivy.uix.anchorlayout import AnchorLayout
    from simpleForm import MyLayout
    
    
    class JwelKreator(AnchorLayout):
        pass
    
    
    class JwelKreatorApp(App):
    
        def build(self):
            return JwelKreator()
    
    
    if __name__ == "__main__":
        JwelKreatorApp().run()
    

    jwealkreator.kv

    # File name: jwelkreator.kv
    #:kivy 1.10.0
    #:include simpleform.kv
    
    <JwelKreator>:
        anchor_x: 'left'
        anchor_y: 'top'
        MyLayout:
            id: _tool_box
            size_hint: None,0.75
            width: 300
    

    simpleForm.py

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    
    
    class MyLayout(BoxLayout):
        def print_something(self):
            print("Hello")
    
    
    class SimpleFormApp(App):
    
        def build(self):
            return MyLayout()
    
    
    if __name__ == "__main__":
        SimpleFormApp().run()
    

    simpleform.kv

    #:kivy 1.10.0
    
    <LblTxt@BoxLayout>:
        id:LblTxtid
        orientation: 'horizontal'
        lblTxtIn: 'default'
        theTxt: iAmTxt
        Label:
            text: root.lblTxtIn
            size_hint: 1,0.5
        TextInput:
            id: iAmTxt
            multiline: False
            hint_text: "numeric only"
            input_filter: 'int'
            size_hint: 0.5,None
            height: 30
    
    <MyLayout>:
        orientation: 'vertical'
    
        LblTxt:
            id: lt0
            lblTxtIn: 'Base Layers'
    
        LblTxt:
            id: lt1
            lblTxtIn: 'Base exposer time(ms)'
    
        LblTxt:
            id: lt2
            lblTxtIn: 'Min Support Height(mm)'
    
        LblTxt:
            id: lt3
            lblTxtIn: 'Support Layers'
    
        LblTxt:
            id: lt4
            lblTxtIn: 'Support exposer time(ms)'
    
        LblTxt:
            id: lt5
            lblTxtIn: 'Job exposer time(ms)'
    
        Label:
            text:"Number of Layers"
        Button:
            text: 'OK'
            size_hint: 0.5,None
            height: 30
            on_release: root.print_something()
    

    Output

    0 讨论(0)
提交回复
热议问题