第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將我的 Python 方法合并到 Kivy 代碼中?

如何將我的 Python 方法合并到 Kivy 代碼中?

藍(lán)山帝景 2022-09-13 19:50:04
我在Python中編寫了一個(gè)小程序,該程序在兩個(gè)類別之間隨機(jī)選擇,并相應(yīng)地子集數(shù)據(jù)幀。完成后,它會(huì)選擇一行并提出問題和潛在的選擇。問題和選項(xiàng)位于數(shù)據(jù)幀的行中的兩個(gè)不同列下。到目前為止,它運(yùn)行良好;問題是當(dāng)我試圖將其與kivy相結(jié)合時(shí)。我不明白行動(dòng)的順序是如何發(fā)生的?;旧?,我希望能夠通過kivy文件在屏幕上包含問題和選擇。到目前為止,我能夠顯示它們,但看起來問題中的值與選擇列中的值不匹配。我的直覺告訴我,我的kivy文件運(yùn)行“Choosing_category”兩次,而不是只運(yùn)行一次并獲取適當(dāng)?shù)妮敵?。有誰知道我該如何解決這個(gè)問題?以下是我到目前為止所擁有的: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貢獻(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()


查看完整回答
反對 回復(fù) 2022-09-13
  • 1 回答
  • 0 關(guān)注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號