質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

3026閲覧

Kivyの画面遷移時のpropertyについて

cromalto

総合スコア12

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2018/07/10 21:47

編集2018/07/13 04:07

前提・実現したいこと

kivyの練習としてシャドウバースのコストを問題として出題するソフトを作ってます。以下のコードで、二問目以降はきちんと出力されるのですが、一問目だけ問題と選択肢が出力されません。

どなたか原因がわかる方ご教授願います。

発生している問題・エラーメッセージ

上記の通り

該当のソースコード

Python

1from kivy.app import App 2from kivy.core.text import LabelBase, DEFAULT_FONT 3from kivy.uix.screenmanager import ScreenManager, Screen 4from kivy.uix.checkbox import CheckBox 5from kivy.config import Config 6from kivy.uix.modalview import ModalView 7from kivy.properties import NumericProperty, StringProperty 8import random 9import csv 10 11Config.set("graphics", "resizable", False) 12Config.set("graphics", "width", 800) 13Config.set("graphics", "height", 600) 14LabelBase.register(DEFAULT_FONT, "ipaexg.ttf") 15sm = ScreenManager() 16 17 18class ChoiceScreen(Screen): 19 start = StringProperty("start") 20 21 def StartButtonClicked(self): 22 QuestionScreen.click(QuestionScreen(), 0) 23 24 sm.current = "question" 25 26 27class QuestionScreen(Screen): 28 counter = NumericProperty(1) 29 questions = StringProperty() 30 choice1 = StringProperty() 31 choice2 = StringProperty() 32 choice3 = StringProperty() 33 choice4 = StringProperty() 34 answer_position = 10 35 names = [] 36 rare = [] 37 cost = [] 38 print("ss") 39 40 with open("Book1.csv", "r") as f: 41 reader = csv.reader(f) 42 43 for row in reader: 44 names.append(row[0]) 45 rare.append(row[1]) 46 cost.append(row[3]) 47 48 def makequestion(self, mode): 49 if mode == 0: 50 costs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 51 returner = [] 52 choicer = [] 53 name0 = random.choice(self.names) 54 pos = self.names.index(name0) 55 cost_answer = int(self.cost[pos]) 56 choicer.append(cost_answer) 57 costs.remove(cost_answer) 58 bad_cost1 = costs.pop(random.randint(0, len(costs) - 1)) 59 choicer.append(bad_cost1) 60 bad_cost2 = costs.pop(random.randint(0, len(costs) - 1)) 61 choicer.append(bad_cost2) 62 bad_cost3 = costs.pop(random.randint(0, len(costs) - 1)) 63 choicer.append(bad_cost3) 64 random.shuffle(choicer) 65 returner.append(name0) 66 pos = choicer.index(cost_answer) 67 returner.append(cost_answer) 68 returner.append(pos + 1) 69 returner = returner + choicer 70 return returner 71 72 def click(self, n): 73 if n == self.answer_position: 74 readyView = CollectView() # 追加 75 readyView.open() 76 elif n != 0: 77 readyView = BadView() 78 readyView.open() 79 value = self.makequestion(0) 80 print(value) 81 self.answer_position = value[2] 82 self.questions = str(value[0]) 83 self.choice1 = str(value[3]) 84 self.choice2 = str(value[4]) 85 self.choice3 = str(value[5]) 86 self.choice4 = str(value[6]) 87 self.counter += 1 88 print(self.questions) 89 90 91class QuestionApp(App): 92 def build(self): 93 sm.add_widget(ChoiceScreen(name="choice")) 94 sm.add_widget(QuestionScreen(name="question")) 95 return sm 96 97 98class CollectView(ModalView): 99 def nextButtonClicked(self): 100 self.dismiss() 101 102 103class BadView(ModalView): 104 def nextButtonClicked(self): 105 self.dismiss() 106 107 108if __name__ == "__main__": 109 QuestionApp().run() 110 111## 以下kvファイル 112<QuestionScreen>: 113 BoxLayout: 114 115 116 Label: 117 size_hint_x: 1.0 118 text: "" 119 120 BoxLayout: 121 orientation: "vertical" 122 size_hint_x: 5.0 123 Label: 124 text: "{}/10".format(str(root.counter)) 125 Label: 126 text: "" 127 Label: 128 text: str(root.questions) 129 font_size: 24 130 Label: 131 text: "" 132 133 Button: 134 text: str(root.choice1) 135 font_size: 24 136 on_press: root.click(1) 137 138 139 Button: 140 text: str(root.choice2) 141 font_size: 24 142 on_press: root.click(2) 143 144 145 Button: 146 text: str(root.choice3) 147 font_size: 24 148 on_press: root.click(3) 149 150 Button: 151 text: str(root.choice4) 152 font_size: 24 153 on_press: root.click(4) 154 155 156 Label: 157 size_hint_x: 1.0 158 text: "" 159 160<ChoiceScreen>: 161 BoxLayout: 162 163 Label: 164 size_hint_x: 1.0 165 text: "" 166 167 BoxLayout: 168 orientation: "vertical" 169 size_hint_x: 5.0 170 Label: 171 text: "" 172 Label: 173 text: "" 174 Label: 175 text: "AAA" 176 Label: 177 text: "BBB" 178 Label: 179 text: "CCC" 180 Label: 181 text: "DDD" 182 Label: 183 text: "EEE" 184 Label: 185 text: "FFF" 186 Label: 187 text: "" 188 Label: 189 text: "" 190 191 BoxLayout: 192 orientation: "vertical" 193 size_hint_x: 5.0 194 Label: 195 text: "" 196 Label: 197 text: "" 198 CheckBox: 199 id : aaa 200 CheckBox: 201 id : bbb 202 CheckBox: 203 id : ccc 204 CheckBox: 205 id : ddd 206 CheckBox: 207 id : eee 208 CheckBox: 209 id : fff 210 Button: 211 text: str(root.start) 212 on_press: root.StartButtonClicked() 213 Label: 214 text: "" 215 216 Label: 217 size_hint_x: 1.0 218 text: "" 219 220<CollectView>: 221 size_hint: None, None 222 size: 400, 400 223 auto_dismiss: False 224 BoxLayout: 225 orientation: "vertical" 226 Label: 227 text: "正解" 228 font_size: 48 229 BoxLayout: 230 padding: 100, 75 231 Button: 232 text: "つぎ" 233 on_press: root.nextButtonClicked() 234 235<BadView>: 236 size_hint: None, None 237 size: 400, 400 238 auto_dismiss: False 239 BoxLayout: 240 orientation: "vertical" 241 Label: 242 text: "不正解" 243 font_size: 48 244 BoxLayout: 245 padding: 100, 75 246 Button: 247 text: "つぎ" 248 on_press: root.nextButtonClicked()

