…いや、ソレ基本中の基本だろ?
fopen でパパっと…では無くて、GUIベースです。
なぜゆえこんなものを作ったかと申しますと、以前途中まで作って投げ出していたプログラムがありまして、いきなり難しい所から初めても続かないというのと、リハビリも兼ねて簡単な所から…
と、思ったんですが意外と面倒でしたね、ハイ。
## load_text ##
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
import os
from os.path import join,isdir
from kivy.uix.filechooser import FileChooserIconView
from kivy.uix.filechooser import FileChooserListView
class BtnChangeSelectFile(Button):
def on_release(self):
self.parent.DrawSelectFile()
class BtnSelectFile(Button):
def on_release(self):
self.parent.parent.lFile.text='\n'.join(self.parent.fc.selection)
self.parent.DrawBase()
class BtnChangeBase(Button):
def on_release(self):
self.parent.DrawBase()
class SubDisplay(BoxLayout):
def __init__(self,**kwargs):
super(SubDisplay,self).__init__(**kwargs)
self.fc=FileChooserListView()
#self.fc=FileChooserIconView()
self.fc.size_hint_y=7
self.fc.filters=['*.txt']
def DrawBase(self):
self.clear_widgets()
self.orientation='vertical'
outputtext = self.parent.lFile.text
if os.path.exists(self.parent.lFile.text):
outputtext = "exists"
with open(self.parent.lFile.text,'r') as f:
outputtext = f.read()
t1 =TextInput(text=outputtext,size_hint_y=4,multiline=True)
b1 =BtnChangeSelectFile(text='SelectFile',size_hint_y=1)
self.add_widget(t1)
self.add_widget(b1)
def DrawSelectFile(self):
self.clear_widgets()
self.orientation='vertical'
b1=BtnSelectFile(text='SelectFile',size_hint_y=1)
b2=BtnChangeBase(text='QUIT',size_hint_y=1)
self.add_widget(self.fc)
self.add_widget(b1)
self.add_widget(b2)
class Display(BoxLayout):
orientation='vertical'
def __init__(self,**kwargs):
super(Display,self).__init__(**kwargs)
self.lFile = Label(text='No Selected',size_hint_y=1)
self.boxSub = SubDisplay(size_hint_y=8)
self.add_widget(self.lFile)
self.add_widget(self.boxSub)
self.boxSub.DrawBase()
class LoadTextApp(App):
def build(self):
layout = Display()
return layout
if __name__=='__main__':
LoadTextApp().run()
この画面で SelectFile を押すとファイル選択画面に移行
この画面でテキストファイルを選択しつつ SelectFile を押すと前画面に戻りつつ、テキストファイルを読み込めた場合、中央に表示。
QUIT を選択した場合は何もせずに前画面に戻る。
既知のバグとして
複数のファイルを同時に選ぶと不安定ファイル選択画面に入りきらないファイル/ディレクトリがあると選択できない
等がありますが、今後改善するかは不透明です。
今後はこのプログラムをベースになんかしら作ろうと思います。
0 件のコメント:
コメントを投稿