1 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊
關(guān)于該方法被調(diào)用兩次是正確的。解決這個(gè)問題的一個(gè)好方法是按照我建議的方法使用。您可以將類修改為:choosing_category()on_enter()QuestionWindows
class QuestionWindows(Screen):
word = StringProperty('')
choice1 = StringProperty('')
.
.
.
def choosing_category(self):
# Loading the dataframe
self.df = self._get_df()
.
.
.
# Getting the question from the temporary dataframe
self.word = np.random.choice(self.tmp_df['questions'])
# Getting the choice from the question that was previously selected
# Note the added [0] at the end of this line
self.choice1 = self.df.loc[self.tmp_df[self.tmp_df['questions'] == self.word].index, "choice1"].values[0]
# return str(self.word), str(self.choice1[0])
def on_enter(self, *args):
self.choosing_category()
這會(huì)向類中添加兩個(gè)由該方法更新的屬性,并且可以在 中引用:QuestionWindowschoosing_categroy()kv
<QuestionWindows>:
#id: question_page
name: "question_page"
FloatLayout:
QuestionButton:
id: question
text: root.word
pos_hint: {'x': 0.1, 'y': 0.77}
size_hint: 0.8, 0.17
back_color: 1, 1, 1, 1
background_normal: ''
font_size: 20
background_down: ''
SmoothButton:
id: choice1
text: root.choice1
pos_hint: {'x': 0.1, 'y': 0.27}
size_hint: 0.8, 0.1
這種方法的一個(gè)優(yōu)點(diǎn)是,您只需致電,問題和選擇就會(huì)更新。choosing_category()
添加回答
舉報(bào)