###前提・実現したいこと
今、kivyファイルを1枚で作ってます。
ScreenManagerで画面切り替えをできるようにしています。
test.kvの中身が増えてきて、ごちゃごちゃしてきたので画面の処理ごとにkvファイルを作りたいと思ってます。
サイトのソースをそのままコピーしてみましたが、エラーが発生しました。
kivyのバージョンが違うのでしょうか?
サイト名:Qiita 項目:Python Kivy で外部KvファイルをIncludeして使用する
######main.py(サイトのソース)
python
from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.properties import ObjectProperty class RootWidget(BoxLayout): button_include = ObjectProperty() def print_obj(self, obj): print(obj) def check_button_include(self): print(self.button_include) class TestApp(App): def build(self): return RootWidget() if __name__ == '__main__': TestApp().run()
######test.kv(サイトのソース)
python
#:include gui/tab_test.kv <RootWidget>: button_include: tab_button_include.button_include TabbedPanel: do_default_tab: False TabbedPanelItem: text: 'Tab1' BoxLayout: Button: text: 'button 1' on_press: root.print_obj(self) Button: text: 'button 2' on_press: root.check_button_include() TabbedPanelItem: text: 'Tab2' ButtonInclude: id: tab_button_include
######gui/tab_test.kv(サイトのソース)
python
<ButtonInclude@Button>: button_include: button_include id: button_include text: 'button include' on_press: app.root.print_obj(self)
######main.py(私のソース)
python
# -*- coding: utf-8 -*- import kivy kivy.require('1.7.3') from kivy.lang import Builder from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.widget import Widget from kivy.core.window import Window from kivy.properties import StringProperty,ListProperty Window.clearcolor=[0.999,0.89,0.999,1] import csv import codecs import sys from kivy.core.text import LabelBase, DEFAULT_FONT from kivy.resources import resource_add_path # デフォルトに使用するフォントを変更する resource_add_path('./fonts') LabelBase.register(DEFAULT_FONT, 'mplus-2c-regular.ttf') #日本語が使用できるように日本語フォントを指定する class TextWidget(BoxLayout): text = StringProperty() def __init__(self, **kwargs): super(TextWidget, self).__init__(**kwargs) self.text = 'Good' def buttonClicked(self): self.text=self.ids["text_box"].text self.text2=self.ids["text_box2"].text word=[[self.text,self.text2]] f = codecs.open('sample.csv',"a","utf-8") writer = csv.writer(f) #writer.writerow(self.text) writer.writerows(word) f.close def buttonClicked2(self): f = codecs.open('sample.csv',"r","utf-8") reader = csv.reader(f) for row in reader: print(row[0] + " " + row[1]) f.close() def buttonClicked_tops(self): pass def buttonClicked_bottoms(self): bird = Bird() self.add_widget(bird) def buttonClicked_autor(self): pass class TestApp(App): def __init__(self, **kwargs): super(TestApp, self).__init__(**kwargs) self.title = 'Apomol' reload(sys) sys.setdefaultencoding('utf-8') Builder.load_file('main.kv') def build(self): return TextWidget() if __name__ == '__main__': TestApp().run()
#####test.py
TextWidget: # ルートに追加 <TextWidget>: orientation: 'vertical' ScreenManager: size_hint_y: 0.7 id: scrmgr Screen: name: 'top' Screen: #ここの中身を別kvにしたい name: 'main1' BoxLayout: orientation:'vertical' size:root.size Label: id:label2 font_size:20 text:'Select the type' color:[0.2,0.2,0.2,1] BoxLayout: Label: size: 50,50 size_hint: None, None Button: text:"トップス" size: 150,150 size_hint: None, None Image: source: "bird.png" center_x: self.parent.center_x center_y: self.parent.center_y Button: size: 150,150 size_hint: None, None Image: source: "bird.png" center_x: self.parent.center_x center_y: self.parent.center_y Button: id:button_bottoms text:"bottoms" size_hint_y:0.3 on_press:scrmgr.current = 'bottoms' Button: id:button_autor text:"autor" size_hint_y:0.3 on_press:scrmgr.current = 'autor' Screen: #ここの中身を別kvにしたい name: 'main2' BoxLayout: orientation:'vertical' size:root.size Label: id:label1 font_size:20 text:"洋服の登録をします" color:[0.2,0.2,0.2,1] BoxLayout: TextInput: id:text_box focus:True text:"名前" on_text_validate:root.buttonClicked() TextInput: id:text_box2 focus:True text:"色" on_text_validate:root.buttonClicked() Label: id:label_pass1 Label: id:label_pass11 BoxLayout: Label: id:label_pass1 Button: id:button1 text:"登録" size_hint_x:1 size_hint_y:1 on_press:root.buttonClicked() Label: id:label_pass2 Button: id:button2 text:"表示" size_hint_x:1 size_hint_y:1 on_press:root.buttonClicked2() Label: id:label_pass3 Screen: #main1から派生するScreen name: 'tops' BoxLayout: orientation:'vertical' BoxLayout: Button: id:back_tops Image: source: "bird.png" BoxLayout: Button: id:back_tops text:'back' size_hint_x:1 on_press:scrmgr.current = 'main1' Screen: #main1から派生するScreen name: 'bottoms' Screen: #main1から派生するScreen name: 'autor' BoxLayout: size_hint_y: 0.1 padding: 10,10,20, 10 Button: text: "top" font_size: 20 on_press: scrmgr.current = 'top' Button: text: "main1" font_size: 20 on_press: scrmgr.current = 'main1' Button: text: "main2" font_size: 20 on_press: scrmgr.current = 'main2'
###補足情報(言語/FW/ツール等のバージョンなど)
python2.7
kivy
まだ回答がついていません
会員登録して回答してみよう