本篇開始:
- 我的開發環境是win11
- python是3.10.7版本
- kivy我設定在2.1.0版本
- 編碼的工具是Visual Studio Code(VS Code)
【本篇說明】上一篇分享一些 TextInput部件基本用法,今天接續分享 TextInput部件的文字(背景)顏色及游標使用。
首先在main.py寫下固定的程式碼
以下.py程式碼:(在vscode開啟python檔案,取名為main.py,以下簡稱.py)from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class firstlayout(BoxLayout):
pass
class Mainapp(App):
def build(self):
return firstlayout()
if __name__ == "__main__":
Mainapp().run()
▲說明:
1.從 kivy.uix.boxlayout這個模組導入 BoxLayout類別 (如果還沒看過,可參考:【kivy layout】BoxLayout類別的使用方法)
2.創建一個class 類別取名為 firstlayout(可自取無限定命名),然後在()填入 BoxLayout,目的是為了繼承 BoxLayout類別所有特性3.這邊 kivy language(kv),我使用的是"相同命名方式",如果不太懂我說的,可先參考這篇:【kivy language】如何使用 kivy language(kv)

▲說明:main.py程式碼
在vscode新增 kivy檔案,我取名 main.kv,並且放在同資料夾內

▲說明:我的是放在以下路徑:桌面-> python-> Kivy-> test
加入 TextInput文字及背景顏色
以下.kv程式碼:(在vscode編輯 main.kv,簡稱.kv)
<firstlayout>:
orientation: 'vertical'
Label:
text:'i am a label!'
font_size: 30
TextInput:
text:'i am a textinput'
font_size: 30
foreground_color: 0 ,0 ,0
background_color: 117/255 ,220/255 ,141/255
halign: 'right'
▲說明:
1.加入<firstlayout> : (記得要使用角括號 < >),目的使用"main.py"裡面的類別 firstlayout,也就是 BoxLayout類別的特性
2.在 <firstlayout> 裡面,需先縮排,然後加入 orientation參數
3.orientation:代表排列方向,有區分水平(horizontal)及垂直(vertical)方向,這邊填入 'vertical',要記得加引號''
4.加入 Label部件(可參考這篇:【kivy Label】經常使用的Label(標籤)語法)
5.添加 TextInput部件,使用方式為縮排後加上 TextInput部件名稱,再加上冒號:
6.text: '顯示的字串'
7.font_size: 字體大小
8.【本次新增】foreground_color: 文字顏色(黑色),【注意】這邊跟其他部件使用的參數(font_color)不同
9.【本次新增】background_color: 背景顏色(草綠色)
10.halign: 'text字串對齊方向',可填入 left、right、center,要記得加引號''

▲說明:main.kv程式碼

▲說明:執行 python檔案,看看顏色是否有效果
加入游標設定
以下.kv程式碼:(在 VSCode編輯 main.kv)
<firstlayout>:
orientation: 'vertical'
Label:
text:'i am a label!'
font_size: 30
TextInput:
text:'i am a textinput'
font_size: 30 # 文字大小
foreground_color: 0 ,0 ,0 # 文字顏色
background_color: 117/255 ,220/255 ,141/255 #背景顏色
halign: 'right'
cursor_color: 'green' # 游標顏色
cursor_blink: False # 游標釘住
cursor_width:5 # 游標寬度
▲說明:(以下均縮排)
1.cursor_color: 游標顏色
2.cursor_blink: 游標閃爍,預設為 True,這邊游標釘住,改 False
3.cursor_width: 游標寬度

▲說明:main.kv程式碼

▲說明:執行 python檔案,點擊 TextInput,游標為紅框標示處
改成提示文字
以下.kv程式碼:(在 VSCode編輯 main.kv)
<firstlayout>:
orientation: 'vertical'
Label:
text:'i am a label!'
font_size: 30
TextInput:
font_size: 30 # 文字大小
foreground_color: 0 ,0 ,0 # 文字顏色
background_color: 117/255 ,220/255 ,141/255 #背景顏色
halign: 'right'
cursor_color: 'green' # 游標顏色
cursor_blink: False # 游標釘住
cursor_width:5 # 游標寬度
hint_text: 'type password' # 提示字串
# hint_text_color: 10 ,10 ,10 # 提示顏色
▲說明:(以下均縮排)
1.hint_text: 提示字串
2.hint_text_color: 提示字顏色(預設為淺灰色,大部分人的習慣)

▲說明:main.kv程式碼

▲說明:執行 python檔案
將 TextInput改成密碼模式
以下.kv程式碼:(在 VSCode編輯 main.kv)
<firstlayout>:
orientation: 'vertical'
Label:
text:'i am a label!'
font_size: 30
TextInput:
font_size: 30 # 文字大小
foreground_color: 0 ,0 ,0 # 文字顏色
background_color: 117/255 ,220/255 ,141/255 #背景顏色
halign: 'right'
cursor_color: 'green' # 游標顏色
cursor_blink: False # 游標釘住
cursor_width:5 # 游標寬度
hint_text: 'type password' # 提示字串
password: True # 開啟密碼模式
password_mask: '#' # 預設為'*'
▲說明:(以下均縮排)
1.password: 密碼模式,預設為 False
2.password_mask: 預設為'*'

▲說明:main.kv程式碼

▲說明:執行 python檔案,輸入文字後會以 # 表達
本篇小結
以上就是接續說明 TextInput部件的基本使用,接下來會加入觸發功能,其他篇章再來分享進階的使用方式。
本篇結束:
在自學路上遇到困難是很正常的事,只要堅持到底,相信就會有所成果,期勉大家一同努力。




















