簡體   English   中英

kivy-按鈕執行功能

[英]kivy - button executing function

我正在使用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()

謝謝

您可以在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()使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM