How to access classes using kivy? - python

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

Related

What is the method for accessing classes outside of the MainLayout class in a Kivy file?

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.

kivy.factory.FactoryException: Unknown class <WindowManager>

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')

How do i get the BoxLayout to add widget if it's in another class?

so here is my Class
class CreateTask(Screen):
def CreateTask(self, *args):
bl = BoxLayout(orientation="vertical",spacing=10)
name=self.ids.TaskName.text
desc=self.ids.TaskDesc.text
bl.add_widget(name)
bl.add_widget(desc)
self.MainLayout.add_widget(bl)
and that is the kv of MainLayout
<SecondWindow>:
name:"second"
BoxLayout:
id:MainLayout
orientation:"vertical"
Button:
text:"Add Task"
on_release:
app.root.current="third"
root.manager.transition.direction = "right"
Button:
text:"Come Back"
on_release:
app.root.current ="main"
root.manager.transition.direction = "right"
it has to create widgets by clicking in the BoxLayout which is in another class. But when i run it i get "AttributeError: 'super' object has no attribute 'getattr'" error, so how do i refer to the BoxLayout if it's in another class?
here is my .py file
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.uix.anchorlayout import AnchorLayout
Window.clearcolor = (1,1,1,1)
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class CreateTask(Screen):
def CreateTask(self,*args):
bl = BoxLayout(orientation="vertical",spacing=10)
name=self.ids.TaskName.text
desc=self.ids.TaskDesc.text
lbl_name = Label(text=name)
lbl_desc = Label(text=desc)
bl.add_widget(lbl_name)
bl.add_widget(lbl_desc)
self.get_screen("second").ids.MainLayout.add_widget(bl)
class RegisterWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class MainScreenApp(App):
def build(self):
return kv
kv=Builder.load_file("mainscreen.kv")
if __name__ == "__main__":
MainScreenApp().run()
You have to put instances of both the screens inside the screen manager to be able to access them. Put this in the .kv file:
<WindowManager>:
CreateTask
SecondWindow
Then edit this in the .py file:
self.get_screen("second").ids.MainLayout.add_widget(bl)
to:
self.root.get_screen("second").ids.MainLayout.add_widget(bl)
Here's the explaination:
Since you were calling self from a method of CreateTask class, it was referring to the instance of CreateTask class. I told you to put an instance of CreateTask class as well as SecondWindow class inside the WindowManager. So this makes WindowManager the root of both CreateTask and SecondWindow classes. Therefore with respect to CreateTask, the WindowManager would be self.root. Hence with respect to CreateTask class, SecondWindow would be self.root.get_screen("second")

Adding custom view classes to Screen Manager

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.

Kivy - Adding widgets to a class

I am trying to add a widget to a class but it is not working.
Example:
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
Builder.load_string('''
<test>:
Label:
text: "Hi!"
''')
class test(GridLayout):
def build(self):
pass
testbutton=Button(text="This I want to show on test class!")
test().add_widget(testbutton)
class apprun(App):
def build(self):
return test()
apprun().run()
When run, this does not work.
It is generally good practice to explain how things don't work. For instance, you might have a useful python traceback including information about an exception that was raised, or you might simply mean you don't get the result you expect in which case you should explain how.
In this case, the obvious problem is that you try to inherit from app, when you mean App. Since app doesn't exist this would throw an exception.
You also try to add a widget to a class definition (test) rather than an instance of a class (test()). This will also fail, and if you don't see why you should read about the difference between class definitions and instances.
Also, widgets don't need a build method, this will do nothing.
Is this what you were going for?
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
Builder.load_string('''
<test>:
Label:
text: "Hi!"
''')
class test(GridLayout):
pass
class apprun(App):
def build(self):
t = test()
b = Button(text="This I want to show on test class!")
t.add_widget(b)
return t
apprun().run()
Or maybe this... so your widgets don't overlap each other.
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.lang import Builder
Builder.load_string('''
<test>:
Label:
text: "Hi!"
''')
class test(GridLayout):
pass
class apprun(App):
def build(self):
t = test(cols=1, size=Window.size)
b=Button(text="This I want to show on test class!")
t.add_widget(b)
return t
apprun().run()

Categories

Resources