Reputation: 31
So i am writing my first python kivy code and came across this problem. Tried searching up but didn't quite understand how to solve it. can u please correct my code and tell me what's wrong?
import kivy
from kivy.app import App
from kivy.config import Config
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '150')
kv = Builder.load_file("krxls.kv")
class LoginPage(Screen):
pass
class RegisterPage(Screen):
pass
class WindowManager(ScreenManager):
pass
class KrxLS(App):
def build(self):
return kv
if __name__ == "__main__":
KrxLS().run()
This is my kv file
WindowManager:
LoginPage:
RegisterPage:
<LoginPage>:
name: "lp"
GridLayout:
cols: 1
Label:
text: "Login!!"
GridLayout:
cols: 2
Label:
text: "Username"
TextInput:
multiline: "False"
Label:
text: "Password"
TextInput:
multiline: "False"
Button:
text: "Sign up"
on_release: app.root.current = "rp"
Button:
text: "Sign in!"
<RegisterPage>:
name: "rp"
GridLayout:
cols: 1
Label:
text: "Register!!"
GridLayout:
cols: 2
Label:
text: "Username"
TextInput:
multiline: "False"
Label:
text: "Password"
TextInput:
multiline: "False"
Button:
text: "Sign in"
on_release: app.root.current = "lp"
Button:
text: "Sign Up!"
i get this error
Traceback (most recent call last):
File "/Users/randomman/PycharmProjects/Beginner/GUIDev/kivyGUI.py", line 12, in <module>
kv = Builder.load_file("krxls.kv")
File "/Users/randomman/PycharmProjects/Beginner/venv/lib/python3.7/site-packages/kivy/lang/builder.py", line 301, in load_file
return self.load_string(data, **kwargs)
File "/Users/randomman/PycharmProjects/Beginner/venv/lib/python3.7/site-packages/kivy/lang/builder.py", line 399, in load_string
widget = Factory.get(parser.root.name)(__no_builder=True)
File "/Users/randomman/PycharmProjects/Beginner/venv/lib/python3.7/site-packages/kivy/factory.py", line 131, in __getattr__
raise FactoryException('Unknown class <%s>' % name)
kivy.factory.FactoryException: Unknown class <WindowManager>
Please help!! i am learning kivy through youtube vids and tried following them but i got this error... I have already searched this up in google and haven't found a proper solution.
Upvotes: 0
Views: 63
Reputation: 38837
You need to define your WindowManager
:
class WindowManager(ScreenManager):
pass
before you load the kv
file. A generally safe way to do the kv
load is to call it inside the build()
method, like this:
class KrxLS(App):
def build(self):
kv = Builder.load_file("krxls.kv")
return kv
Upvotes: 1