我已经创建了按钮作为驱动程序的数量,我用于循环。但创建的按钮路径是相同的,F:\,那么我应该做什么?
这是py文件:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.utils import platform
class MyApp(BoxLayout):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.get_drivers()
def get_drivers(self):
if platform == 'win':
import win32api
box_drivers = self.ids.box_drivers
box_drivers是包含按钮的框布局
drivers = win32api.GetLogicalDriveStrings()
drivers = drivers.split('\x00')[:-1]
drivers = [' C:\',' D:\',' E:\',' F:\& #39]
for each_driver in drivers:
button = Button()
button.text = each_driver
button.bind(on_press=lambda x:self.get_path(each_driver))
box_drivers.add_widget(button)
def get_path(self,driver_path):
filechooser = self.ids.file_chooser
filechooser.path = driver_path
class SrtEditor(App):
def build(self):
self.title = "Srt Editor"
return MyApp()
if __name__ == "__main__":
SrtEditor().run()
这是kv文件:
#:import os os
<MyApp>:
orientation: 'vertical'
BoxLayout:
BoxLayout:
id: box_drivers
size_hint_x:20
orientation: 'vertical'
Label:
text: 'Drivers'
size_hint_y:None
height: '40dp'
FileChooserIconView:
size_hint_x:80
id: file_chooser
filters: ['*.srt*']
on_selection: pass
path: os.getcwd()
这是应用图片:
答案 0 :(得分:1)
您使用相同的驱动器,因为您使用的lambda函数可以查看当前帧状态
#button.bind(on_press=lambda x: self.get_path(each_driver))
#should be
button.bind(on_press=lambda x, drive=each_driver: self.get_path(drive))
read more about python function scopes to understand the issue better