【问题标题】:How do I make FileChooser update it's path or selection [Kivy]?如何让 FileChooser 更新它的路径或选择 [Kivy]?
【发布时间】:2018-06-08 14:26:26
【问题描述】:

我是 Kivy 的新手,经过大量的反复试验,我得到了一个相当不错的工作应用程序。

我一生无法弄清楚的是如何让 FileChooser 根据我选择的内容(目录或其他)更新其路径。

.selection 总是返回并清空列表,.path 总是返回起始目录,即使我已经移动了。我在某个地方错过了一些活动,并且花了很多时间试图解决这个问题,并希望这里有人可以提供帮助。

我以为.selection.path 会在我导航时更新,但它们似乎停留在它们的第一个/默认值上。我创建了testbutton 只是为了看看我是否可以获得路径或选择来打印/更新。

一旦他们点击另一个按钮,这一切都会被触发

def testbutton(self,test,iconview,*args):
    print(test,iconview.path)

filepop=Popup(title='SaveFile')
FileChooserLayout=BoxLayout(orientation='vertical')
ButtonArea=BoxLayout(orientation='horizontal',spacing=50,size_hint=(.5,.5),pos_hint={'center_x': 0.5, 'center_y': 0.5})    
listview=FileChooserListView(path='somepath',dirselect=True)
test=listview.path
testbutton=Button(text='test',on_press=partial(self.testbutton, test,iconview))
ButtonArea.add_widget(testbutton)
FileChooserLayout.add_widget(ButtonArea)
filepop.add_widget(FileChooserLayout)

感谢您的帮助!

【问题讨论】:

    标签: python-3.x kivy filechooser


    【解决方案1】:

    FileChooserListView中,使用on_selection事件并将*args传递给方法。详情请参考示例。

    sn-ps

            FileChooserListView:
                on_selection: 
                    app.root.selected_file(*args)
    

    示例

    main.py

    ​​>
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.popup import Popup
    from kivy.lang import Builder
    
    Builder.load_string('''
    #:kivy 1.10.0
    
    <SaveFile>:
        title: 'Save File'
    
        # FileChooserLayout
        BoxLayout:
            orientation: 'vertical'
    
            # ButtonArea
            BoxLayout:
                orientation: 'horizontal'
                spacing: 50
                size_hint: (.5,.5)
                pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    
                FileChooserListView:
                    on_selection: 
                        app.root.selected_file(*args)
    
    <RootWidget>:
    
    ''')
    
    
    class SaveFile(Popup):
        pass
    
    
    class RootWidget(BoxLayout):
    
        def __init__(self, **kwargs):
            super(RootWidget, self).__init__(**kwargs)
            filepop = SaveFile()
            filepop.open()
    
        def selected_file(self, *args):
            print("*args=", args)
            for arg in args:
                print("arg=", arg)
    
    
    class DemoApp(App):
        def build(self):
            return RootWidget()
    
    
    if __name__ == '__main__':
        DemoApp().run()
    

    输出

    【讨论】:

    • 感谢您的回复。我试图在不使用 .kv 文件的情况下执行此操作。我早些时候在迭代地制作按钮时遇到了另一面墙,发现(对我来说)用直接的 python 更容易做到这一点。我在为不同的小部件创建单独的类方面做得非常糟糕。目前几乎整个应用程序都属于 1 个类...所以当我尝试使用类似于 self.on_selection 的方法时,它只是指有意义的类。我不能让它引用自己,因为那是我调用 on_selection 的同一行。
    【解决方案2】:

    我发现 on_submit 事件成功

    https://kivy.org/docs/_modules/kivy/uix/filechooser.html

    我必须在我的程序中将每种类型的 FileChooser 设置为自己的类,并为其提供 on_submit 函数,然后我才能获得要打印的地址的响应。

    class FileChooserI(FileChooserIconView):
        def on_submit(*args):
            print(args[1][0])
    

    这篇文章真的很有帮助:Kivy FileChooser doubleclick

    【讨论】:

    • 我也是 kivy 的新手,但您可以“绑定”到 on_submit 事件,或将 on_submit = funcname 传递给构造函数,并且只使用 1 个类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多