user7657029
user7657029

Reputation:

How do you import another python file into Kivy?

I copied this piece of code from the internet to try to learn how Kivy works (btw it does work). I'm trying to import another python file I created called "Mifflin" (a calorie equation program) by using the command:

import Mifflin

with the rest of the imports. It imports it correctly but whenever I run the program it runs Mifflin and once its done executing the file it then runs the rest of the code.

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
class TestApp(App):
    def build(self):
        layout = BoxLayout(orientation='vertical')
        # use a (r, g, b, a) tuple
        blue = (0, 0, 1.5, 2.5)
        red = (2.5, 0, 0, 1.5)
        btn =  Button(text='Touch me!', background_color=blue, font_size=120)        
        btn.bind(on_press=self.callback)
        self.label = Label(text="------------", font_size='50sp')
        layout.add_widget(btn)
        layout.add_widget(self.label)
        return layout
    def callback(self, event):
        print("button touched")  # test
        self.label.text = "button touched"
TestApp().run()

My main goal is to press a button which then runs the program "Mifflin" and I don't know how to do that. Thanks in advance for helping me.

Upvotes: 1

Views: 1816

Answers (1)

Peter Badida
Peter Badida

Reputation: 12169

I believe your code looks similar to this:

import <that file>
print('hi')

and your console output will be:

[INFO   ] [GL          ] NPOT texture support is available
button touched
[INFO   ] [Base        ] Leaving application in progress... # Kivy app exits here
hi

because there's TestApp().run() at the end of the imported file. Do this to prevent it:

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

When App.run() method is called, Kivy starts its event loop, which in a very simple way looks like this:

while True:
    pass

and until such a loop is broken no code after it (therefore even after the import) will be executed.

Upvotes: 1

Related Questions