In kivy using screenmanager how can I add other widgets like scatter? - python

I'm developing kivy app.
While I'm using ScreenManager, I can only add Screen in to it.
But Screen seems that it can add other widgets.
How can I add scatter widget to Screen for interface performance?
class sc(Scatter):
pass
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class manager(ScreenManager):
pass
class MyApp(App):
def build(self):
sm = manager()
s1 = Screen1()
sca = sc()
s1.add_widget(sca)
sm.add_widget(s1)
return sm
MyApp().run()

Your code does add a Scatter (what you call sca) to a Screen (what you call s1).

Related

cannot display progress bar in window

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

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.

Adding widgets to a screen subclass in .py file

I am using a screen manager and would like to add widgets to the screen subclass without using the .kv file.
class MainMenu(Screen):
def __init__(self, **kwargs):
gLayout = GridLayout()
gLayout.add_widget(Button(text = 'test'))
class Sis(App):
def build(self):
root = ScreenManager()
root.add_widget(MainMenu(name = 'mainMenu'))
root.current = 'mainMenu'
return root
Sis().run()
When I try to run the above code I get (pygame parachute) Segmentation Fault.
If I create the layout in the .kv file it works fine.
I've tried fiddling around with on_pre_enter and on_enter but I'm pretty sure I was using them wrong.
Any help is appreciated.
You forgot calling parent constructor of MainMenu class:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
class MainMenu(Screen):
def __init__(self, **kwargs):
super(MainMenu, self).__init__(**kwargs)
self.add_widget(Button(text = 'test'))
class Sis(App):
def build(self):
root = ScreenManager()
root.add_widget(MainMenu(name = 'mainMenu'))
root.current = 'mainMenu'
return root
Sis().run()

Categories

Resources