2016-11-11 6 views
0

私は2つの異なるファイルからファイルパスを取得するためにKivyのサンプルコードを使用しています。 私の目標は、ファイルパスを使用してファイルからデータを開いて操作することです。kivy - ボタン実行機能

私の問題は、以下のテスト機能のファイルのオープンコマンドにファイルパスを渡すことです。ここで

は私のコードです:

from kivy.app import App  
from kivy.uix.button import Button  
from kivy.core.window import Window  
from kivy.uix.boxlayout import BoxLayout  
from kivy.uix.label import Label  

import re  
import pandas as pd 

class DropFile(Button): 

     def __init__(self, **kwargs): 
      super(DropFile, self).__init__(**kwargs) 
      # get app instance to add function from widget 
      app = App.get_running_app() 
      # add function to the list 
      app.drops.append(self.on_dropfile) 

     def on_dropfile(self, widget, path): 
      # a function catching a dropped file 
      # if it's dropped in the widget's area 
      if self.collide_point(*Window.mouse_pos): 
       self.text = path 

     def test(self): 
      minimum_wage = open(**FILE PATH HERE**) 
      LinesToString = '' 
      for line in minimum_wage: 
       LinesToString += line 
      patFinder = re.compile('\d{5}\s+\d{5,9}') 
      findPat = re.findall(patFinder, LinesToString) 
      empno_list = [] 
      pattern = '(\d{5})\s+(\d{5})' 
      for string in findPat: 
       match = re.search(pattern, string) 
       empno = match.group(2) 
       empno_list.append(empno) 
      MinimumWage = pd.DataFrame({'EMPNO': empno_list}) 
      MinimumWage.set_index('EMPNO') 
      print MinimumWage.head() 
      print MinimumWage.shape 

class DropApp(App): 

     def build(self): 
      # set an empty list that will be later populated 
      # with functions from widgets themselves 
      self.drops = [] 
      # bind handling function to 'on_dropfile' 
      Window.bind(on_dropfile=self.handledrops) 
      box = BoxLayout(orientation='vertical') 
      top_label = Label(text='Data manipulation', font_size=45) 
      box.add_widget(top_label) 

      run_button = Button(text='Run', size_hint=(1, 0.5)) 
      run_button.bind(on_press=DropFile.test) 
      box.add_widget(run_button) 

      two_buttons = BoxLayout(orientation='horizontal') 
      dropleft = DropFile(text='Drag & Drop File here') 
      # dropright = DropFile(text='right') 
      two_buttons.add_widget(dropleft) 
      # two_buttons.add_widget(dropright) 
      box.add_widget(two_buttons) 
      return box 

     def handledrops(self, *args): 
      # this will execute each function from list with arguments from 
      # Window.on_dropfile 
      # 
      # make sure `Window.on_dropfile` works on your system first, 
      # otherwise the example won't work at all 
      for func in self.drops: 
       func(*args) 

DropApp().run() 

おかげ

+0

インデントが壊れています。 –

+0

私はそれを修正しようとしているが、ちょうどインデントを壊し続けている – MaxKedem

+0

@MaxKedemコピーコードここでは、すべてを選択し、Ctrl + Kを押す – KeyWeeUsr

答えて

0

あなたが例えばon_dropfile()の最後の行にtest()メソッドを呼び出すことができます。既存のものから

def on_dropfile(self, widget, path): 
    # a function catching a dropped file 
    # if it's dropped in the widget's area 
    if self.collide_point(*Window.mouse_pos): 
     self.text = path 
     self.test(path) 

def test(self, path): 
    minimum_wage = open(path) 
    LinesToString = '' 
    ... 

や打ち上げすでに例えばあなたはon_dropfile()機能別にtest()を実行し、テキストを変更した後self.textプロパティを変更しない場合:

def on_dropfile(self, widget, path): 
    # a function catching a dropped file 
    # if it's dropped in the widget's area 
    if self.collide_point(*Window.mouse_pos): 
     self.text = path # path is assigned to self.text <-- 

def test(self): 
    minimum_wage = open(self.text) # <-- and you can use it 
    LinesToString = '' 
    ... 

それともon_dropfileの終わりを別々の変数にそれを入れて、open()にそれを使用。

+0

私はKeyWeeUsrにお答えいただきありがとうございます。でも、私は "Run"ボタンを押すとアプリケーションがクラッシュします... TypeError:test()は2つの引数(1つ)をとります。 – MaxKedem

+0

@MaxKedem Q&Aの仕組みについて[faq](http://stackoverflow.com/tour)があります。 :P – KeyWeeUsr