Kivy:FileChooser重绘

时间:2018-01-12 08:04:24

标签: kivy

我想使用FileChooser进行基本操作。我使用FileChooser选择文件夹,我将使用自己的功能删除它。文件夹被删除,我想显示光盘的新内容但内容不正确我如何显示光盘的当前内容?

1 个答案:

答案 0 :(得分:0)

我的问题来自下一个代码的问题。当我删除根目录中的文件夹时, FileChooserListView 的争议不正确。问题的根源在于discname。光盘名称中的最后一个符号不是' **'。添加后这个(函数 delete_dir ()不是问题。

   Builder.load_string('''
    <ConfirmPopup>:
        cols:1
        Label:
            text: root.text
        GridLayout:
            cols: 2
            size_hint_y: None
            height: '44sp'
            Button:
                text: 'Yes'
                on_release: root.dispatch('on_answer','yes')
            Button:
                text: 'No'
                on_release: root.dispatch('on_answer', 'no')
    ''')

    class ConfirmPopup(GridLayout):
        text = StringProperty('')
        def __init__(self,**kwargs):
            self.register_event_type('on_answer')
            super(ConfirmPopup,self).__init__(**kwargs)

        def on_answer(self, *args):
           pass 


    class PopupYesNo(GridLayout):

        def __init__(self, save_as, task):
             self.save_as = save_as
             self.task    = task

        def show_widget(self, question):
            self.content = ConfirmPopup(text= question)
            self.content.bind(on_answer = self._on_answer)
            self.popup = Popup(title="Answer Question",
                                content=self.content,
                                size_hint=(None, None),
                                size=(480,400),
                                auto_dismiss= False)
            self.popup.open()

        def _on_answer(self, instance, answer):
                if answer == 'yes':
                    self.save_as.act_task()
                self.popup.dismiss()    
                return  


    class SaveAs(BoxLayout):
        def __init__(self, **kwargs):
            super(SaveAs,self).__init__(**kwargs)

            self.orientation = 'vertical'
            self.fichoo = FileChooserListView(size_hint_y = 0.8)
            self.add_widget(self.fichoo)

            control   = GridLayout(cols = 5, row_force_default=True, row_default_height=35, size_hint_y = 0.14)

            self.tein_dir  = TextInput(size_hint_x = None, width = 350)
            self.tein_dir.multiline = False
            bt_del_dir = Button(text = 'Remove',size_hint_x = None, width = 80)
            bt_del_dir.bind(on_release = self.on_delete_dir)
            control.add_widget(self.tein_dir)
            control.add_widget(bt_del_dir)
            self.fichoo.bind(path = self.on_path_select)              
            self.add_widget(control)
            return

        def on_path_select(self, inst, val):
            self.tein_dir.text = str(self.fichoo.path)
            return
        def on_delete_dir(self, obj):
            question = 'Do You want to remove: '+ self.tein_dir.text+ '?'
            self.act_task = self.delete_dir 
            popup = PopupYesNo(self, SaveAs.delete_dir)
            popup.show_widget(question)
            return

        def delete_dir(self):
            pos = self.fichoo.path.rfind('\\', 0, len(self.fichoo.path))
            new_path = str(self.fichoo.path)[0:pos]
            if new_path[-1] == ':':
                new_path += '\\' # last symbol in discname is '\'
            self.tein_dir.text  = new_path
            os.chdir(new_path)
            shutil.rmtree(str(self.fichoo.path))
            self.fichoo.path = new_path
            return    

    class ExplorerApp(App):
        def build(self):
            self.save_as = SaveAs()         
            return self.save_as

    if __name__ == '__main__':
        ExplorerApp().run()