📜  Python| kivy中的文件选择器

📅  最后修改于: 2022-05-13 01:55:18.311000             🧑  作者: Mango

Python| kivy中的文件选择器

Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux、Windows等平台上运行,基本上是用来开发Android应用的,但不代表不能用在Desktops应用上。

文件选择器:

FileChooser 模块提供了各种用于描述、显示和浏览文件系统的类。它很简单,就像我的电脑一样,我们可以从中选择系统中的任何文件。

  • FileChooserListView 将文件条目显示为垂直列表中的文本项,其中可以折叠和展开文件夹。
  • FileChooserIconView 从左到右显示图标和文本,并根据需要包装它们。

注意:以上两点都提供滚动、选择和基本用户交互。

Basic Approach 
1) import kivy
2) import kivyApp
3) import Boxlayot
4) Set minimum version(optional)
5) create layout class
6) create App class
7) create .kv file
8) return Layout/widget/Class(according to requirement)
9) Run an instance of the class or App

方法的实施:
.py 文件

Python3
# Program to explain how to use File chooser in kivy 
       
# import kivy module    
import kivy  
         
# base Class of your App inherits from the App class.    
# app:always refers to the instance of your application   
from kivy.app import App 
       
# this restrict the kivy version i.e  
# below this kivy version you cannot  
# use the app or software  
kivy.require('1.9.0') 
 
# BoxLayout arranges widgets in either in
# a vertical fashion that is one on top of
# another or in a horizontal fashion
# that is one after another.
from kivy.uix.boxlayout import BoxLayout
 
# create the layout class
class Filechooser(BoxLayout):
    def select(self, *args):
        try: self.label.text = args[1][0]
        except: pass
 
# Create the App class
class FileApp(App):
    def build(self):
        return Filechooser()
 
# run the App
if __name__ == '__main__':
    FileApp().run()


Python3
#, kv file implementation
 
:
     
    label: label
 
    # Providing the orientation
    orientation: 'vertical'
 
    # Creating the File list / icon view
     
    BoxLayout:
 
        # Creating list view one side
        FileChooserListView:
            canvas.before:
                Color:
                    rgb: .4, .5, .5
                Rectangle:
                    pos: self.pos
                    size: self.size
            on_selection: root.select(*args)
 
        # Creating Icon view other side
        FileChooserIconView:
            canvas.before:
                Color:
                    rgb: .5, .4, .5
                Rectangle:
                    pos: self.pos
                    size: self.size
            on_selection: root.select(*args)
 
    # Adding label
    Label:
        id: label
        size_hint_y: .1
        canvas.before:
            Color:
                rgb: .5, .5, .4
            Rectangle:
                pos: self.pos
                size: self.size


.kv 文件

Python3

#, kv file implementation
 
:
     
    label: label
 
    # Providing the orientation
    orientation: 'vertical'
 
    # Creating the File list / icon view
     
    BoxLayout:
 
        # Creating list view one side
        FileChooserListView:
            canvas.before:
                Color:
                    rgb: .4, .5, .5
                Rectangle:
                    pos: self.pos
                    size: self.size
            on_selection: root.select(*args)
 
        # Creating Icon view other side
        FileChooserIconView:
            canvas.before:
                Color:
                    rgb: .5, .4, .5
                Rectangle:
                    pos: self.pos
                    size: self.size
            on_selection: root.select(*args)
 
    # Adding label
    Label:
        id: label
        size_hint_y: .1
        canvas.before:
            Color:
                rgb: .5, .5, .4
            Rectangle:
                pos: self.pos
                size: self.size
        

输出: