0

How do I make this python code containing kivy run in another python code containing kivy like you would call a function so I can have different segment of the code. I don't want too much code on a particular python file because I would be dealing with large code.

import kivy
from kivy.app import App
from kivy.uix.floatlayout import Floatlayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.label import Label 


class LandingScreen(FloatLayout):
    def __init__(self, **kwargs):
        super(LandingScreen, self).__init__(**kwargs)

         
        self.btn1=Button(text='button1 ', size_hint=(0.5, 0.5), 
        on_press=self.click_b1))
        self.btn2=Button(text='button2', size_hint=(0.5, 0.5), 
        on_press=self.click_b2))


            
        self.add_widget(self.btn1)
        self.add_widget(self.btn2)

        def click_b1(self, instance):
             
             pass
        def click_b2(self, instance):
             pass
       
class SplashApp(App):
    def build(self):
        return LandingScreen()

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

How do I call this python file inside this other python file below supposed the first file is a.py and the second file is b.py

import kivy
from kivy.app import App
from kivy.uix.floatlayout import Floatlayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.label import Label 


class LandingScreen2(FloatLayout):
    def __init__(self, **kwargs):
        super(LandingScreen2, self).__init__(**kwargs)

         
        self.btn3=Button(text='button1 ', size_hint=(0.5, 0.5), 
        on_press=self.click_b3))
        self.btn4=Button(text='button2', size_hint=(0.5, 0.5), 
        on_press=self.click_b4))


            
        self.add_widget(self.btn3)
        self.add_widget(self.btn4)

        def click_b3(self, instance):
             
             pass
        def click_b4(self, instance):
             pass
       
class SplashApp(App):
    def build(self):
        return LandingScreen2()

if __name__ == '__main__':
    SplashApp().run()
Thompson
  • 39
  • 4
  • See https://stackoverflow.com/questions/20309456/call-a-function-from-another-file-in-python and https://docs.python.org/3.8/tutorial/modules.html –  Jul 08 '20 at 19:56
  • 1
    Does this answer your question? [How to import other Python files?](https://stackoverflow.com/questions/2349991/how-to-import-other-python-files) – MartinNajemi Aug 22 '20 at 14:05
  • Dupe of https://stackoverflow.com/questions/2349991/how-to-import-other-python-files – MartinNajemi Jun 11 '21 at 15:57

2 Answers2

0

To call another file, you treat it just like a module, you would use:

#a.py
import b
b.execute()
#b.py
def execute() :
    print("This works!")

All variables and functions would need to have the prefix b. to reference the "module" as you can see in the example

This question is a possible duplicate of:

https://stackoverflow.com/questions/2349991/how-to-import-other-python-files
MartinNajemi
  • 514
  • 3
  • 18
-1

Its called import. you can import the second module , say b.py in your current module and call the entry point of this module.

in a.py

import b
...
...
#call b.py entry point.
b.SplashApp().run()

you can do something extra ordinary, spawning anew process and call b.py with python via the shell (but this is extraordinary, and i mention this just to let you know that you can do what ever u like with python)

import subprocess
p= subprocess.Popen("python b.py",shell=True)
p.communicate
Adam
  • 2,820
  • 1
  • 13
  • 33