Python 在构建期间从kivy文本输入中获取值(单位:kv)

Python 在构建期间从kivy文本输入中获取值(单位:kv),python,kivy,Python,Kivy,我有三个问题: 下面给出了app/app kv文件的配置,为什么我必须调用self.build-in-ButtonsFactory类。若我删除它,我会得到黑屏,可能是因为kv文件中并没有根元素,但若我尝试生成MainView根元素,我也会得到黑屏。 第二个问题,是否可以在我的someapp.kv文件中设置自定义按钮的最小高度?它们不应小于X。 最后,但对我来说最重要的是,为什么我无法从kv文件中获取TextInput的文本属性,在类函数get_list_of_files中?解决这个问题的最佳方法

我有三个问题:

下面给出了app/app kv文件的配置,为什么我必须调用self.build-in-ButtonsFactory类。若我删除它,我会得到黑屏,可能是因为kv文件中并没有根元素,但若我尝试生成MainView根元素,我也会得到黑屏。 第二个问题,是否可以在我的someapp.kv文件中设置自定义按钮的最小高度?它们不应小于X。 最后,但对我来说最重要的是,为什么我无法从kv文件中获取TextInput的文本属性,在类函数get_list_of_files中?解决这个问题的最佳方法是什么?全局变量在python代码中哪个硬编码此值?在python代码嵌入kv作为字符串之前移动生成器? 最后一个问题…按钮填充scrollview\u id,而不是保留大小并在此视图中可滚动,我希望它们停止自我调整大小。 someapp.py文件

from kivy.app import App
from kivy.factory import Factory
from kivy.properties import ObjectProperty, StringProperty
# views
from kivy.uix.modalview import ModalView
# layouts
from kivy.uix.boxlayout import BoxLayout

import os

# defined in kv file.
class HeaderContainer(BoxLayout): pass
class ButtonsContainer(BoxLayout): pass
class MainView(ModalView): pass

class ButtonsFactory(BoxLayout):
    target_location = StringProperty(None)
    def __init__(self, *args, **kwargs):
        super(ButtonsFactory, self).__init__(*args, **kwargs)

        self.build() # Question 1: is that neccessary? (i think not, but black screen without it, why?)

    def build(self):
        self.orientation = "vertical"
        for file_name in self.get_list_of_files():
            btn = Factory.CustomButton()
            with open(file_name, 'r') as test_file:
                btn.file_name = test_file.readline().strip()[1:20]
            btn.nice_name = file_name  # Question 2: is it possible to set minimum height for kivy button? (havent found in api)
            self.add_widget(btn)
        # print ("1.!!!!!!", self.target_location) == NONE
    @classmethod
    def get_list_of_files(cls):
        # print "2.!!!!!!", repr(cls.target_location) == <kivy.properties.StringProperty object at 0x7f1dd7596e20> 
        dir_ = "/tmp" #dir_ = cls.target_location
        try:
            files = [os.path.join(dir_, name) for name in os.listdir(dir_)
                     if os.path.isfile(os.path.join(dir_, name))]
        except (OSError, IOError):
            files = []
        return files

class SomeApp(App):
    def on_pause(self):
        pass
    def on_resume(self):
        pass
    def build(self):
        return MainView()

if __name__ == '__main__':
    SomeApp().run()
和someapp.kv文件

#:kivy 1.8.0
#:import platform platform

<CustomButton@Button>:
    file_name: ''
    nice_name: ''
    text: root.nice_name + "\n" + root.file_name
    halign:'center'
    size_hint:(1, 0.1)

<HeaderContainer>:
    id: header_layout
    size_hint:(1, 0.1)
    orientation:'horizontal'
    # 2-nd-try # target_location: textinput_target_location.text
    # I was trying here to pass by ObjectProperty (or StringProperty) but unfortunately failed.
    TextInput:
        size_hint:(0.7, 1)
        id: textinput_target_location
        multiline: False
        hint_text: "path where stress files are stored, default /sdcard/appdir"
        text: "/tmp" if platform.machine() in ["x86_64", "i686", "i386"] else "/sdcard/appdir/"  # arm "arm7l", but also other arm's
        #on_text: my_callback_to_reload_dir_contents()
    Button:
        size_hint:(0.2, 1)
        id: read_target_location
        text: "read target_location directory"
        #on_release: my_callback_to_reload_dir_contents()

<ButtonsContainer>:
    size_hint:(1, 0.9)
    orientation:'vertical'
    ScrollView:
        id: scrollview_id
        orientation: 'vertical'
        ButtonsFactory


<MainView>:
    BoxLayout:
        # 1-st-try # target_location: HeaderContainer.target_location
        id: main_layout
        padding:10
        spacing: 5
        orientation:'vertical'
        HeaderContainer
        # n-th-try # target_location: HeaderContainer.target_location
        ButtonsContainer

我将尝试回答您的问题:

1查看您的代码,ButtonsFactory只是一个BoxLayout,它是一个容纳其他小部件的容器。self.build函数创建CustomButton小部件并将它们放入容器中。如果不调用self.build,则不会向容器中添加任何内容,并且您的BoxLayout为空

2这有点复杂,因为高度通常由存放小部件的容器控制。通过将size\u hint属性设置为None并指定高度,可以手动设置小部件的高度

3如果可以避免,我永远不会使用全局变量。在这种情况下,如果您只需要访问TextInput的文本内容,我会将它绑定到一个StringProperty,该StringProperty附加到一个您可以访问的小部件上,或者绑定到App对象本身,该对象可以在.kv as App.中的任何位置访问

4同样,高度由容器控制,小部件位于BoxLayout中。您可以通过在小部件上将size\u hint设置为None来手动控制它们的大小


另一个问题是,您正在将按钮工厂的BoxLayout放入ScrollViewer中。BoxLayout将调整自身大小,使其完全适合ScrollViewer,因此不会滚动。您需要通过清除size\u提示来覆盖此行为。有关详细信息,请参阅。

首先感谢您的关注和帮助。继续,1。我明白,但我认为关键是Kivy称之为构建本身?也许它只对小部件有效。2.四,。理解。3.这就是问题所在。正如您可能注意到的,我已经尝试设置和使用此属性,但唯一的方法是将其设置为app.property,并在构建函数中修改set default。我不认为这是一个很好的解决方案,但它仍然有效,而且建议对我们帮助很大。当然。您考虑的构建函数是App.build,kivy调用它来获取应用程序的根小部件。