E71:PyS60でテキストエディタを作ってみる
Last-modified: 2013-08-24 (土) 02:14:21 (3897d)
Top / E71:PyS60でテキストエディタを作ってみる
E71:PyS60でテキストエディタを作ってみる †
PyS60のアプリ作成の勉強がてら、テキストエディタを作ってみました。
■出来ること
ファイルは固定で「e:\other\text.txt」を使い
- ファイルのオープン
- 編集内容の保存
ができます。
勉強要素としては、
- 基本的なPyS60アプリの作り方について
- メニューの作り方について
- PyS60(Python)のエラー処理
- appuifw.Text
- PyS60の文字コード
以下はソースコード
■testedit.py
import appuifw import e32 import sys sys.setdefaultencoding('utf-8') text = appuifw.Text() appuifw.app.body = text appuifw.app.screen = 'normal' filename = "e:/others/text.txt" lock = e32.Ao_lock() def exit(): lock.signal() def menu_new(): text.set('') def menu_open(): try: f = open(filename,'r') text.set('') for line in f: text.add(unicode(line,'utf-8')) f.close() except IOError: appuifw.note(u'Cannot open file.','error') def menu_save(): out = text.get() try: f = open(filename,'w') f.write(out) f.flush() f.close() except IOError: appuifw.note(u'Cannot save file.','error') appuifw.app.exit_key_handler = exit appuifw.app.menu = [(u'new',menu_new),(u'open',menu_open),(u'save',menu_save)] lock.wait()
参考にしたサイト †
ライブラリリファレンス(公式?:日本語)のファイルオブジェクト
Textの使い方とか
ダイアログの使い方
ファイルの読み書きについて
文字コードのエンコーディング設定について