Kivy how to use Builder.load_file? - python

I have the following directory structure:
project/
controller/
__init__.py
app1.py
view/
app1.kv
main.py
My main.pyis:
from controller.app1 import App1
def main():
App1().run()
if __name__ == '__main__':
main()
My app1.pyis:
from kivy.app import App
from kivy.lang import Builder
Builder.load_file('view/app1.kv')
class App1(App):
pass
I am running:
kivy main.py
However, the contents of my Kivy is not loaded.

When you load a .kv file using Build.load_file make sure the widget is the root
to avoid it from returning None.read the doc to see other things you do with Builder
from kivy.app import App
from kivy.lang import Builder
class App1(App):
def build(self):
self.root = Builder.load_file(os.path.join(dirname(__file__),/app1.kv')
)

It turns out that the method build() has to be implemented in the class that inherits from App and this method has to return Builder.load_file('view/app1.kv'), and using #Leon suggestion my app1.py is:
from kivy.app import App
from kivy.lang import Builder
class App1(App):
def build(self):
return Builder.load_file(
os.path.join(dirname(__file__), '../view/app1.kv')
)
Now, the Kivy file is properly loaded.

Related

When I run the code, it says "ModuleNotFoundError: No module named 'kivy.uix.Label"

import kivy
from kivy.app import App
from kivy.uix.Label import Label
class invisible(App):
def build(self):
return Label(text="Hello")
if __name__ == "__main__":
invisibleApp().run()
Error comes from the third line.Cannot import name "label"
You want kivy.uix.label, not kivy.uix.Label.
from kivy.uix.label import Label
always remember to use lower case after uix
Eg
kivy.uix.image
kivy.uix.button
kivy.uix.screenmanager
Rename your code name if you are using kivy.py

can't import kivy properly

I am trying to learn kivy in python and I have built a small program that should display hello world on the screen but when I run it I get:
cannot import name 'app' from 'kivy.app'
code:
from kivy.app import app
from kivy.uix.label import label
class MyApp(App):
def build(self):
return label(text="hello world")
if __name__ == "__main__":
MyApp.run()
and I am running this in a virtual enviroment
Good day. As mentioned by buran in the comments of your question, your import code must change to:
from kivy.app import App
from kivy.uix.label import Label
Even later in your code, MyApp inherits from App.
Watch your syntax. Classes generally begin with capital letters.

I am new to python I am getting stuck in a error: Exception: Invalid instance in App.root

I use VSCode and this is the code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
class RajdeepGrid():
pass
class RajdeepApp(App):
def build(self):
return RajdeepGrid()
if __name__ == "__main__":
RajdeepApp().run()
Now when I execute this after all imports this error occurs:
Exception: Invalid instance in App.root
It looks like your class RajdeepGrid(): should inherit from a Kivy class, like class RajdeepGrid(GridLayout):.

Kivy keeps coming up blank

I am trying to learn kivy and have two files open my main python file and a kv file for styling, everytime I run the python file the resulting window always comes up blank. I am running it using python main.py on windows 10.
main.py
import kivy
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.label import Label
class CustomLabel(Label):
pass
class App(App):
def build(self):
return CustomLabel()
app = App()
app.run()
kivy.kv
<CustomLab#Label>:
font_size: 32
<CustomLabel>:
CustomLab:
text: "Hello World"
I wanted to edit your post and comment but I can't.
You should not name your main class as "App" instead you can name it like "TestApp".
Like this:
class TestApp(App):
Also your kivy file's name should be a lowercase of your "TestApp" without the word "App" on it.
Like this:
test.kv
Inside your kivy file, you misspelled the class "CustomLabel" as "CustomLab".
To declare a class inside a kivy file, you must use a "<" and ">".
Like this:
<CustomLabel>:
I changed your code and it looked like this:
This is your main.py:
import kivy
kivy.require("1.9.0")
from kivy.app import App
from kivy.uix.label import Label
class CustomLabel(Label):
pass
class TestApp(App):
def build(self):
return CustomLabel()
app = TestApp()
app.run()
This is your test.kv:
<CustomLabel>:
text: "Hello World"
font_size: 32
I hope this will work.

Running multiple Kivy apps at same time that communicate with each other

I would like my Kivy application to be able to spawn multiple apps (i.e. new windows) on a Windows machine that can communicate with each other.
ScreenManager and Popup options will not cut it because they live in the same window..I need to be able to drag new screens across multiple monitors and therefore need multiple windows.
Kivy docs explicitly state that "Kivy supports only one window
per application: please don't try to create more than one."
A google search produces this simple approach of simple spawning a new app from within another app, like so:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
class ChildApp(App):
def build(self):
return Label(text='Child')
class MainApp(App):
def build(self):
b = Button(text='Launch Child App')
b.bind(on_press=self.launchChild)
return b
def launchChild(self, button):
ChildApp().run()
if __name__ == '__main__':
MainApp().run()
However, when I do this, it launches the app within the same window and crashes, and my terminal spits out like crazy:
Original exception was:
Error in sys.exceptionhook:
I get the same result if instead of ChildApp().run() I do multiprocessing.Process(target=ChildApp().run()).start()
Using the subprocess library gets me closer to what I want:
# filename: test2.py
from kivy.app import App
from kivy.uix.label import Label
class ChildApp(App):
def build(self):
return Label(text='Child')
if __name__ == '__main__':
ChildApp().run()
# filename: test.py
from kivy.app import App
from kivy.uix.button import Button
import subprocess
class MainApp(App):
def build(self):
b = Button(text='Launch Child App')
b.bind(on_press=self.launchChild)
return b
def launchChild(self, button):
subprocess.call('ipython test2.py', shell=True)
if __name__ == '__main__':
MainApp().run()
This spawns the child window without error, however now the main window is locked (white canvas) and if I close the child window, it just gets reopened.
They need to be able pass data between one another. Any ideas on how to do this correctly in Windows? This post seems to suggest that this is possible but I'm not sure where to start.
I tried baconwichsand's code and can confirm with Python 3.6 and Windows 10 it does not work. Apparently only top level object classes can be pickled, and since both apps inherit from the App class python throws an error. However a top level definition that simply executes the ChildApp().run() command can be pickled and works. Here is my working code.
import multiprocessing
from kivy.app import App
from kivy.uix.label import Label
class MainApp(App):
def build(self):
return Label(text='Main App Window')
class OtherApp(App):
def build(self):
return Label(text='Other App Window')
def open_parent():
MainApp().run()
def open_child():
OtherApp().run()
if __name__ == '__main__':
a = multiprocessing.Process(target=open_parent)
b = multiprocessing.Process(target=open_child)
a.start()
b.start()
And here is the code I am using, including the Builder to use a shared .kv file for both windows.
import multiprocessing
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.widget import Widget
class MainRoot(Widget):
pass
class OtherRoot(Widget):
pass
class MainApp(App):
def build(self):
Builder.load_file('B:\Python_Codes\Testing Grounds\shared.kv')
main = MainRoot()
return main
class OtherApp(App):
def build(self):
Builder.load_file('B:\Python_Codes\Testing Grounds\shared.kv')
other = OtherRoot()
return other
def open_parent():
MainApp().run()
def open_child():
OtherApp().run()
if __name__ == '__main__':
a = multiprocessing.Process(target=open_parent)
b = multiprocessing.Process(target=open_child)
a.start()
b.start()
I'm not sure why it doesn't work with multiprocessing (I've never tried it), but it should at least work with subprocess. The reason your main window is locked is because subprocess.call blocks the thread that calls it while it waits for the subprocess to finish and return a result.
You want to use subprocess.Popen instead, which does not block.
bj0's answer regarding subprocess was correct.
Even better, I figured out how to do this via multiprocessing, which allows better communication and passing of information between apps. It wasn't working before because I did multiprocessing.Process(target=ChildApp().run()).start() when it should be multiprocessing.Process(target=ChildApp().run).start(). The following works
# filename: test.py
from kivy.app import App
from kivy.uix.button import Button
from test2 import ChildApp
import multiprocessing
class MainApp(App):
def build(self):
b = Button(text='Launch Child App')
b.bind(on_press=self.launchChild)
return b
def launchChild(self, button):
app = ChildApp()
p = multiprocessing.Process(target=app.run)
p.start()
if __name__ == '__main__':
MainApp().run()
# filename: test2.py
from kivy.app import App
from kivy.uix.label import Label
class ChildApp(App):
def build(self):
return Label(text='Child')
if __name__ == '__main__':
ChildApp().run()
Working on Ubuntu Python 3.10
Calling a subprocess in a multiprocess.
main.py
import multiprocessing
import subprocess
import shlex
#my kivy code..
def sub_window():
subprocess.call(shlex.split('python3 test.py'))
if __name__ == '__main__':
b = multiprocessing.Process(target=sub_window)
b.start()
MyApp().run()
test.py
#another kivy app script..
OtherApp().run()

Categories

Resources