Проблема с использованием clock.schedule_once с kivy в Python - PullRequest
0 голосов
/ 03 мая 2020

Мое приложение отображает вопрос с тремя потенциальными ответами. Пользователь должен будет выбрать ответ, и будет отображен результат, показывающий, был ли выбор правильным или неправильным. В моем классе у меня есть метод выбора и показа вопроса и ответов. У меня есть другой метод, который оценивает, хороший выбор или нет. В рамках этого метода я хочу отобразить на экране слово «правильный» или «неправильный» в метке через киви. Как только результат отображается в течение 3 секунд, я хочу выбрать и показать еще один вопрос и ответы. Я попытался использовать clock.schedule_once для отображения результата и заморозить метод, прежде чем приступить к новому испытанию. К сожалению, результат отображается только на следующем дисплее. Возможно, я не использую clock_once адекватно, и мне было интересно, есть ли у кого-нибудь идея, как решить эту проблему. Ниже приведен пример моего кода. Я не включил все; моя главная проблема заключается в методе eval_answer.

my_project.py
class QuestionWindows(Screen):
    word = StringProperty('')
    choice1 = StringProperty('')
    choice2 = StringProperty('')
    choice3 = StringProperty('')
    answer = StringProperty('')
    button_press = StringProperty('')
    result = StringProperty('')


    def __init__(self,  **kwargs):
        super(QuestionWindows, self).__init__(**kwargs)
        self.prob = ''
        self.word = ''
        self.choice1 = ''
        self.choice2 = ''
        self.choice3 = ''
        self.word = ''
        self.df = pd.DataFrame()
        self.tmp_df = pd.DataFrame()
        self.table_name = ''
        self.outcome = 0
        self.result = ''


    def _get_df(self):
        # get the df
        return self.df

    def update_table(self):
        self.df.to_csv(str(self.table_name) + '.csv', index=False)

    def choosing_category(self):
        # Choose the category accurately
        return self.prob

    def choosing_question(self):
        # Choose the question and answers in the dataframe previously loaded

    def on_enter(self, *args):
        # Make sure that a question/answers are displayed after loading the display 
        self.choosing_question()

    def update_main_df(self):
        # Update the main dataframe following the presentation of a question/answers
        return self.df


    def eval_answer(self, index):
        # Prompt user fo an answer
        if index == 1:
            button_press = self.ids.choice1.text
        elif index == 2:
            button_press = self.ids.choice2.text
        else:
            button_press = self.ids.choice3.text

        if button_press == self.answer:
            self.result = "Correct"
            Clock.schedule_once(self.print_result, 3)
            # Update the main dataframe
            self.update_main_df()
            self.update_table()
            self.choosing_question()
        else:
            self.result = 'Incorrect'
            Clock.schedule_once(self.print_result, 3)
            self.update_main_df()
            self.update_table()
            self.choosing_question()
my_project.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
            text_size: self.size
            halign: "center"
            valign: "middle"
            back_color: 1, 1, 1, 1
            background_normal: ''
            font_size: 26
            background_down: ''
        Label:
            id: result
            text: root.result
            pos_hint: {'x': 0.25, 'y': 0.7}
            size_hint: 0.5, 0.04
            font_size: 30
            color: 0,1,0,1
        SmoothButton:
            id: choice1
            text: root.choice1
            pos_hint: {'x': 0.1, 'y': 0.27}
            size_hint: 0.8, 0.1
            text_size: self.size
            font_size: 22
            halign: "center"
            valign: "middle"
            on_release: root.eval_answer(1)
        SmoothButton:
            id: choice2
            text: root.choice2
            pos_hint: {'x': 0.1, 'y': 0.42}
            size_hint: 0.8, 0.1
            text_size: self.size
            font_size: 22
            halign: "center"
            valign: "middle"
            markup: True
            on_release: root.eval_answer(2)
        SmoothButton:
            id: choice3
            text: root.choice3
            pos_hint: {'x': 0.1, 'y': 0.57}
            size_hint: 0.8, 0.1
            text_size: self.size
            font_size: 22
            halign: "center"
            valign: "middle"
            markup: True
            on_release: root.eval_answer(3)
        Button:
            id: quit
            text: 'Go back'
            pos_hint: {'x': 0.425, 'y': 0.22}
            size_hint: 0.15, 0.03
            background_color: .8, .5, .5, 1
            background_normal: ''
            font_size: 20
            color: 0,0,0,1
            on_release:
                app.screen_manager.transition.direction = "right"
                app.screen_manager.transition.duration = 1
                app.screen_manager.current = 'signin_page'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...