我对Kivy/Python/编程比较陌生。所以,如果有什么不清楚的地方,我很抱歉,但我的问题是:我一直在研究一个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发布于 2018-01-13 16:05:59
好的,我已经在解决我的问题方面取得了一些进展,我认为我的代码可以工作到能够将它应用到我的整个程序中,并让它完全完成我需要它做的事情(希望如此!)我对我的.py代码做了一些重要的修改,对kv文件做了一些小的修改,我将在这里发布,以防它帮助其他人在类似的情况下。
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
exercisedict = {'exercise':['Pushups', 'Squats', 'Curls'],'focus':['Chest','Legs','Arms'],'equip':['None','None','Dumbells'], 'deftime':['30','30','40']}
exercisedf = pd.DataFrame(exercisedict)
class MainScreen(Screen):
    pass
class EditWorkoutScreen(Screen):
    def setupscreen(self):
        for index, row in exercisedf.iterrows():
            MyGrid(str(row.exercise),str(row.focus),str(row.equip),str(row.deftime))
    def addgrids(self):
        global grids
        for i in grids:
            print i.children
            self.ids.exercisestoverify.add_widget(i)
    def printtext(self):
        global grids
        for i in grids:
            print i.extime.text
class ScreenManagement(ScreenManager):
    pass
grids= []
class MyGrid(GridLayout):
    def __init__(self,exname,exfocus,exequip,extime, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        global grids
        def testtext(self):
            print self.text
        self.exname = Label(text=exname)
        self.exfocus = Label(text=exfocus)
        self.exequip = Label(text=exequip)
        self.extime = TextInput(text=extime, size_hint=(None,None), size=(25,30),font_size=11, multiline=False)
        self.extime.bind(on_text_validate=testtext)
        self.add_widget(self.exname)
        self.add_widget(self.exfocus)
        self.add_widget(self.exequip)
        self.add_widget(self.extime)
        grids.append(self)
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>:
    rows: 1
<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
        Button:
            text: 'press'
            on_press: root.addgrids()
            size: 100,20
            size_hint: None,None
        Button:
            text: 'text input text'
            on_press: root.printtext()
            size: 100,20
            size_hint: None,None
            pos: 100,100因此,现在进入EditWorkout屏幕,我的setupscreen函数就会运行,它为我的dataframe中的每个练习实例化一个自定义GridLayout (MyGrid),然后附加到一个列表(网格)中。然后,我通过addgrids函数将网格列表中的每个对象添加到屏幕上的GridLayout中,现在我可以从EditWorkout屏幕下添加的printtext函数中获得每个文本输入的值,方法是使用网格列表来选择我想要的文本输入。
主要的关键是创建自定义GridLayout类,使用所需的所有变量/小部件初始化它,并将每个自定义对象添加到列表中,以便在需要时遍历该列表并获取必要的文本输入文本。
我认为可以有更好的方法来实现这一点(也许是使用RecycleView?)因此,如果其他人有任何想法或建议,请告诉我!
https://stackoverflow.com/questions/48239257
复制相似问题