使用 python 无法在 .kv 文件的输出屏幕中动态添加小部件

Failed to add widgets dynamically in output screen in .kv file using python

我试图将结果添加到内容应该是动态的新屏幕。我尝试了一些 Whosebug 本身给出的琐碎方法,但未能显示任何内容。我的代码是这样的。

我输入了多个数据,用“,”分隔,没有空格,使用文本输入,然后拆分将其存储为一个列表。数据将被解析,并且在我尝试但未成功执行的输出屏幕上应显示等效数量的标签(anbs.py 中的第 51-56 行)。

例如,输入“I,am,a,good,boy”。
结果应该与控制台中的一样,即每个 Label 的文本应该包含一个单独的项目,但没有任何内容进入输出屏幕...只是一个用于在屏幕之间遍历的大按钮。

这是我的 anbs.py 文件。

from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.factory import Factory

Window.size = (600, 600)

class HelpWindow(Screen):
    """
    This is a help window. It contains the functionality of all the given boxes
    on the main window.
    """

    def main_window(self):
        sm.current = "main"

class MainWindow(Screen):
    """
    This is the main window that contains the main form.
    This connects the frontend of the app to the backend
    """
    target = ObjectProperty(None)


    def v_popup(self):
        version_popup()
    
    def help(self):
        sm.current = "help"
    
    def output_window(self):
        sm.current = "output"

    def get_results(self):
        #OutputWindow.main(options = options)
        out_window = OutputWindow()
        out_window.main(self.target.text)
        sm.current = "output"
    

class OutputWindow(Screen):
    """
    This is the output window. All the generated results will be seen here.
    """
    res = ObjectProperty(None)
    res_out = ObjectProperty(None)

    def main(self, options):
        options = list(options.split(","))
        for item in options:
            print(item)
            self.res_out.add_widget(Label(text=item))


    def main_window(self):
        sm.current = "main"

class WindowManager(ScreenManager):
    pass

def version_popup():
    """
    Version Popup Window.
    """
    
    version = "v1.0"
    version_text = "this is "+version+" for this app"
    vpop = Popup(title="Version",
                    content=Label(text=version_text),
                    size_hint=(None, None), size=(400, 400))
    
    vpop.open()

### main builder and WindowManager object
kv = Builder.load_file("start.kv")
sm = WindowManager()

### Adding screens to widget
screens = [MainWindow(name="main"), HelpWindow(name="help"), OutputWindow(name="output")]
for screen in screens:
    sm.add_widget(screen)

sm.current = "main"

### main working
class AnbsApp(App):
    def build(self):
        return sm
        
if __name__ == '__main__':
    AnbsApp().run()

我的 start.kv 文件看起来像这样

<HelpWindow>
    name:"help"

    Button:
        id:ms
        text:"Main Screen"
        on_release:
            root.manager.transition.direction = "left"
            root.main_window()

<MainWindow>
    name:"main"

    target : target
    
    
    GridLayout:
        cols:1

        GridLayout:
            cols:3
            
            row_force_default: True
            row_default_height: 50
            
            Button:
                id:hp
                text:"Help"
                on_release:
                    root.manager.transition.direction = "right"
                    root.help()
            
            Button:
                id:version
                text: "Version"
                on_release: 
                    root.v_popup()
        
        GridLayout:
            cols:2
            row_force_default: True
            row_default_height: 30

            Label:
                text:"Target *"
            TextInput:
                id:target
                multiline:False
            
            
        GridLayout:
            cols:3
            row_force_default: True
            row_default_height: 50

            Label:
                text:""
            
            Button:
                text:"Submit"
                on_release:
                    root.get_results()
            
            Label:
                text:""


<OutputWindow>
    name:"output"
    
    res_out:res_out

    GridLayout:
        id:res_out
        cols : 1
        
    
    Button:
        id:ms
        text:"Main Screen"
        on_release:
            root.manager.transition.direction = "right"
            root.main_window()

我真的不能说我在其中遗漏的重点是什么。

您的代码:

def get_results(self):
    #OutputWindow.main(options = options)
    out_window = OutputWindow()
    out_window.main(self.target.text)
    sm.current = "output"

正在行中创建 OutputWindow 的新实例:

out_window = OutputWindow()

然后调用该新实例的 main() 方法。但是,该新实例不在您的 GUI 中,因此没有观察到任何效果。要更正此问题,请更改代码以使用 GUI 中的 OutputWindow 实例:

def get_results(self):
    out_window = self.manager.get_screen('output')  # get OutputWindow instance
    out_window.main(self.target.text)
    sm.current = "output"

您还需要调整 OutputWindowButton 的 size/position,使其不完全覆盖 GridLayout