Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

python 3.x - Kivy pass variable to screen and change screen

Edit: i was trying to make a minmal app as John Anderson from the first answer told me to do to reproduce my problem but i wasn't able but i got another error how can i make my self.manager to work along on the screens?

'NoneType' object has no attribute 'get_screen'
  File "C:Users
bertDesktopstackoverflowdashboard.py", line 39, in update_post_screen
    self.manager.get_screen("Post_view").list = list([post_id, post_body])
  File "C:Users
bertDesktopstackoverflowdashboard.py", line 28, in __init__
    self.update_post_screen(self.item['post_id'], self.item['post_body'])
  File "C:Users
bertDesktopstackoverflowmain.py", line 12, in build
    return Builder.load_file("main.kv")
  File "C:Users
bertDesktopstackoverflowmain.py", line 16, in <module>
    CyberXtremeApp().run()

well i've searched along stack overflow but i didnt get an answer for what i'm searching i have screens in separated files and my ScreenManager was specified in the main.kv file and what i'm trying to do is change some values on other screen before enter but when i try to change values with self.app.manager.get_screen it says

File "C:Python39libsite-packageskivyuixscreenmanager.py", line 1071, in get_screen
     raise ScreenManagerException('No Screen with name "%s".' % name)
 kivy.uix.screenmanager.ScreenManagerException: No Screen with name "Post_view".

my screen code is this one it's called dashboard.py

from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.list import MDList, IconLeftWidget, OneLineIconListItem
from kivy.uix.scrollview import ScrollView
import json
import requests



class Dashboard(Screen):

    def __init__(self, **kw):
        super().__init__(**kw)
        self.sub_title = ""
        self.nombre = ""
        self.Bienvenido = ""
        self.scroll = ScrollView()
        self.posts = list(self.get_posts())
        self.sorted_posts = self.posts.reverse()
        self.list_view = MDList()
        for self.item in self.posts:
            if self.item["post_status"] == "3":
                print("post oculto")
            else:
                self.icons = IconLeftWidget(icon="music")
                print(self.item['post_id'] + ", " + self.item['post_body'])
                self.update_post_screen(self.item['post_id'], self.item['post_body'])
                self.items = OneLineIconListItem(text=self.item['post_title'], on_press=lambda x:self.update_post_screen(self.item['post_id'], self.item['post_body']))
                self.items.add_widget(self.icons)
                self.list_view.add_widget(self.items)

        self.scroll.add_widget(self.list_view)
        # End List

        self.add_widget(self.scroll)

    def update_post_screen(self, post_id, post_body):
        self.manager.get_screen("Post_view").list = list([post_id, post_body])
        self.manager.current = 'Post_view'
        return print("ok")

    
    def on_enter(self, *args):
        self.app.title = "Posts"
    

    def get_posts(self):

        url = "https://example/api/"

        payload = {}
        headers = {'Content-Type': 'application/json'}
        response = requests.request("GET", url, headers=headers, data=payload)
        return json.loads(response.text)

my dashboard.kv file is

# -*- coding: utf-8 -*-
<Dashboard>:

  FloatLayout:
    MDLabel:
      id: sub_title
      pos_hint: { "center_x": 0.02, "top": 1.03}
      size_hint: (.5, .2)
      theme_text_color: "Primary"
      font_style: "Caption"
      halign: "center"
      text: root.sub_title

    MDLabel:
      id: Bienvenido
      pos_hint: { "center_x": 0.20, "top": 1.03 }
      size_hint: (.3, .2)
      theme_text_color: "Primary"
      font_style: "Caption"
      halign: "center"
      text: root.Bienvenido

    MDLabel:
      id: nombre
      pos_hint: {"center_x": 0.28, "top": 1.03}
      size_hint: (.3, .2)
      theme_text_color: "Primary"
      font_style: "Caption"
      halign: "center"
      text: root.nombre

my main.py

from kivymd.app import MDApp
from kivy.lang import Builder
from dashboard import Dashboard
from post_view import Post_view
    
class CyberXtremeApp(MDApp):
    def build(self):
        self.title = "TestApp"
        self.theme_cls.primary_palette == "Blue"
        
        return Builder.load_file("main.kv")
        print(self.ids)
    
if __name__ == "__main__":
    CyberXtremeApp().run()

how can i pass variables through screen and change to screen with the ScreenManager specified on the kv file? please don't explain me how to pass variables through screen on the same file because i already tried it and works but when i try to do with separated files the error comes up

question from:https://stackoverflow.com/questions/65922201/kivy-pass-variable-to-screen-and-change-screen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Difficult to be sure without more of your code, but I would suggest changing:

    self.app.manager.get_screen("Post_view").list = list([post_id, post_body])
    self.parent.current = "Post_view"

to:

    self.manager.get_screen("Post_view").list = list([post_id, post_body])
    self.manager.current = "Post_view"

Note that when a Screen is added to a ScreenManager, that ScreenManager is assigned to the manager attribute of the Screen.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...