I don't know why, but every time I try to run my code it doesn't work, here's the code.
If you can help me it'd be really wholesome. Thank you in advance.
Here's the code of the 3 files.
main.py
from kivy.app import App
from App.Constructor.constructor import Constructor
class MyApp(App):
def build(self):
return Constructor().constr()
if __name__ == '__main__':
MyApp().run()
constructor.py
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
kv = Builder.load_file('Constructor\\constructor.kv')
class WindowManager(ScreenManager):
pass
class Constructor():
def constr(self):
return kv
constructor.kv
#:import Login App.Login
#:import SignUp App.SignUp
WindowManager:
Login:
SignUp:
<Login>:
name: "login"
<SignUp>:
name: "signup"
This because of you used the WindowManager in kv file before you define it in the python file
The solution will be to load the kv file after creating the WindowManager class
like this
1)
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('Constructor\\constructor.kv')
or
2)
loading directly when you need it
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
class WindowManager(ScreenManager):
pass
class Constructor():
def constr(self):
return Builder.load_file('Constructor\\constructor.kv')
Related
How do you reference methods from outside classes in a .kv file? For example, here is my main.py:
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
import random
class MyClass:
def add():
return 1 + 1
class MainLayout(Widget):
pass
class MyApp(App):
def build(self):
return MainLayout()
if __name__ == "__main__":
MyApp().run()
And here is my main.kv:
<MainLayout>
GridLayout:
size: root.width, root.height
Button:
text: "Button"
on_press: root.add()
My problem is being able to access the add() function from the .kv file since the function is not inside the MainLayout class, I just need a button that will call add(). What is the proper method for doing this?
I could probably define add() in the MainLayout class but that might be too messy with a large program with many classes and methods.
I just cannot understand why it doesn't, I'm trying to create this simple program but it gives me this error.
my code is:
main.py
from kivy.app import App
from kivymd.theming import ThemeManager
class Mainapp(App):
theme_cls = ThemeManager()
Mainapp().run()
helper.kv
NavigationLayout:
MDNavigationDrawer:
ScreenManager:
You need to change:
class Mainapp(App):
to:
class Mainapp(MDApp):
I'm new to kivy so I don't really know how to display a progress bar in my NaviWindow. I cant seem to put progress bar inside the kv file so anyone knows how to display the bar in the NaviWindow?
.pyfile
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.progressbar import ProgressBar
from kivy.uix.screenmanager import ScreenManager, Screen
KV = """
WindowManager:
NaviWindow:
<NaviWindow>:
"""
class WindowManager(ScreenManager):
pass
class NaviWindow(Screen):
def build(self):
Progress = ProgressBar(max=1000)
Progress.value = 100
return Progress
class MyMainApp(App):
def build(self):
return Builder.load_string(KV)
if __name__ == "__main__":
MyMainApp().run()
Only the App class (and obviously the classes that inherit from App) has the build method, but you seem to think that the Screen method also has it and is clearly incorrect. The solution is to add the ProgressBar using the add_widget() method:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.progressbar import ProgressBar
from kivy.uix.screenmanager import ScreenManager, Screen
KV = """
WindowManager:
NaviWindow:
<NaviWindow>:
"""
class WindowManager(ScreenManager):
pass
class NaviWindow(Screen):
def __init__(self, **kwargs):
super(NaviWindow, self).__init__(**kwargs)
self.progress = ProgressBar(max=1000)
self.progress.value = 100
self.add_widget(self.progress)
class MyMainApp(App):
def build(self):
return Builder.load_string(KV)
if __name__ == "__main__":
MyMainApp().run()
How to access a class in python code using on_release: by kivy? For example, a function that is in root I use. root.function() and a class?
on_release: # What to put here to access the function `chama`?
class Tela(ScreenManager):
pass
class teste(Screen):
def chama(self):
pass
def save_d(self):
class Prg(App):
def build(self):
return Tela()
Prg().run()
If you use kv (file or string), then a good way to access other classes is to use ids.
Here is an example:
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
class Tela(ScreenManager):
pass
class Teste(Screen):
def chama(self):
print ('Hello')
class Teste2(Screen):
pass
class PrgApp(App):
def build(self):
return Builder.load_file('Prg.kv')
PrgApp().run()
Prg.kv:
Tela
Teste2
Button
on_release: t.chama()
Teste
id: t
note: the first letter of a widget class must be capitalized, otherwise you will get a syntax error
I'm simply trying to make some example code I found which dynamically adds widgets to a view incorporate with a Screen Manager, and I cannot get it to work.
I found this example Associating Screens with GridLayout classes in kivy and as far as I know I've implemented the strategy defined there, but I keep getting kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.
EDIT: Here's my updated code. now getting error: AttributeError: MainScreen instance has no attribute 'add_widget'
from kivy.uix.modalview import ModalView
from kivy.uix.listview import ListView
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.app import App
import citylists
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
#using 'sla'...whatever that means...
Builder.load_string("""
#:import ListItemButton kivy.uix.listview
#:import sla kivy.adapters.listadapter
<ListViewModal>:
ListView:
size_hint: .8, .8
adapter:
sla.ListAdapter(
data=["Item #{0}".format(i) for i in range(100)],
cls=ListItemButton.ListItemButton)
""")
class ListViewModal(ModalView):
def __init__(self, **kwargs):
super(ListViewModal, self).__init__(**kwargs)
class MainView(Screen):
def __init__(self, **kwargs):
kwargs['cols'] = 1
super(MainView, self).__init__(**kwargs)
listview_modal = ListViewModal()
self.add_widget(listview_modal)
class MainScreen():
pass
mainscreen=MainScreen()
mainlayout = MainView()
mainscreen.add_widget(mainlayout)
class CARApp(App):
screen_manager = None
def build(self):
self.screen_manager = ScreenManager()
self.screen_manager.add_widget(mainscreen)
if __name__ == '__main__':
CARApp().run()
self.screen_manager.add_widget(MainScreen)
You're passing the actual class MainScreen, but you need to add an instance of the class, i.e. MainScreen().
Edit, although looking more at your code, you probably want to add the instance you have already created, which is mainscreen. It might also be neater to move this widget creation into the build method, since that's where it's actually needed.