1 回答

TA贡献1796条经验 获得超7个赞
关于该方法被调用两次是正确的。解决这个问题的一个好方法是按照我建议的方法使用。您可以将类修改为: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()
这会向类中添加两个由该方法更新的属性,并且可以在 中引用: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
这种方法的一个优点是,您只需致电,问题和选择就会更新。choosing_category()
添加回答
举报