>>> Let's run Python on Mobile

วันอังคาร, สิงหาคม 02, 2548

กำหนด Softkey สร้างMenu และ Set text on Screen(2)

Smartphone จะมี Softkey ใต้ Sceen ซ้ายและขวา
Softkeyขวา เรากำหนดให้เป็น Exit ด้วยคำสั่ง

appuifw.app.exit_key_handler=stop_now


คำสั่งที่ตามหลังเครื่องหมาย = จะเห็นว่าเป็นแบบไม่มีเครื่องหมาย () ตามหลัง นั่นคือเราไม่สามารถเรียกใช้คำสั่งต่างๆได้โดยตรงที่นี่ เราต้องกำหนดไว้ก่อน (ด้วย def )
Softkeyซ้ายก็คล้ายๆกัน เรากำหนดด้วยคำสั่ง

appuifw.app.menu=[(u'menu text1',funtion1),(u'menu text2',funtion2)]


menuมีได้หลายอัน จากตัวอย่างข้างบน menu text1 ต้องเป็นแบบ Unicode String เป็นตัวอักษรที่จะแสดงบนแต่ละ menu บนหน้าจอ Smartphone
ส่วนถัดมา (funtion1จากตัวอย่าง) เป็นคำสั่งที่เรากำหนดไว้ก่อนเป็นแบบไม่มีเครื่องหมาย () ตามหลัง
ลองมาดูโปรแกรมเต็มๆกัน
import appuifw ,e32
def info_hello():
     appuifw.note(u'hello','info')
def clear_screen():
     t.clear()
def note_with_clear():
     t.clear()
     appuifw.note(u'Clear text on screen','info')
def note_with_settext():
     t.set(u'set text on screen to hello\n')
     appuifw.note(u'Set text on screen to hello','info')
def note_with_add():
     t.add(u'add something\n')
     appuifw.note(u'Add text on screen body','info')
def stop_now():
     a.signal()
appuifw.app.title=u'test06'
t=appuifw.Text()
appuifw.app.body=t
t.set(u'set text on screen to hello\n')
appuifw.app.menu=[(u'hello menu',info_hello),(u'clear screen menu',clear_screen),(u'note with clear menu',note_with_clear),(u'note with add menu',note_with_add),(u'note with settext menu',note_with_settext)]
appuifw.app.exit_key_handler=stop_now
a=e32.Ao_lock()
a.wait()


โปรแกรม test06.py จะสร้าง menu 5อัน มี 5Function ที่เรากำหนดให้เรียกใช้
เช่น
appuifw.note() แสดงnoteสั้นๆ
t.clear() จะลบอักษรบน Text Screen Body
t.set() กำหนดอักษรบน Text Screen Body ทั้งหมด
t.add() เพิ่มอักษรบน Text Screen Body บริเวณ cusor
หากให้มีการขึ้นบรรทัดใหม่ ก็ใส่ \n ลงไปใน string


Codeตัวอย่าง
test06.zip

หน้าถัดไป >>>
หน้าก่อน
กลับไปหน้าหลัก

รายละเอียดเกี่ยวกับ Python for Symbian s60
>>> Introduction to Python
>>> Let's run Python for Symbian s60
>>> สั่งงานในPython ด้วย InterActiveConsole
>>> Editor บน Symbian ที่เขียนด้วย Python
>>> เริ่มต้นเขียนโปรแกรม กับ Python
>>>การสร้าง Function สร้าง Menu และกำหนด Exit
>>>And More...