AttributeError:对象在类python kivy中没有属性

时间:2018-03-26 23:18:35

标签: python kivy

调用此类时,我得到AttributeError:' FixedLayout'对象没有属性' __ fit_to_window'。 我试图弄清楚为什么会这样,但我找不到任何有帮助的东西。 我在运行python 2.7的系统上看到了完全相同的代码,这可能是问题吗?我真的不知道还有什么可说的,但如果您有任何疑问,请询问,如果可以,我会回答。

        from kivy.uix.scatterlayout import ScatterLayout
        from kivy.properties import ObjectProperty
        from kivy.core.window import Window

        class FixedLayout(ScatterLayout):

            screensize = ObjectProperty((1920, 1080))

            def __init__(self, **kw):
                kw['size'] = (Window.width, Window.height)
                kw['size_hint'] = (None, None)
                kw['do_scale'] = False
                kw['do_translation'] = False
                kw['do_rotation'] = False
                super(FixedLayout, self).__init__(**kw)
                self.__fit_to_window()
                Window.bind(system_size=self.__fit_to_window)
                self.screensize = ObjectProperty((1920, 1080))

            def __fit_to_window(self, *args):
                screen_ratio = self.screensize[0]/float(self.screensize[1])
                window_ratio = Window.width/float(Window.height)
                if screen_ratio > window_ratio:
                    self.scale = Window.width/float(self.screensize[0])
                else:
                    self.scale = Window.height/float(self.screensize[1])
                print('self.scale ', self.scale)
                print('screen_ratio, window_ratio', screen_ratio, window_ratio)
                print('screensize', self.screensize)
                print('window size', Window.width, Window.height)

输出:

[INFO   ] [Kivy        ] v1.10.0
        [INFO   ] [Python      ] v3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
        [INFO   ] [Factory     ] 194 symbols loaded
        [INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
        [INFO   ] [OSC         ] using <thread> for socket
        [INFO   ] [Window      ] Provider: sdl2
        [INFO   ] [GL          ] Using the "OpenGL" graphics system
        [INFO   ] [GL          ] GLEW initialization succeeded
        [INFO   ] [GL          ] Backend used <glew>
        [INFO   ] [GL          ] OpenGL version <b'4.6.0 NVIDIA 388.43'>
        [INFO   ] [GL          ] OpenGL vendor <b'NVIDIA Corporation'>
        [INFO   ] [GL          ] OpenGL renderer <b'GeForce GTX 980 Ti/PCIe/SSE2'>
        [INFO   ] [GL          ] OpenGL parsed version: 4, 6
        [INFO   ] [GL          ] Shading version <b'4.60 NVIDIA'>
        [INFO   ] [GL          ] Texture max size <16384>
        [INFO   ] [GL          ] Texture max units <32>
        [INFO   ] [Window      ] auto add sdl2 input provider
        [INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
        self.scale  0.4166666666666572
        screen_ratio, window_ratio 1.7777777777777777 1.3333333333333333
        screensize (1920, 1080)
        window size 800 600
        [INFO   ] [GL          ] NPOT texture support is available
        self.scale  0.4166666666666572
        screen_ratio, window_ratio 1.7777777777777777 1.3333333333333333
        screensize (1920, 1080)
        window size 800 600
         Traceback (most recent call last):
           File "c:/Users/DWL/Documents/Python_Projects/Kivy/TestMancala/MancalaTest.py", line 23, in <module>
             presentation = Builder.load_file('mancala.kv')
           File "C:\Python\lib\site-packages\kivy\lang\builder.py", line 301, in load_file
             return self.load_string(data, **kwargs)
           File "C:\Python\lib\site-packages\kivy\lang\builder.py", line 382, in load_string
             self._apply_rule(widget, parser.root, parser.root)
           File "C:\Python\lib\site-packages\kivy\lang\builder.py", line 566, in _apply_rule
             self.apply(child)
           File "C:\Python\lib\site-packages\kivy\lang\builder.py", line 451, in apply
             self._apply_rule(widget, rule, rule, ignored_consts=ignored_consts)
           File "C:\Python\lib\site-packages\kivy\lang\builder.py", line 564, in _apply_rule
             child = cls(__no_builder=True)
           File "c:\Users\DWL\Documents\Python_Projects\Kivy\TestMancala\fixedlayout.py", line 17, in __init__
             Window.bind(system_size=self.__fit_to_window)
           File "kivy\_event.pyx", line 439, in kivy._event.EventDispatcher.bind (kivy\_event.c:6558)
           File "kivy\_event.pyx", line 951, in kivy._event.EventObservers.bind (kivy\_event.c:11000)
           File "C:\Python\lib\site-packages\kivy\weakmethod.py", line 47, in __call__
             return getattr(self.proxy, self.method_name)
         AttributeError: 'FixedLayout' object has no attribute '__fit_to_window'

0 个答案:

没有答案