为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

蓝山帝景 2022-09-13 19:50:04
我在Python中编写了一个小程序,该程序在两个类别之间随机选择,并相应地子集数据帧。完成后,它会选择一行并提出问题和潜在的选择。问题和选项位于数据帧的行中的两个不同列下。到目前为止,它运行良好;问题是当我试图将其与kivy相结合时。我不明白行动的顺序是如何发生的。基本上,我希望能够通过kivy文件在屏幕上包含问题和选择。到目前为止,我能够显示它们,但看起来问题中的值与选择列中的值不匹配。我的直觉告诉我,我的kivy文件运行“Choosing_category”两次,而不是只运行一次并获取适当的输出。有谁知道我该如何解决这个问题?以下是我到目前为止所拥有的:tryapp.pyimport kivyfrom kivy.app import Appimport pandas as pdimport numpy as npfrom kivy.uix.screenmanager import ScreenManager, Screen, FadeTransitionkivy.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
查看完整描述

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()


查看完整回答
反对 回复 2022-09-13
  • 1 回答
  • 0 关注
  • 138 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号