Python Kivy:没有kv语言,FileChooser如何选择目录?
下面是将FileChooser与kivy语言结合使用的示例。我只想在python代码中使用FileChooser。当我用鼠标标记目录时,按“选择目录”按钮,实际值在变量FileChooser.path中。未使用此按钮的选择没有结果。 在kv file from example is used on_selection事件中,我将此事件与我的函数绑定,但没有效果 我的问题是:Python Kivy:没有kv语言,FileChooser如何选择目录?,python,kivy,kivy-language,Python,Kivy,Kivy Language,下面是将FileChooser与kivy语言结合使用的示例。我只想在python代码中使用FileChooser。当我用鼠标标记目录时,按“选择目录”按钮,实际值在变量FileChooser.path中。未使用此按钮的选择没有结果。 在kv file from example is used on_selection事件中,我将此事件与我的函数绑定,但没有效果 我的问题是: 如何仅使用鼠标获取路径的值 哪个类在选择时使用事件 谢谢大家! class Explorer(BoxLayout):
class Explorer(BoxLayout):
def __init__(self, **kwargs):
super(Explorer,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)
lbl_dir = Label(text = 'Folder',size_hint_x = None, width = 80)
self.tein_dir = TextInput(size_hint_x = None, width = 350)
bt_dir = Button(text = 'Select Dir',size_hint_x = None, width = 80)
bt_dir.bind(on_release =self.on_but_select)
self.fichoo.bind(on_selection = self.on_mouse_select)
control.add_widget(lbl_dir)
control.add_widget(self.tein_dir)
control.add_widget(bt_dir)
self.add_widget(control)
return
def on_but_select(self,obj):
self.tein_dir.text = str(self.fichoo.path)
return
def on_mouse_select(self,obj):
self.tein_dir.text = str(self.fichoo.path)
return
def on_touch_up(self, touch):
self.tein_dir.text = str(self.fichoo.path)
return super().on_touch_up(touch)
return super().on_touch_up(touch)
几乎不需要做什么改变 在_selection上没有这样的事件
,文件选择器列表视图中有属性。您可以在具有这些属性的类中使用,但是当您使用bind时,应该只使用bind(
)
第二件事是,默认情况下,正如您在文档选择中所看到的那样,
包含选定文件的列表,而不是目录。要使目录真正可选择,您应该将属性更改为True
最后一个是鼠标选择签名时的on\u签名:属性以其值触发,您应该计算它
简要变动如下:
self.fichoo.dirselect = True
self.fichoo.bind(selection = self.on_mouse_select)
# ... and
def on_mouse_select(self, obj, val):
在那之后,你会做同样的按钮
如果您不想用您所处的实际路径,而是所选路径来填充输入,您可以执行以下操作:
def on_touch_up(self, touch):
if self.fichoo.selection:
self.tein_dir.text = str(self.fichoo.selection[0])
return super().on_touch_up(touch)
@谢谢!我真的通过两个函数来管理处理变量“path”和“selection”,即输入“on”。