【发布时间】:2018-01-14 00:05:59
【问题描述】:
我对 Kivy/Python/Programming 比较陌生。因此,如果有任何不清楚的地方,我深表歉意,但这是我的问题:我一直在研究 Kivy 锻炼计划,该计划可以随机选择,也可以手动选择我想要进行的锻炼。
我现在遇到的问题是在手动选择练习期间(在下面的 EditWorkoutScreen 中,只需单击主屏幕上的按钮即可到达那里)。下面我的代码的相关部分几乎显示了我想要的内容(到目前为止,它包括标签和 ScrollView/GridLayout 中的文本输入),但我无法弄清楚如何访问文本输入中的文本。该文本代表每次锻炼的默认持续时间,我希望能够更改该时间(例如,如果我想在锻炼期间进行更长/更短的特定锻炼)。我有一个 on_text_validate 函数,它打印文本输入中的内容,但显然现在它只返回最后一行,因为每行的变量都被覆盖,但我需要的是每个文本输入与适当的练习配对。我尝试过使用 RecycleView,因为这似乎是一个不错的选择,但是在尝试从动态创建的小部件中获取文本时,我仍然遇到了问题。
所以我的主要问题是,如何从这些动态创建的小部件访问每个练习的文本输入?
wotester.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
import pandas as pd
class MainScreen(Screen):
pass
exercisedict = {'exercise':['Pushups', 'Squats', 'Curls'],'focus':['Chest','Legs','Arms'],'equip':['None','None','Dumbells'], 'deftime':['30','30','40']}
exercisedf = pd.DataFrame(exercisedict)
class EditWorkoutScreen(Screen):
def setupscreen(self):
global exercisedf
for index, row in exercisedf.iterrows():
def testtext(self):
print extime.text
grid = GridLayout(rows=1, spacing=2)
exname = Label(text=str(row.exercise))
exfocus = Label(text=str(row.focus))
exequip = Label(text=str(row.equip))
extime = TextInput(text=str(row.deftime), size_hint=(None,None), size=(25,30),font_size=11, multiline=False)
extime.bind(on_text_validate=testtext)
grid.add_widget(exname)
grid.add_widget(exfocus)
grid.add_widget(exequip)
grid.add_widget(extime)
self.ids.exercisestoverify.add_widget(grid)
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("wotester.kv")
class MainApp(App):
def build(self):
return presentation
MainApp().run()
wotester.kv
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import main wotester
ScreenManagement:
transition: FadeTransition()
MainScreen:
EditWorkoutScreen:
####### Layout Outlines #############################################################
<GridLayout>:
canvas.after:
Color:
rgb: 1,0,0
Line:
rectangle: self.x+1,self.y+1,self.width-1,self.height-1
Color:
rgb: 1,1,1
<FloatLayout>:
canvas.after:
Color:
rgb: 1,0,0
Line:
rectangle: self.x+1,self.y+1,self.width-1,self.height-1
Color:
rgb: 1,1,1
<BoxLayout>:
canvas.after:
Color:
rgb: 1,0,0
Line:
rectangle: self.x+1,self.y+1,self.width-1,self.height-1
Color:
rgb: 1,1,1
#########################################################################################
<MainScreen>:
name: "main"
FloatLayout:
id: test
canvas.before:
Color:
rgba: 0, 0, 1, .5
Rectangle:
pos: self.pos
size: self.size
Label:
text: "Workout Creator"
pos_hint:{"x": 0, "y": .4}
font_size: 40
Label:
text: "Welcome"
pos_hint:{"x": -.4, "y": .4}
font_size: 20
Button:
text: "Click here"
color: 0,1,0,1
size_hint: .2, .1
pos_hint: {"x":.4, "y":.7}
on_release: root.manager.current = "editworkout"
<MyGrid>:
<EditWorkoutScreen>:
name:'editworkout'
on_enter: root.setupscreen()
FloatLayout:
Label:
text: 'Verify/Edit Workout'
pos: 0, 550
font_size: 20
ScrollView:
pos_hint: {"x":.160, "y":-.15}
GridLayout:
id: exercisestoverify
size_hint_y: None
size_hint_x: .80
orientation: "vertical"
height: self.minimum_height
row_default_height: 30
spacing: 0
cols:1
【问题讨论】: