class myBase(FloatLayout):
basic_layout_wid = ObjectProperty(None) #object of layout
basic_layout_main_place_wid = ObjectProperty(None) #object of main
def do_action(self):
wid = myBasicInfo
self.basic_layout_main_place_wid.add_widget(wid.info_wid)
class myApp(App):
def build(self):
return myBase(info='Hello world')
if __name__ in ('__android__', '__main__'):
myApp().run()
--------------------------------------------------------------------------
and of course kv
--------------------------------------------------------------------------
#:kivy 1.0
<myBasicInfo>:
id: my_basic_info
info_wid: my_info_main
Label:
id: my_info_main
text: 'Not Connected'
height: 20
<myBase>:
id: my_widget
basic_layout_wid: my_basic_layout
basic_layout_main_place_wid: my_main_place
StackLayout:
id: my_basic_layout
anchor_x: 'left'
anchor_y: 'top'
Button:
size: (50, 20)
text: 'Home'
on_press: root.do_action();
StackLayout:
id: my_main_place
------------------------------------------------
What I am trying to do is, when I push a button HOME I want to put
content of myBasicInfo into my_main_place
I can do it by using property of class myBasicInfo.info_label:
def do_action(self):
wid = myBasicInfo
self.basic_layout_main_place_wid.add_widget(wid.info_label)
-----, but I want to use kv definition..
Any help???
to do it from KV 1. register widget by factory
------------main.kv
#:kivy 1.0
#:import SomeWidget main.SomeWidget
# rules for the widget
<SomeWidget>:
BoxLayout:
pos: root.pos
size: root.size
orientation: "vertical"
Label:
text: "hello"
Button:
text: "world"
# root
BoxLayout:
id: layout
Button:
text: "add"
on_press: layout.add_widget(SomeWidget())
------------main.py
#!/usr/bin/env python
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.factory import Factory
class SomeWidget(Widget):
pass
Factory.register('SomeWidget', SomeWidget)
class Main(App):
pass
Main().run()
------main.py
#!/usr/bin/env python
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
class SomeWidget(Widget):
pass
Factory.register('SomeWidget', SomeWidget)
class myOBDBase(BoxLayout):
layout_wid = ObjectProperty(None) #object of layout
def on_click(self):
print "Hello"
self.layout_wid.add_widget(SomeWidget())
class Main(App):
def build(self):
return myOBDBase(info='Hello world')
Main().run()
------main.kv
#:kivy 1.0
#:import SomeWidget main.SomeWidget
# rules for the widget
<SomeWidget>:
BoxLayout:
pos: root.pos
size: root.size
orientation: "vertical"
Label:
text: "hello"
Button:
text: "world"
# root
<myOBDBase>:
layout_wid: layout
BoxLayout:
id: layout
Button:
text: "add KV"
on_press: layout.add_widget(SomeWidget());
Button:
text: "add PY"
on_press: root.on_click()
#:kivy 1.0#:import Button kivy.uix.button
BoxLayout: id: test Button: text: 'Add Device' on_press: test.add_widget(Button(text = 'Hello World'))from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.app import App
class MyMainWidget(BoxLayout):
#Since there aren't any real callbacks in this widget, we pass.
pass
class AddWidgetApp(App):
def build(self):
#You need to actually DO something in `build` for your app to work.
app = MyMainWidget()
return app
Builder.load_string("""
#Here, we design & define the parameters of `MyMainWidget`
<MyMainWidget>:
id: test
Button:
text: 'Add Device'
on_press: test.add_widget(Button(text='Hello World'))
""") File "_event.pyx", line 281, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:4152) File "<string>", line 1, in <module> TypeError: 'module' object is not callable File "_event.pyx", line 281, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:4152) File "...\addwidget.kv", line 1, in <module> #:kivy 1.0 TypeError: 'module' object is not callableBuilder.load_string("""
#:import Button kivy.uix.button.Button
...
""")class AddWidgetApp(App):
def build(self): pass
Builder.load_string("""#:import Button kivy.uix.button.Button
BoxLayout: id: test Button: text: 'Add Device'[MyButton@Button]: text: 'My Button'class MyButton(Button): pass
Factory.register('MyButton', cls=MyButton)#:import MyButton main.MyButton
Unable to import package 'main.MyButton'
<MyButton@Button>: text: 'My Button' Button: text: 'Add Button' on_press: test.add_widget(MyButton(text='Hello World')) NameError: name 'MyButton' is not defined
property : python code # with `id`sclass MyButton(Button):
pass
class Test(BoxLayout):
def some_callback(self, *args):
self.add_widget(MyButton())
############################################
<MyButton>:
text: 'My Button'
<Test>:
btn: btn_id
Button:
id: btn_id
text: 'Add Button'
on_press: root.some_callback(*args)from kivy.app import Appfrom kivy.uix.screenmanager import ScreenManager, Screenfrom kivy.uix.textinput import TextInputfrom kivy.uix.dropdown import DropDownfrom kivy.uix.button import Buttonfrom kivy.properties import NumericProperty, StringPropertyfrom kivy.lang import Builderfrom kivy.storage.jsonstore import JsonStorefrom os.path import joinfrom kivy.uix.widget import Widgetfrom kivy.factory import Factory
Builder.load_string('''#:import random random.random#:import SlideTransition kivy.uix.screenmanager.SlideTransition#:import NoTransition kivy.uix.screenmanager.NoTransition#:import CharactersScreen main.CharactersScreen
<BasicScreen>: hue: random() canvas: Color: hsv: self.hue, .5, .3 Rectangle: size: self.size
Label: font_size: 42 text: root.name pos_hint: {'center_y': 0.95}
BoxLayout: size_hint: None, 1 pos_hint: {'center_y': .5} orientation: 'vertical'
Button: text: 'Characters!' on_release: root.manager.current = 'Characters' Button: text: 'Identity' on_release: root.manager.current = 'Identity' Button: text: 'Abilities' on_release: root.manager.current = 'Abilities' Button: text: 'Attacks' on_release: root.manager.current = 'Attacks' Button: text: 'Health' on_release: root.manager.current = 'Health' Button: text: 'Powers' on_release: root.manager.current = 'Powers' Button: text: 'Skills' on_release: root.manager.current = 'Skills' Button: text: 'Feats' on_release: root.manager.current = 'Feats' Button: text: 'Equipment' on_release: root.manager.current = 'Equipment' Button: text: 'Rituals' on_release: root.manager.current = 'Rituals' Button: text: 'Notes' on_release: root.manager.current = 'Notes'
<CharactersScreen>: Button: text: 'Save' pos_hint: {'center_x': 0.95, 'center_y': .95} size_hint: .1, .1 on_release: root.save()
Button: text: 'New' pos_hint: {'center_x': 0.85, 'center_y': .95} size_hint: .1, .1 on_release: self.createNew() Button: text: '+' size_hint: .05, .05 pos_hint: {'center_x': 0.34, 'center_y': 0.8} on_release: root.createNew() TextInput: id: newchar text: 'enter name here' multiline: False pos_hint: {'center_x': 0.5, 'center_y': 0.8} size_hint: 0.25, None height: 30 StackLayout: id: chargrid size_hint: .5,.5 pos_hint: {'center_x': .5,'center_y': .5}
<IdentityScreen>: BoxLayout: size_hint: .25, .75 pos_hint: {'center_x': 0.6,'center_y': .5} orientation: 'vertical'
Button: text: 'Name' on_release: root.yopo()
TextInput: id: charname text: '' multiline: False
Label: text: 'Level'
TextInput: id: charlevel text: '' multiline: False
Label: text: 'XP'
TextInput: id: charxp text: '' multiline: False
Label: text: 'Race'
TextInput: id: charrace text: '' multiline: False
Label: text: 'Class'
TextInput: id: charclass text: '' multiline: False
Label: text: 'Paragon'
TextInput: id: charparagon text: '' multiline: False
Label: text: 'Epic'
TextInput: id: charepic text: '' multiline: False
Label: text: 'Misc.'
TextInput: id: charmisc text: '' multiline: False
<AbilitiesScreen>: GridLayout: cols: 5 size_hint: .3, .5 pos_hint: {'center_x': .5,'center_y': .5}
Label: text: 'Abil' Label: text: 'Score' Label: text: 'Mod' Label: text: 'Defense' Label: text: 'Score' Label: text: 'Str' TextInput: text: '' multiline: False Label: text: '0' Label: text: 'AC' Label: text: '15' Label: text: 'Con' TextInput: text: '' multiline: False Label: text: '0' Label: text: 'AC' Label: text: '15' Label: text: 'Dex' TextInput: text: '' multiline: False Label: text: '0' Label: text: 'AC' Label: text: '15' Label: text: 'Int' TextInput: text: '' multiline: False Label: text: '0' Label: text: 'AC' Label: text: '15' Label: text: 'Wis' TextInput: text: '' multiline: False Label: text: '0' Label: text: 'AC' Label: text: '15' Label: text: 'Cha' TextInput: text: '' multiline: False Label: text: '0' Label: text: 'AC' Label: text: '15'
ScreenManager: id: screen_manager CharactersScreen: id: characters_screen
''')
class BasicScreen(Screen): hue = NumericProperty(0) def save(self): data_dir = App().user_data_dir store = JsonStore(join(data_dir, 'dandy.json')) cname = self.ids.charname.text crace = self.ids.charrace.text store.put(cname,race=crace)
class IdentityScreen(BasicScreen): pass
class CharactersScreen(BasicScreen): def loadem(self): data_dir = App().user_data_dir store = JsonStore(join(data_dir, 'dandy.json')) chars = store.keys() for i in range(len(chars)): self.add_button(chars[i]) def loadCharData(self,char): data_dir = App().user_data_dir store = JsonStore(join(data_dir, 'dandy.json')) self.ids.charname.text = 'Dave' self.ids.charrace.text = 'Human' def createNew(self): data_dir = App().user_data_dir store = JsonStore(join(data_dir, 'dandy.json')) store.put(self.ids.newchar.text) self.add_button(self.ids.newchar.text) def add_button(self,name): btn = Button(text=name) self.ids.chargrid.add_widget(btn) btn.bind(on_press=self.callback) def callback(self,instance): self.loadCharData('%s')
class AbilitiesScreen(BasicScreen): pass
Factory.register('CharactersScreen', CharactersScreen)
class ScreenManagerApp(App): title = 'Dandy' def build(self): pages = ['Attacks','Health','Powers','Skills','Feats','Equipment','Rituals','Notes'] root = ScreenManager() #charscreen = CharactersScreen(name='Characters') #Factory.register('CharactersScreen', cls=CharactersScreen) #idscreen = IdentityScreen(name='Identity') #root.add_widget(charscreen) #root.add_widget(idscreen) #root.add_widget(AbilitiesScreen(name='Abilities')) for i in range(len(pages)): root.add_widget(BasicScreen(name=pages[i])) #charscreen.loadem() return root
if __name__ == '__main__': ScreenManagerApp().run()