搜尋感興趣的網誌

所有文章連結

2022年5月29日 星期日

python kivy app實作計算APP - 下

 

上篇把開頭都處理的差不多了,這篇開始要寫完整個APP,其中滾動的布局處理是花最多時間的,原因有兩個 :

1.      布局不管是GridLayout還是BoxLayout,都是單一方向,GridLayout是網格狀,方向無法控制,只能依照一開始定義多少的colsrows去排列。

2.      BoxLayout則是僅能設定直式或是橫式,第一行的格式大小方式,在第二行時無法使用變更,會產生錯誤或是全部不見,所以遇到下面的圖形排列時,就會無法處理,這部分若是使用kv file可以很方便的解決,網路上的資訊幾乎都是以kv file形式解決,但小雷一開始就打算完全使用Python,所以也花了不少時間去研究。


最後解決的辦法就是創造一個主要的GridLayout以及好幾個橫式的BoxLayout,分別把各個函式套用進去之後再添加回主要的布局之中,總算是不會衝突並且可以正常執行,滾動的scrollview要注意size_hint_y必須是None,才能正常滾動,只要設定值不是None,整個滾動頁面就會產生錯誤甚至閃退

# 建立滾動處
    def create_scrollview(self):
        # 主要布局
        self.layout = GridLayout(cols=1, size_hint_y=None)
        self.layout.spacing = 20
        self.layout.bind(minimum_height=self.layout.setter("height"))
 
        # 橫向布局_0
        self.hori = BoxLayout(orientation="horizontal", size_hint_y=None)
        self.hori.spacing = 10
 
        # 橫向布局_1
        self.one_hori = BoxLayout(orientation="horizontal", size_hint_y=None)
        self.one_hori.spacing = 10
 
        # 橫向布局_2
        self.two_hori = BoxLayout(orientation="horizontal", size_hint_y=None)
        self.two_hori.spacing = 10


 接著處理要顯示的按鈕、標籤與輸入框,按照每一行對應的Layout進行添加

        # boxlayout
        self.one_button = MyButton(
            text="A方案",
            size_hint=(0.2, None))
        self.one_button.bind(on_press=self.one_info)
        self.hori.add_widget(self.one_button)
 
        self.two_button = MyButton(
            text="B方案",
            size_hint=(0.2, None))
        self.two_button.bind(on_press=self.two_info)
        self.hori.add_widget(self.two_button)
 
        self.three_button = MyButton(
            text="C方案",
            size_hint=(0.2, None))
        self.three_button.bind(on_press=self.three_info)
        self.hori.add_widget(self.three_button)
 
        self.four_button = MyButton(
            text="D方案",
            size_hint=(0.2, None))
        self.four_button.bind(on_press=self.four_info)
        self.hori.add_widget(self.four_button)

 

滾動到尾部時可以添加一些空白的Label,防止手機鍵盤彈出時不好打字,對於滾動的順暢度也會比較好

        # 增加的空格
        for i in range(10):
            self.test = Label(
                size_hint=(0.2, None)
            )
            self.layout.add_widget(self.test)

 

把滾動的函式添加回主要的Layout與要滾動的範圍

        # 導入滾動函式
        scrollview = ScrollView(size=(Window.width, Window.height))
        scrollview.add_widget(self.layout)
        self.window.add_widget(scrollview)


最後將所有的計算公式與回傳值編寫進函式,要顯示的文字編寫好與準備測試

# boxlayout
        self.one_button = MyButton(
            text="A方案",
            size_hint=(0.2, None))
        self.one_button.bind(on_press=self.one_info)
        self.hori.add_widget(self.one_button)
 
        self.two_button = MyButton(
            text="B方案",
            size_hint=(0.2, None))
        self.two_button.bind(on_press=self.two_info)
        self.hori.add_widget(self.two_button)

 

這邊需要注意幾件事情 :

1. 按鈕按下去之後,並不是依照按鈕上的文字去做傳輸,以one_button為例,雖然設定的文字是A方案,但實際列印出來的卻不是文字,所以輸出的時候要注意型態是否有正確

self.one_button = MyButton(
            text="A方案",
            size_hint=(0.2, None))
 
        print(self.one_button)
<__main__.MyButton object at 0x000001E3A4BB3C80>
 
        print(self.one.button.text)
A方案
 

2. 再來就是數字計算完成之後,要輸出到Label時需要轉換為字符chr,否則會產生錯誤,Label只接受字符

self.referral = 20 / 100
self.bouns_output = str(self.bouns_calculation)

3. 最後就是當按鈕或是文字沒有輸入時,APP會閃退,所以函式中可以善用try..except去檢測錯誤並給予回傳的值。

        # 獎金A判斷
        try:
            if self.bouns_output == str(self.bouns_calculation):
                self.bouns_calculation = self.bouns_calculation
            else:
                pass
        except:
            self.bouns_calculation = str("0")


到這邊已經APP本體已經完成了,剩下的就是打包成.apk並傳送到手機安裝進行測試啦!


沒有留言:

張貼留言

其他文章

看看精選文章

納希克房價分析 | Nashik Apartment Price Analyze – 語法解析(上)

  這次 Nashik 的房價分析有上傳至 Kaggle ,有興趣的朋友可以前往閱覽, RMarkdown PDF 報告存放在 Google 雲端,程式碼則是存放於 Github ,照慣例會分享好用的函式語法,雖說基本的 Packages 與語法可能很多人都會完整的閱覽,但是實際...