kivy

kivy戦記(13-2) observablelistが現れた

kivy
この記事は約17分で読めます。

私は、どこで間違ってしまったのか。

この、過ちの日々は、もう・・・・かえりました。

というわけで、ロールバック後のソースを再掲。

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.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')
class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)
class AllSCN(BoxLayout):
    tv = 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 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)
        #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):
        return AllSCN()
if __name__ == '__main__':
    FilelistApp().run()

filelist.kv


<AllSCN>:
    tv: tv
    orientation: 'vertical'
    BoxLayout: #OpnSCN
        size_hint_y: 0.1
        TextInput:
            size_hint_x: 0.8
            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:

実行画面はこう。

帰ってきたシンカリオン。

 

ここから、ファイル選択したら「ファイルパス」のテキストボックスに、選択したパスを表示させる改造をしよう。

で、あまり考えずにいろいろ修正する。

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.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')
class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)
class AllSCN(BoxLayout):
    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 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
        #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):
        return AllSCN()
if __name__ == '__main__':
    FilelistApp().run()

filelist.kv


<AllSCN>:
    tv: tv
    file_name: file_name
    orientation: 'vertical'
    BoxLayout: #OpnSCN
        size_hint_y: 0.1
        TextInput:
            id: file_name
            size_hint_x: 0.8
            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:

こいつを実行すると、

observablelist? なんなんだ、これはっ!

 

どうも、filelist.pyの

(省略な)
class AllSCN(BoxLayout): 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 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 #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() (また省略)

def load (self, path, filename):の、仮引数のfilenameがobservablelistだと言うことがわかった。

 

でも、ググったけど、なんかドキュメントが出てくるが、読んでもよくわからない。オール英語だし。

なんだかわからないけど、Listなんだし、先頭を指定したら、

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.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')
class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)
class AllSCN(BoxLayout):
    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 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):
        return AllSCN()
if __name__ == '__main__':
    FilelistApp().run()

filelist.kvは、変わらず。

で、実行したら、

これで、observablelistとなっているfilelistの先頭を指定することで、解決した。

 

しかし、その後、驚愕の事実が判明する。

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.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')
class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)
class AllSCN(BoxLayout):
    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 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):
        return AllSCN()
if __name__ == '__main__':
    FilelistApp().run()

 

この、コメントアウトをしているところで、使っているやんかーーーーーーーーーーーーー

 

タイトルとURLをコピーしました