前提
Kivyにて、以下のような内容のKVファイルからidがusernameとpasswordのウィジェットを.pyファイルで扱うにはどのようにすればよいですか?
また、下で示すやり方以外にもっと効率的な記述方法があれば教えてください。
該当のソースコード
KVファイル
KV
1# -*- coding: utf-8 -*- 2Root: 3<Root>: 4 canvas.before: 5 Color: 6 rgb: 1,1,1 7 Rectangle: 8 pos: self.pos 9 size: self.size 10 11<Login>: 12 BoxLayout: 13 id: box 14 orientation: 'vertical' 15 BoxLayout: 16 id: username_box_box 17 orientation:'horizontal' 18 Label: 19 text:'メールアドレス:' 20 BoxLayout: 21 id: username_box 22 orientation:'vertical' 23 Label: #ユーザネーム上 24 text:' ' 25 UserName: #このウィジェットを取得したい 26 id: username 27 Label: #ユーザネーム下 28 text:' ' 29 Label: 30 text:'@gmail.com' 31 BoxLayout: 32 id: password_box_box 33 orientation:'horizontal' 34 Label: 35 text:'パスワード:' 36 BoxLayout: 37 id: password_box 38 orientation:'vertical' 39 Label: #パスワード上 40 text:' ' 41 Password: #このウィジェットを取得したい 42 id: password 43 Label: #パスワード下 44 text:' ' 45 Label: 46 text:' ' 47 Button: 48 color: 1,1,1,1 49 padding: self.width/4, (self.height-self.line_height)/2 50 text:'ログイン' 51 on_press: app.root.login.check() 52 53 54 #UserName: 55 #center_x: 0.5 56 #center_y: 0.8 57 #Password: 58 #center_x: 0.5 59 #center_y: 0.6 60 #Button: 61 #text:'ログイン' 62 #on_press: app.root.login.check() 63<Top@BoxLayout>: 64 Label: 65 text:'トップ画面' 66 67<UserName@TextInput>: 68 text: 's1801063' 69 font_size: 20 70 multiline: False 71 padding: self.width/4, (self.height-self.line_height)/2 #文字を真ん中に表示 72 on_text: 73 if len(self.text)>2: self.text = self.text[0:8] #文字数制限 74 75<Password@TextInput>: 76 text: '' 77 font_size: 20 78 multiline: False 79 password: True 80 padding: self.width/4, (self.height-self.line_height)/2 #文字を真ん中に表示 81 on_text: 82 if len(self.text)>2: self.text = self.text[0:16] #文字数制限 83 84<Button>: 85 color: 1,1,1,1 86 font_size: 24 87 88<Label>: 89 color: 0,0,0,1 90 font_name: 'VL-Gothic-Regular' 91 font_size: 32
Pythonファイル
python
1# -*- coding: utf-8 -*- 2from kivy.app import App 3from kivy.factory import Factory 4from kivy.uix.boxlayout import BoxLayout 5from kivy.uix.label import Label 6from kivy.uix.floatlayout import FloatLayout 7from kivy.properties import ObjectProperty 8 9#省略 10 11 def check(self): 12 #selfはLoginクラス 13 #username = self.ids['box'].ids['username_box_box'].ids['username_box'].ids['username'] #usernameWidget 14 #password = self.ids['box'].ids['password_box_box'].ids['password_box'].ids['password'] #passwordWidget 15 username = self.ids['username'] #usernameWidget 16 password = self.ids['password'] #passwordWidget 17 if username.value != '' and password.value != '': 18 self.parent.gotoTop() 19 else: 20 self.children[1].add_widget(Label(text='メールアドレスまたは、パスワードが入力されていません')) 21 22#省略
試したこと
selfが<Login>クラスで以下のように2通りのやり方で、idがusernameとpasswordのウィジェットの取得を試みました。
1回目
python
1username = self.ids['box'].ids['username_box_box'].ids['username_box'].ids['username'] #usernameWidget 2password = self.ids['box'].ids['password_box_box'].ids['password_box'].ids['password'] #passwordWidget
エラー: KeyError: 'username_box_box'
2回目
python
1username = self.ids['username'] #usernameWidget 2password = self.ids['password'] #passwordWidget
エラー:
File "C:\tenko\main.py", line 20, in check
if username.value != '' and password.value != '':
File "kivy\weakproxy.pyx", line 32, in kivy.weakproxy.WeakProxy.getattr
AttributeError: 'UserName' object has no attribute 'value'
補足情報(FW/ツールのバージョンなど)
Python 3.10.9

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。