KIVY. Как решить: AttributeError: 'NoneType' object has no attribute 'current'?

новичок в питоне

Новичок
Пользователь
Май 5, 2020
18
1
3
Есть окно:



Python:
from os.path import abspath, basename
import sys

from kivy.app import App

from kivy.config import Config
Config.set('graphics', 'width', '550')
Config.set('graphics', 'height', '550')

from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.stacklayout import StackLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen

from kivy.lang import Builder

File_location = str(abspath(__file__)).replace(str(basename(__file__)),'')
Builder.load_file('player_1_design.kv')

class Player_1(Screen):
    global File_location
    File_loc = File_location
    font_loc = File_loc + 'arial_black.ttf'

class Money(Screen):
    pass

sm_pl_1 = ScreenManager()
sm_pl_1.add_widget(Player_1(name='Main'))
sm_pl_1.add_widget(Money(name='Money'))

class StartApp(App):
    def build(self):
        return sm_pl_1

if __name__ == "__main__":
    StartApp().run()

И элементы окна:

Python:
<Player_1>:
    StackLayout:
        spacing: 5
        padding: 2
        Image:
            sourse: root.File_loc + 'polzovatel_1.png'
            size_hint: 1,0.3
            pos_hint: (0.90, 0.99)
        Button:
            text: 'Валюта'
            size_hint: 0.30, 0.07
            pos_hint: {'center_x': 0.15, 'center_y': 0.1}
            font_size: 20
            font_name: root.font_loc
            on_press: root.manager.current = 'Money'

        TextInput:
            text:'Название компании'
            multiline: False
            size_hint: 0.40, 0.07
            pos_hint: (None, 0.15)
            font_size: 17
            font_name: root.font_loc

        Button:
            text: 'Репутация'
            size_hint: 0.30, 0.07
            pos_hint: {'center_x': 0.45, 'center_y': 0.75}
            font_size: 20
            font_name: root.font_loc
        Button:
            text: 'Кредит'
            size_hint: 0.30, 0.07
            pos_hint: {'center_x': 0.60, 'center_y': 0.75}
            font_size: 20
            font_name: root.font_loc
        Button:
            text: 'Открытие рынков'
            size_hint: 0.40, 0.07
            pos_hint: {'center_x': 0.60, 'center_y': 0.75}
            font_size: 20
            font_name: root.font_loc
        Button:
            text: 'Доход'
            size_hint: 0.30, 0.07
            pos_hint: {'center_x': 0.60, 'center_y': 0.75}
            font_size: 20
            font_name: root.font_loc

Также есть главное окно, где все модули собираются в программу:

Python:
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.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen

from players import player_1

class Main(BoxLayout):
    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)
        self.p1 = player_1.Player_1()
        self.add_widget(self.p1)

sm = ScreenManager()
class StartApp(App):
    def build(self):
        return Main()

if __name__ == "__main__":
    StartApp().run()

При нажатии на кнопку "Валюта" из главного окна, появляется ошибка:

Python:
  Traceback (most recent call last):
File "C:\Users\Danila\Desktop\game_kivy\main.py", line 24, in <module>
  StartApp().run()
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\app.py", line 855, in run
  runTouchApp()
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\base.py", line 504, in runTouchApp
  EventLoop.window.mainloop()
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 747, in mainloop
  self._mainloop()
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 479, in _mainloop
  EventLoop.idle()
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\base.py", line 342, in idle
  self.dispatch_input()
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\base.py", line 327, in dispatch_input
  post_dispatch_input(*pop(0))
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-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\Danila\AppData\Local\Programs\Python\Python37-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\Danila\AppData\Local\Programs\Python\Python37-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\Danila\AppData\Local\Programs\Python\Python37-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\Danila\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\uix\relativelayout.py", line 288, in on_touch_down
  ret = super(RelativeLayout, self).on_touch_down(touch)
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-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\Danila\AppData\Local\Programs\Python\Python37-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\Danila\AppData\Local\Programs\Python\Python37-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\Danila\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\lang\builder.py", line 64, in custom_callback
  exec(__kvlang__.co_value, idmap)
File "C:\Users\Danila\Desktop\game_kivy\players\player_1_design.kv", line 15, in <module>
  on_press: root.manager.current = 'Money'
 

stud_55

Модератор
Команда форума
Модератор
Апр 3, 2020
1 522
672
113
Попробуйте заменить в .kv-файле строку:

Python:
on_press: root.manager.current = 'Money'
на такую:
Python:
on_press: app.root.current = 'Money'
 

новичок в питоне

Новичок
Пользователь
Май 5, 2020
18
1
3
Попробуйте заменить в .kv-файле строку:

Python:
on_press: root.manager.current = 'Money'
на такую:
Python:
on_press: app.root.current = 'Money'

Ошибки нет, но экраны не переключаются.
 

stud_55

Модератор
Команда форума
Модератор
Апр 3, 2020
1 522
672
113
Ошибки нет, но экраны не переключаются.
Вам нужно в главном окне добавлять в качестве виджета не скрин а менеджер скринов:
Python:
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.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen

from players import player_1

class Main(BoxLayout):
    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)
        self.p1 = player_1.sm_pl_1
        self.add_widget(self.p1)

sm = ScreenManager()
class StartApp(App):
    def build(self):
        return Main()

if __name__ == "__main__":
    StartApp().run()
а в .kv файле нужно сделать как было раньше:
Python:
on_press: root.manager.current = 'Money'
 

новичок в питоне

Новичок
Пользователь
Май 5, 2020
18
1
3
Вам нужно в главном окне добавлять в качестве виджета не скрин а менеджер скринов:
Python:
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.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen

from players import player_1

class Main(BoxLayout):
    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)
        self.p1 = player_1.sm_pl_1
        self.add_widget(self.p1)

sm = ScreenManager()
class StartApp(App):
    def build(self):
        return Main()

if __name__ == "__main__":
    StartApp().run()
а в .kv файле нужно сделать как было раньше:
Python:
on_press: root.manager.current = 'Money'
Спасибо
 

Форум IT Специалистов