Book1.csv

高貴なる猫姉妹・シャムとシャマ,ゴールド,BOS,3,BOS01
烈風の翼神・ガルラ,レジェンド,BOS,9,BOS02
レジェンダリーファイター,レジェンド,BOS,2,BOS03
リトルパペッター・ロココ,レジェンド,BOS,2,BOS04
リトルドラゴン,シルバー,BOS,2,BOS05
蘇りし狂戦士,ゴールド,BOS,3,BOS06
揺らぎの斬姫・ナルメア,ゴールド,BOS,4,BOS07
幽想の少女・フェリ,レジェンド,BOS,2,BOS08
憂鬱な霊媒師,ブロンズ,BOS,5,BOS09
冥河の案内人,シルバー,BOS,4,BOS10

©Cygames

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

Python 3.6.5
Kivy 1.10.0

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gottadiveintopy

2018/07/12 00:06

Book1.csvも載せてくれないと実行できないですよ
guest

回答1

0

ベストアンサー

python

1class ChoiceScreen(Screen): 2 start = StringProperty("start") 3 4 def StartButtonClicked(self): 5 QuestionScreen.click(QuestionScreen(), 0) # A 6 7 sm.current = "question"

A行が間違いで、新しくインスタンスを作っているのでQuestionApp.build()内で作ったインスタンスとは別の物のclick()を呼んでいることになります。代わりに

python

1class ChoiceScreen(Screen): 2 start = StringProperty("start") 3 4 def StartButtonClicked(self): 5 sm.get_screen('question').click(0) # 変更点 6 7 sm.current = "question"

とすればいいです。

後、質問とは関係無いんですがコード内のglobal変数smは以下のようにして除くことができますよ。

python

1LabelBase.register(DEFAULT_FONT, "ipaexg.ttf") 2sm = ScreenManager() # <= この行を削除

python

1class QuestionApp(App): 2 def build(self): 3 sm = ScreenManager() # 追加 4 sm.add_widget(ChoiceScreen(name="choice")) 5 sm.add_widget(QuestionScreen(name="question")) 6 return sm
class ChoiceScreen(Screen): start = StringProperty("start") def StartButtonClicked(self): sm = self.manager # 追加 sm.get_screen('question').click(0) sm.current = "question"

投稿2018/07/13 19:24

編集2018/07/13 21:16
gottadiveintopy

総合スコア736

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cromalto

2018/07/16 15:59

わぁぁありがとうございます!!!! おかげで解決することができました!! ほかにも困ることがあったら頼りにさせてください!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問