Kivy入門サイトを参考に、ボタンで入力された数字キーを表示するディスプレイを作ろうとしています。
0ボタンが押されたときにフレーム上段のディスプレイに0と表示させたいのですが、何度試してもうまく行きませんでした。どこが間違っているのかご存知でしたら教えていただけないでしょうか。
よろしくお願いします。
記載コードで実行し、0ボタンを押すと強制終了し下記のエラーが出ます。
File "c:\Users...\test1.kv", line 17, in <module>
on_press: root.ids["ScreenOne"].print_number(self.text)
KeyError: 'ScreenOne'
kvファイルの
on_press: root.ids["ScreenOne"].zero_clicked(self.text)
を
on_press: root.ids["display"].zero_clicked(self.text)
にした場合は下記のエラーが出ました。
File "kivy\weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.getattr (kivy\weakproxy.c:1386)
AttributeError: 'TextInput' object has no attribute 'print_number'
test1.py
Python
1from kivy.app import App 2from kivy.uix.screenmanager import ScreenManager, Screen 3SM = ScreenManager() 4 5class ScreenOne(Screen): 6 def zero_clicked(self, number): 7 text = "{}{}".format(self.display.text, number) 8 self.display.text = text 9 def next_clicked(self): 10 SM.current = "Two" 11 12class ScreenTwo(Screen): 13 def back_clicked(self): 14 SM.current = "One" 15 16class TEST1(App): 17 def build(self): 18 SM.add_widget(ScreenOne(name="One")) 19 SM.add_widget(ScreenTwo(name="Two")) 20 return SM 21 22if __name__ == "__main__": 23 TEST1().run() 24
TEST1.kv
Kivy
1#:kivy 1.10.0 2<ScreenOne>: 3 BoxLayout: 4 orientation: "vertical" 5 TextInput: 6 id: display 7 size_hint_y: 1 8 font_size: 100 9 BoxLayout: 10 size_hint_y: 2 11 Button: 12 id: zero 13 text: "0" 14 on_press: root.ids["ScreenOne"].zero_clicked(self.text) 15 Button: 16 id: clear 17 text: "Clear" 18 Button: 19 id: next 20 text: "Next" 21 on_press: root.next_clicked() 22<ScreenTwo>: 23 Button: 24 id: back 25 text: "Back" 26 on_press: root.back_clicked() 27<Button>: 28 font_size: 60
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。