猿问

如何将我的 Python 方法合并到 Kivy 代码中?

我在Python中编写了一个小程序,该程序在两个类别之间随机选择,并相应地子集数据帧。完成后,它会选择一行并提出问题和潜在的选择。问题和选项位于数据帧的行中的两个不同列下。到目前为止,它运行良好;问题是当我试图将其与kivy相结合时。


我不明白行动的顺序是如何发生的。基本上,我希望能够通过kivy文件在屏幕上包含问题和选择。到目前为止,我能够显示它们,但看起来问题中的值与选择列中的值不匹配。我的直觉告诉我,我的kivy文件运行“Choosing_category”两次,而不是只运行一次并获取适当的输出。有谁知道我该如何解决这个问题?


以下是我到目前为止所拥有的:


tryapp.py


import kivy

from kivy.app import App

import pandas as pd

import numpy as np


from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition

kivy.require('1.11.1')


class QuestionWindows(Screen):

    def __init__(self,  **kwargs):

        super(QuestionWindows, self).__init__(**kwargs)

        self.prob = ''

        self.word = ''

        self.choice1 = ''

        self.word = ''

        self.df = pd.DataFrame()


    def _get_df(self):

        df = pd.DataFrame([['new', 'What is you favorite color?', 'blue?', 'blue', 0,0,0,0,0],

                           ['familiar', 'What is your favorite fruit?', 'apple?', 'apple', 0,0,0,0,0],

                           ['new', 'What is your favorite vegetable?', 'carrot?', 'carrot',0,0,0,0,0]],

                          columns=['state', 'questions', 'choice1', 'answer', 'cnt', 'count_correct', 'error_count', 'total_error', 'total_correct'])

        return df


    def choosing_category(self):

        # Loading the dataframe

        self.df = self._get_df()


        # Generating the category for which the question/answer will be sampled from

        self.prob = np.random.choice(['new', 'familiar'], 1, p=[0.7, 0.3])

        # Dealing with the condition on whether the state 'familiar' is not found in the data set

        if self.prob not in self.df['state'].values:

            self.prob = ['new']

            self.prob = self.prob


蓝山帝景
浏览 165回答 1
1回答

芜湖不芜

关于该方法被调用两次是正确的。解决这个问题的一个好方法是按照我建议的方法使用。您可以将类修改为:choosing_category()on_enter()QuestionWindowsclass QuestionWindows(Screen):&nbsp; &nbsp; word = StringProperty('')&nbsp; &nbsp; choice1 = StringProperty('')...&nbsp; &nbsp; def choosing_category(self):&nbsp; &nbsp; &nbsp; &nbsp; # Loading the dataframe&nbsp; &nbsp; &nbsp; &nbsp; self.df = self._get_df()&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; # Getting the question from the temporary dataframe&nbsp; &nbsp; &nbsp; &nbsp; self.word = np.random.choice(self.tmp_df['questions'])&nbsp; &nbsp; &nbsp; &nbsp; # Getting the choice from the question that was previously selected&nbsp; &nbsp; &nbsp; &nbsp; # Note the added [0] at the end of this line&nbsp; &nbsp; &nbsp; &nbsp; self.choice1 = self.df.loc[self.tmp_df[self.tmp_df['questions'] == self.word].index, "choice1"].values[0]&nbsp; &nbsp; &nbsp; &nbsp; # return str(self.word), str(self.choice1[0])&nbsp; &nbsp; def on_enter(self, *args):&nbsp; &nbsp; &nbsp; &nbsp; self.choosing_category()这会向类中添加两个由该方法更新的属性,并且可以在 中引用:QuestionWindowschoosing_categroy()kv<QuestionWindows>:&nbsp; &nbsp; #id: question_page&nbsp; &nbsp; name: "question_page"&nbsp; &nbsp; FloatLayout:&nbsp; &nbsp; &nbsp; &nbsp; QuestionButton:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: question&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: root.word&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pos_hint: {'x': 0.1, 'y': 0.77}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size_hint: 0.8, 0.17&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; back_color: 1, 1, 1, 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background_normal: ''&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font_size: 20&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background_down: ''&nbsp; &nbsp; &nbsp; &nbsp; SmoothButton:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: choice1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: root.choice1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pos_hint: {'x': 0.1, 'y': 0.27}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size_hint: 0.8, 0.1这种方法的一个优点是,您只需致电,问题和选择就会更新。choosing_category()
随时随地看视频慕课网APP

相关分类

Python
我要回答