首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kivy: FileChooser重绘

Kivy: FileChooser重绘
EN

Stack Overflow用户
提问于 2018-01-12 08:04:23
回答 1查看 138关注 0票数 0

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-12 21:22:14

我的问题是基于下一个代码的问题。当我删除根目录中的文件夹时,FileChooserListView的内容是不正确的。问题的根源在于名称。光盘名中的最后一个符号不是“**”。添加之后(函数delete_dir() )就没有问题了。

代码语言:javascript
复制
   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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48221957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档