如何将方法附加到kv文件按钮?

时间:2019-07-18 06:58:45

标签: python kivy

我正在制作一个带有背景图像和两个相当简单的按钮,一个打开按钮以及另一个称为扫描的程序。当用户按下“打开”按钮时,将出现“浏览文件”对话框(tkinter),使用户可以选择要打开的文档。如果他还按下“扫描”按钮,则程序将开始扫描特定的单词。否则,将按原样打开文件。

问题是我在用户界面(.kv文件)中使用了kivy,并且无法正确调用方法。如果他们确实能够远程运行,则屏幕的其余部分(尤其是背景)将因此受到影响。

这是.py和.kv文件的代码:-

py文件

    class Scorpa(App):
        def open_file(self,file):
            self.curr_path= os.getcwd()
            file=filedialog.askopenfilename
                (initialdir=os.path.dirname(curr_path),
                initialfile=os.path.basename(curr_path),
                filetypes=[("Word files", "*.docx *.doc")])

            if self.file != None:
                if self.fopen.open_file == True:
                    scan(file)

                else:
                    os.startfile(file)

    if __name__ == '__main__':
        Scorpa().run()

kv文件

        BoxLayout:
            orientation: 'vertical' 
            padding: 20
            spacing: 20

            canvas:
              Rectangle:
                size: self.size
                pos: self.pos
                source: 'ScorpaTest2.png'

            Button:
                id: fopen
                size: 200, 30
                pos: 0, 0
                color: .0, .0, .0, 1.
                font_size: 25
                text: 'Open'
                bold: True
                size_hint:  1, 1
                on_press: app.open_file()

            Button:
                id: scan
                size: 200, 30
                pos: 0, 0
                color: .0, .0, .0, 1.
                font_size: 25
                text: 'Scan'
                bold: True
                on_press: root.read()

            Label:
                size: 70, 30
                pos: 30, 0
                text: 'What\'s Next?'
                color: .0, .0, .0, 1.
                font_size: 25
                bold: True

这是我按下“打开”按钮时遇到的错误之一:-

         Traceback (most recent call last):
           File "ScorpaApp.py", line 113, in <module>
             Scorpa().run()
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\app.py", line 855, in run
             runTouchApp()
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 504, in runTouchApp
             EventLoop.window.mainloop()
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 747, in mainloop
             self._mainloop()
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 479, in _mainloop
             EventLoop.idle()
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 342, in idle
             self.dispatch_input()
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 327, in dispatch_input
             post_dispatch_input(*pop(0))
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 233, in post_dispatch_input
             listener.dispatch('on_motion', etype, me)
           File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\__init__.py", line 1402, in on_motion
             self.dispatch('on_touch_down', me)
           File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\__init__.py", line 1418, in on_touch_down
             if w.dispatch('on_touch_down', touch):
           File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\widget.py", line 549, in on_touch_down
             if child.dispatch('on_touch_down', touch):
           File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
             self.dispatch('on_press')
           File "kivy\_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
           File "kivy\_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
           File "kivy\_event.pyx", line 1098, in kivy._event.EventObservers._dispatch
           File "C:\Users\Laptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\lang\builder.py", line 64, in custom_callback
             exec(__kvlang__.co_value, idmap)
           File "C:\Python Programs\scorpa.kv", line 35, in <module>
             on_press: app.open_file()
         TypeError: open_file() missing 1 required positional argument: 'file'

0 个答案:

没有答案