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()
Related
I don't know why when I run, the window won't show the buttons that I've added
Here is my code
from kivy.app import App
from kivy.metrics import dp
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
class StackLayoutExample(StackLayout):
def __int__(self, **kwargs):
super().__init__(**kwargs)
b = Button(text="A", size_hint=(0.2, 0.2))
self.add_widget(b)
class TheLabApp(App):
pass
TheLabApp().run()
and the .kv file is simply this:
StackLayoutExample:
You wrote the typo def __int__ but you need def __init__.
why isn't kivy show a text box for this code? please help me know how to let kivy add a text box by using this style.
thank you
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class textbox(GridLayout):
def __initial__(self, **kwargs):
super(MyGrid, self).__initial__(**kwards)
self.cols = 2
self.add_widger(Label(text = "Point for good"))
self.name = TextInput(multiline = False)
self.add_widget(self.name)
if __name__ == '__main__':
textbox().run()
For any kivy program, You need to inherit from App class and override the build method and place your UI elements here, and call run() method from the instance of the class inheriting the the App class
secondly, its add_widget() not add_widger and perhaps you mean __init__() instead of __initial__
Here is the corrected code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class TextBox(App):
def build(self):
grid = GridLayout()
grid.cols = 2
grid.add_widget(Label(text = "Point for good"))
name = TextInput(multiline = False)
grid.add_widget(name)
return grid
if __name__ == '__main__':
TextBox().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 wrote this python code to dyanamically create a BoxLayout inside a screen.
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class ListScreen(Screen):
def __init__(self,**kwargs):
super(ListScreen, self).__init__(**kwargs)
layout = BoxLayout(orientation ='vertical')
top_buttons=BoxLayout()
layout.add_widget(top_buttons)
top_buttons.add_widget(Button(text='Save'))
class ExampleApp(App):
def build(self):
root=ScreenManager()
root.add_widget(ListScreen(name='list'))
return root
ExampleApp().run()
It ran without any compilation error but the output is just a blank sreen.
The problem is that you have not added layout to the ListScreen instance:
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class ListScreen(Screen):
def __init__(self,**kwargs):
super(ListScreen, self).__init__(**kwargs)
layout = BoxLayout(orientation ='vertical')
self.add_widget(layout) #<<<<<<<<<<<<<<<<<
top_buttons=BoxLayout()
layout.add_widget(top_buttons)
layout.add_widget(Button(text='Save'))
class ExampleApp(App):
def build(self):
root=ListScreen()
root.add_widget(ListScreen(name='list'))
return root
ExampleApp().run()
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.