2017-08-13 15 views
1

私はドライバの数としてボタンを作成しましたが、私はこのためにループに使用しました。しかし、作成されたボタンのパスは同じでF:\だから、私は何をやるべきなのでしょうか?ここKivy、forループのボタンのパスを設定するには?

はPYファイルである: 'D:\'

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] 

ドライバ= [ '\ C' を含むボックスレイアウトであります、 'E:\'、 'F:\']

ここ

は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() 

アプリの画像です:

When i press drivers, they get same path(F:)

答えて

1

あなたは現在のフレームの状態を調べるラムダ関数を使用するので、あなたが同じドライブを取得している

#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

関連する問題