Ошибка вызова функции с несколькими аргументами, расположенными в другом файле Python - PullRequest
1 голос
/ 07 июня 2019

При нажатии кнопки, при попытке вызвать функцию с тремя аргументами, программа прерывается, однако вызов функции без аргументов выполняется правильно.

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

Ошибка при нажатии кнопки на втором экране: Ошибка типа: dummy_two () отсутствует 3 обязательных позиционных аргумента: «один», «два» и «три»

Функция dummy_two (self, one, two, three) корректно выполняется при запуске файла another.py, но вылетает при вызове из основного файла (main.py).

1 Ответ

3 голосов
/ 07 июня 2019

Добавьте None к параметрам в качестве значений по умолчанию. Значения по умолчанию указывают, что параметр функции примет это значение, None, если во время вызова функции не передано значение аргумента.

Отрывки

def dummy_two(self,one=None, two=None, three=None):
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...