在另一个python文件中调用带有多个参数的函数时出错

时间:2019-06-07 18:25:56

标签: python-3.x kivy

按下按钮时,尝试调用带有三个参数的函数,程序将中断,但是调用没有参数的函数将正确执行。

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label

from another import Another
class MainWindow(Screen, Another):
    """ This class imports class Another from another.py file"""
    pass


class SecondWindow(Screen, Another):
    """ This class imports class Another from another.py file"""
    pass


class WindowManager(ScreenManager):
    """ This class is to control screen operations."""
    pass


kv = Builder.load_file("my.kv")
class MyMainApp(App):
    def build(self):
        return kv

if __name__ == "__main__":
    MyMainApp().run()

another.py

class Another:

    def dummy_one(self):
        print("This is Alpha. This function has zero arguments")

    def dummy_two(self,one, two, three):
        """Just a test function"""
        print('This is:',one)
        print('This is:',two)
        print('This is:',three)
        print('This function has three positional arguments')

obj = Another()
obj.dummy_two('Beta','Gamma','Delta')

my.kv

WindowManager:
    MainWindow:
    SecondWindow:

<MainWindow>:
    name: "main"
    BoxLayout:
        orientation: "vertical"
        Label:
            text: "Welcome to the MAIN SCREEN"
        Button:
            text: "Press Me, See the console output!!!"
            on_release:
                app.root.current = "second"
            on_press:
                root.dummy_one() # This executes fine, has zero arguments


<SecondWindow>:
    name: "second"
    BoxLayout:
        orientation: "vertical"
        Label:
            text: "Welcome to the SECOND SCREEN"
        Button:
            text: "Press Me, See the console output. Go back home!!!"
            on_release:
                app.root.current = "main"
                root.manager.transition.direction = "right"
            on_press:
                root.dummy_two()  # This throws error, has three positional arguments

在第二个屏幕中按下按钮时出错: TypeError:dummy_two()缺少3个必需的位置参数:“一个”,“两个”和“三个”

dummy_two(self,one,2,3)函数在运行文件another.py时正确执行,但是从主文件(main.py)调用时崩溃。

1 个答案:

答案 0 :(得分:3)

None添加为参数的默认值。默认值指示函数参数将采用该值,如果在函数调用期间未传递任何参数值,则为None

摘要

def dummy_two(self,one=None, two=None, three=None):