次章へ行くため、ソースをkivy戦記(12) 画面遷移 (http://koh-sen.jp/blog/archives/7195)の時点まで復元させよう。

 

filelist.py


import os

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.uix.treeview import TreeViewLabel
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty

from pprint import pprint


from kivy.core.text import LabelBase, DEFAULT_FONT
from kivy.resources import resource_add_path

# 日本語フォント設定
resource_add_path('./fonts')
LabelBase.register(DEFAULT_FONT, 'ipaexg.ttf')

sm = ScreenManager()

class SetSCN(Screen):
    def okButtonClicked(self):
        sm.current = 'all'

    def cancelButtonClicked(self):
        sm.current = 'all'


class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)



class AllSCN(Screen):

    tv = ObjectProperty(None)
    file_name = ObjectProperty(None)
    loadfile = ObjectProperty(None)
    text_input = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(AllSCN, self).__init__(**kwargs)

        self.tv.add_node(TreeViewLabel(text ='シンカリオン E1 とき'))
        self.tv.add_node(TreeViewLabel(text ='シンカリオン 0 ひかり'))
        self.tv.add_node(TreeViewLabel(text ='シンカリオン キハ32 鉄道ホビートレイン'))

    def setupButtonClicked(self):
        sm.current = 'set'


    def show_load(self):
        content = LoadDialog(load = self.load, cancel = self.dismiss_popup)
        self._popup = Popup( title="読み込み中", content=content, size_hint=(0.9,0.9))
        self._popup.open()

    def load (self, path, filename):
        pprint(filename)
        self.file_name.text = filename[0]
        #with open (os.path.join(path, filename[0])) as stream:
        #    self.text_input.text = stream.read()
        self.dismiss_popup()

    def dismiss_popup(self):
        self._popup.dismiss()


class FilelistApp(App):
    def build(self):
        sm.add_widget(AllSCN(name='all'))
        sm.add_widget(SetSCN(name='set'))
        return sm


if __name__ == '__main__':
    FilelistApp().run()

filelist.kv


<SetSCN>:
    BoxLayout:
        orientation: 'vertical'

        BoxLayout:
            size_hint_y: 0.9
            Label:
                text: 'おほほ'
        BoxLayout:
            size_hint_y: 0.1
            Button:
                size_hint_x: 0.5
                text: 'OK'
                on_release: root.okButtonClicked()
            Button:
                size_hint_x: 0.5
                text: 'キャンセル'
                on_release: root.cancelButtonClicked()






<AllSCN>:

    tv: tv
    file_name: file_name

    BoxLayout:
        orientation: 'vertical'

        BoxLayout: #OpnSCN
            size_hint_y: 0.1
            orientation: 'horizontal'

            BoxLayout:
                Button:
                    size_hint_x: 0.1
                    text: '設定'
                    on_release: root.setupButtonClicked()
                TextInput:
                    id: file_name
                    size_hint_x: 0.7
                    text: 'ファイルパス'
                Button:
                    size_hint_x: 0.1
                    text: '…'
                    on_release: root.show_load()
                Button:
                    size_hint_x: 0.1
                    text: '実行'

        BoxLayout: #ResultSCN
            size_hint_y: 0.9

            TreeView:
                id: tv

<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: 'vertical'
        FileChooserListView:
            id: filechooser

        BoxLayout:
            size_hint_y : None
            height : 30
            Button:
                text: 'キャンセル'
                on_release: root.cancel()

            Button:
                text: '読み込み'
                on_release: root.load(filechooser.path, filechooser.selection)


AllSCN:

これで実行。

「設定」ボタン押下

「OK」ボタン押下

(実際には、もっとテストをしています)

 

これで、ようやく次に進める。

それにしても、前回から一ヶ月。

ひええええええええ、時間がかかりすぎてるよぅ。