Adding custom view classes to Screen Manager - python

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.

Related

Access widget properties inside of add_widget method

I want to make a custom set of widgets and in some of them I would like to access specific widgets' properties inside of the add_widget(widget, index, canvas)method.
The problem is that the properties are always empty. This could be because I access them early but how do I fix it?
My Code:
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.label import MDLabel
from kivymd.app import MDApp
class CustomWidget(BoxLayout):
def add_widget(self, widget, index=0, canvas=None):
if isinstance(widget, MDLabel):
# Why is the text empty?
# Also how can I get the text value?
print("{}'s text is: {}".format(widget, widget.text))
else:
super(CustomWidget, self).add_widget(widget, index, canvas)
class MainApp(MDApp):
def __init__(self, **kwargs):
super(MainApp, self).__init__(**kwargs)
self.kv = Builder.load_string('''
#:kivy 2.0.0
CustomWidget:
MDLabel:
text: "Some text"
''')
def build(self):
return self.kv
if __name__ == '__main__':
MainApp().run()
The text is empty because it hasn't been set yet at the time it's added to the parent.
If you care about what its value changes to, you can bind to the new child widget's text property in add_widget so that you are notified when it changes and can act accordingly.

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

How to access classes using kivy?

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

Blank screen with kivy

I am new to kivy and using some tutorials but i only get black screen while on the videos developpers get their widget on the screen. i've tried multiple solutions but i dont really understand what the problem is. I am trying to build a login screen here is the code i wrote :
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
class LoginScreen(BoxLayout):
def __int__(self, **kwargs):
super(LoginScreen,self).__int__(**kwargs)
sm=ScreenManager
Screen=Screen(name="screen")
layout=BoxLayout(orientation='vertical')
self.Username = TextInput(multiline=False)
self.Password = TextInput(multiline=False, password=True)
layout.add_widget(Label(text="username"))
layout.add_widget(self.Username)
layout.add_widget(Label(text="password"))
layout.add_widget(self.Password)
Screen.add_widget(layout)
sm.add_widget(Screen)
return sm
class simplekivy(App):
def build(self):
return LoginScreen()
if __name__ == "__main__":
simplekivy().run()
Thank you for your help !
Your code has the following errors:
The constructor method is called __init__, it is not called __int__.
The constructor method should not return anything
The variables should not be called the same as the classes or functions, I mean the following code: Screen=Screen(name="screen").
It is best to segment is to separate your code, with the name of the class LoginScreen I think you want to make a Screen, so create another class that is the ScreenManager.
Another error is that you have not imported Label.
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class LoginScreen(Screen):
def __init__(self, *args, **kwargs):
super(LoginScreen,self).__init__(*args, **kwargs)
layout=BoxLayout(orientation='vertical')
self.Username = TextInput(multiline=False)
self.Password = TextInput(multiline=False, password=True)
layout.add_widget(Label(text="username"))
layout.add_widget(self.Username)
layout.add_widget(Label(text="password"))
layout.add_widget(self.Password)
self.add_widget(layout)
class Manager(ScreenManager):
def __init__(self, *args, **kwargs):
super(ScreenManager,self).__init__(*args, **kwargs)
self.add_widget(LoginScreen())
class simplekivy(App):
def build(self):
return Manager()
if __name__ == "__main__":
simplekivy().run()

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