亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將我的 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 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號