[英]How do I incorporate my Python methods in the Kivy code?
我用 Python 编写了一个小程序,它在两个类别之间随机选择并相应地对数据帧进行子集化。 完成后,它会选择一行并提出问题和一个潜在的选择。 问题和选择位于两列不同列下的数据框行中。 到目前为止,它运行良好; 问题是当我尝试将其与 kivy 合并时。
我不明白动作的顺序是如何发生的。 基本上,我希望能够通过 kivy 文件在屏幕上包含问题和选择。 到目前为止,我能够展示它们,但看起来问题中的值与选择列中的值不匹配。 我的直觉告诉我,我的 kivy 文件运行“Choosing_category”两次,而不是只运行一次并获取适当的输出。 有谁知道我该如何解决这个问题?
这是我到目前为止所拥有的:
尝试应用程序
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()
问题.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
您对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()
这向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
这种方法的一个优点是您可以简单地调用choosing_category()
并且问题和选择将更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.