[英]How do I incorporate my Python methods in the Kivy code?
I coded a small program in Python that randomly picks between two categories and subset the dataframe accordingly.我用 Python 编写了一个小程序,它在两个类别之间随机选择并相应地对数据帧进行子集化。 Once done, it picks a row and presents the question and a potential choice.
完成后,它会选择一行并提出问题和一个潜在的选择。 The question and the choice are found in the row of the dataframe under two different columns.
问题和选择位于两列不同列下的数据框行中。 So far, it's working well;
到目前为止,它运行良好; the problem is when I tried to incorporate this with kivy.
问题是当我尝试将其与 kivy 合并时。
I don't understand how the sequence of actions in taken place.我不明白动作的顺序是如何发生的。 Basically, I want to be able to include the question and the choice on the screen via the kivy file.
基本上,我希望能够通过 kivy 文件在屏幕上包含问题和选择。 So far, I'm able to show them, but it looks like the value from the question doesn't match the value from the choice column.
到目前为止,我能够展示它们,但看起来问题中的值与选择列中的值不匹配。 My intuition tells me that my kivy file runs the "Choosing_category" twice instead of just running it once and taking the appropriate outputs.
我的直觉告诉我,我的 kivy 文件运行“Choosing_category”两次,而不是只运行一次并获取适当的输出。 Does anyone knows how I can solve this issue?
有谁知道我该如何解决这个问题?
Here is what I have so far:这是我到目前为止所拥有的:
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
else:
self.prob = self.prob
# Making sure to present a question/answer that hasn't been presented recently
if len(self.df[(self.df['state'] == self.prob[0]) & (self.df['cnt'] == 0)]) > 0:
self.tmp_df = self.df[(self.df['state'] == self.prob[0]) & (self.df['cnt'] == 0)]
elif len(self.df[(self.df['state'] == prob)]) > 0:
self.tmp_df = self.df[(self.df['state'] == prob)]
else:
self.prob = ['familiar']
self.tmp_df = self.df[(self.df['state'] == self.prob) & (self.df['cnt'] == 0)]
# 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
self.choice1 = self.df.loc[self.tmp_df[self.tmp_df['questions'] == self.word].index, "choice1"].values
return str(self.word), str(self.choice1[0])
class QuestionsApp(App):
def build(self):
self.screen_manager = ScreenManager()
self.question_page = QuestionWindows()
self.screen_manager.add_widget(self.question_page)
return self.screen_manager
if __name__ == "__main__":
tryapp = QuestionsApp()
tryapp.run()
questions.kv问题.kv
<SmoothButton@Button>:
background_color: (0.0, 0.4, 0.95, 1)
background_normal: ''
font_size: 20
<QuestionButton@Button>:
background_color: (0,0,0,0)
background_normal: ''
back_color: (0.0, 0.4, 0.95, 1)
border_radius: [2,2,22,22]
color: (0.0, 0.4, 0.95, 1)
bold: True
canvas.before:
Color:
rgba: self.back_color
RoundedRectangle:
size: self.size
pos: self.pos
radius: self.border_radius
<QuestionWindows>
#id: question_page
name: "question_page"
FloatLayout:
QuestionButton:
id: question
text: root.choosing_category()[0]
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.choosing_category()[1]
pos_hint: {'x': 0.1, 'y': 0.27}
size_hint: 0.8, 0.1
You are right about the choosing_category()
method getting called twice.您对
choosing_category()
方法被调用两次是正确的。 A good way to fix that is to use the on_enter()
method as I suggested.解决这个问题的一个好方法是使用我建议的
on_enter()
方法。 You can modify your QuestionWindows
class as:您可以将
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()
This adds two properties to the QuestionWindows
class that are updated by the choosing_categroy()
method, and can be referenced within the kv
:这向
QuestionWindows
类添加了两个属性,这些属性由choosing_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
An advantage of this approach is that you can simply call choosing_category()
and the question and choice will update.这种方法的一个优点是您可以简单地调用
choosing_category()
并且问题和选择将更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.