前提・実現したいこと
kivyの勉強のためサンプルプロジェクトで魔方陣のパズルを作成しました。
教本通りにコードを書いたのですが、AttributeError: 'NoneType' object has no attribute 'gotoTitle'
というエラーが発生してしまいます。
調べてみたのですが、解決法がわからなく質問させていただきました。
発生している問題・エラーメッセージ
エラーメッセージ
Traceback (most recent call last):
File "C:\Users\kazuya_tate\AppData\Local\Programs\Python\Python37-32\file\kivy_practice\practice6\main.py", line 53, in <module>
MagicApp().run()
File "C:\Users\kazuya_tate\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\app.py", line 800, in run
root = self.build()
File "C:\Users\kazuya_tate\AppData\Local\Programs\Python\Python37-32\file\kivy_practice\practice6\main.py", line 51, in build
self.root.gotoTitle()
AttributeError: 'NoneType' object has no attribute 'gotoTitle'
該当のソースコード
python
1from kivy.app import App 2from kivy.factory import Factory 3from kivy.uix.boxlayout import BoxLayout 4from kivy.uix.floatlayout import FloatLayout 5from kivy.properties import ObjectProperty 6 7class Board(BoxLayout):#プレイ画面コード6) 8 puzzle = ((16,3,10,5,9,6,15,4,7,12,1,14,2,13,8,11), 9 (6,12,7,9,16,5,10,3,1,4,15,14,11,13,2,8), 10 (16,7,2,9,14,4,11,5,3,13,6,12,1,10,15,8)) 11 mask = ((0,0,1,1,0,0,1,1,1,1,0,0,1,1,1,0), 12 (1,1,0,0,0,1,1,0,0,0,1,1,1,0,0,1), 13 (0,1,1,0,0,1,0,1,0,0,0,1,1,0,1,0)) 14 def __int__(self,no,**kwargs): 15 super(Board,self).__init__(**kwargs) 16 self.no = no 17 self.W = [] 18 for k in range(16): 19 if self.mask[no-1][k] ==0: 20 c = Factory.NumInput() 21 else: 22 c = Factory.Const(text=str(self.puzzle[no-1][k])) 23 self.W.append(c) 24 self.ids['board'].add_widgwt(c) 25 def check(self): 26 view = Factory.CheckView() 27 for k in range(16): 28 if self.mask[self.no-1][k] ==0 and\ 29 self.puzzle.no[self.no-1][k] != self.W[k].value: 30 view.is_correct = False 31 break 32 view.open() 33 34 35 36class Root(FloatLayout): 37 board = ObjectProperty(None) 38 def gotoTitle(self): 39 self.clear_widgets() 40 self.add_widget(Factory.Titel()) 41 def gotoBoard(self,no): 42 self.clear_widgets() 43 self.board = Board(no) 44 self.add_widget(self.board) 45 46 47 48class MagicApp(App):#アプリクラス(コード6.3) 49 title = '魔方陣アプリ' 50 def build(self): 51 self.root.gotoTitle() 52 53MagicApp().run() 54
pythonとkivyは以下のバージョンを使用しています。
python ver 3.7.1
kivy ver 1.10.1
python
1Root: 2<Root>: 3 canvas.before: 4 rgb:1,1,1 5 rectangle: 6 pos:self.pos 7 size:self.size 8 9 10<Title@BoxLayout>: 11 orientation: 'vertical' 12 Label: 13 size_hint_y: 3 14 text:'魔方陣パズル' 15 GoToButton: 16 no:1 17 GoToButton: 18 no:2 19 GoToButton: 20 no:3 21<GoToButton>: 22 no: 0 23 text: '問題' + str(self.no) 24 on_press: app.root.gotoBoard(self.no) 25 26<Board>: 27 no: 0 28 orientation: 'vertical' 29 Label: 30 text: 問題' + str(root.no) 31 GridLayout: 32 id: board 33 rows:4 34 cols:4 35 size_hint_y:4 36 spacing:3 37 BoxLayout: 38 orientation:'horizontal' 39 Button: 40 text:'もどる' 41 no_press:app.root.gotoTitle() 42 Button: 43 text:'チェック' 44 on_press: app.root.board.check() 45<Const@Label>: 46 canvas.before: 47 Color: 48 rgb:0.6,1,1 49 Rectangle: 50 pos:self.pos 51 size:self.size 52<NumInput@textInput>: 53 font_size:32 54 hint_text: '-' 55 input_filter: 'int' 56 multiline: False 57 padding: self.width/4, (self.height-self.line_height)/2 58 on_text: 59 if len(self.text)>2:self.text = self.text[1:3] 60 if self.text=='':self.value=0 61 else:self.value = int(self.text) 62 63<CheckView@ModalView>: 64 auto_dismiss: False 65 background_color:0,0,0,0.5 66 is_current : True 67 size_hint: 0.5,0.5 68 BoxLayout: 69 canvas.before: 70 Color: 71 rgb:1,1,0.9 72 Rectangle: 73 pos: self.pos 74 size: self.size 75 orientation: 'vertical' 76 padding:self.width/10 77 Label: 78 size_hint_y:4 79 text:'正解 :-)' if root.is_correct else '不正解 :-(' 80 Button: 81 text: 'OK' 82 on_press: 83 root.dismiss() 84 if root.is_correct: app.root.gototitle() 85 86 87<Label>: 88 color: 0.5,025,0.25,1 89 font_name: 'VL-Gothic-Regular.ttf' 90 font_size :32 91<Button>: 92 color:1,1,1,1 93 font_size:24 94 95
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

回答2件
あなたの回答
tips
プレビュー