以我實際開發的APP為例,
我想做的是可以訓練基本加(減)法的心算工具。
命名app叫 「CountInMind」。
☆目的:按下其他 Button同時呼叫類別中方法,回傳列印出訊息,並將模式內容儲存下來在後面他用(要在app第3頁可以綜合顯示出前面選擇的內容)
.py程式碼同上一篇
以下為.kv程式碼:
Button:
text:'減法'
font_name: './font/TW-Kai-98_1.ttf'
font_size: 23
color: root.text_color
size_hint:0.3, 0.1
pos_hint: {'center_x':0.5, 'center_y':0.25}
background_normal: ''
background_color: root.btn_bg_color
on_release: root.manager.current = 'second'; root.select_mode('減法【一開始為30】')
Button:
text:'加(減)法'
font_name: './font/TW-Kai-98_1.ttf'
font_size: 23
color: root.text_color
size_hint:0.3, 0.1
pos_hint: {'center_x':0.5, 'center_y':0.14}
background_normal: ''
background_color: root.btn_bg_color
on_release: root.manager.current = 'second'; root.select_mode(self.text)
▲說明:root代表根物件(Firstscreen類別),呼叫類別中 select_mode方法,並將文字回傳至該方法的參數kind,並列印出來
▲說明:列印出"減法【一開始為30】"
▲說明:列印出"加(減)法"
☆目的:按下 Button同時呼叫類別中方法,並回傳列印出訊息
☆在.py設定Secondscreen類別中 select_degree方法
以下為.py程式碼:
class Secondscreen(Screen):
text_color = Firstscreen.text_color
btn_bg_color = Firstscreen.btn_bg_color
def select_degree(self, num):
print(num)
▲說明:使用調用屬性,從 Firstscreen類別調用 text_color屬性內容來用,btn_bg_color屬性也調用。另外定義 select_degree這個方法。
以下為.kv程式碼:
DegreeButton:
text: '易'
font_name: './font/TW-Kai-98_1.ttf'
font_size: 23
color: root.text_color
size_hint: 0.2, 0.15
pos_hint: {'center_x':0.27, 'center_y':0.3}
on_release: root.manager.current = 'third'; root.select_degree(4)
▲說明:root代表根物件(Secondscreen類別),呼叫類別中 select_degree方法,並將'4'回傳至select_degree的參數num,並列印出來。
▲說明:按下DegreeButton(易)後,畫面切換到app第3頁,並列印出'4'
☆目的:想要在首頁選擇模式(select_degree)後,能將難易度內容儲存下來在後面他用(要在app第3頁可以綜合顯示出前面選擇的內容)
☆在.py設定 Secondscreen類別degree物件,導入類別並且繼承(NumericProperty)
以下為.py程式碼:
from kivy.properties import ColorProperty, ObjectProperty, NumericProperty
class Secondscreen(Screen):
text_color = Firstscreen.text_color
btn_bg_color = Firstscreen.btn_bg_color
description_of_degree = "程度說明:\n(易)適合國小中年級\n(中)適合國小高年級\n(難)適合國中一年級\n\n\n~~請點擊以下程度~~"
degree = NumericProperty()
def select_degree(self, num):
# 可調用該類別(select_degree)方法內屬性(degree)
Secondscreen.degree = num
print(self.degree) # 內外都會變
▲說明:將4同時儲存在degree這個物件,並列印出來確認
設定類別的方法(method)及屬性(property),試過很多語法,只有這樣才能調用Secondscreen類別degree內容,否則會無法正確調用。
為精簡篇幅,本篇所使用完整(.py)(.kv)程式碼,請參考第9~11篇內容
在自學路上遇到困難是很正常的事,只要堅持到底,相信就會有所成果,期勉大家一同努力。