'How should i call a new screen?
I have a ScreenManager problem (Lack of knowledge).
I can't call another screen from a button on my RecycleView.
Note that I have 2 ScreenManager, on the first I login and I am directed to other screens of another ScreenManager.
Note in the line that contains "# <<<< HERE" the way I call the new screen.
import json
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.uix.relativelayout import RelativeLayout
from random import sample, choice
from string import ascii_lowercase
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
sample_images = [
"https://cdn.neemo.com.br/uploads/settings_webdelivery/logo/5591/No-Image.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/SL_Bundala_NP_asv2020-01_img08.jpg/640px-SL_Bundala_NP_asv2020-01_img08.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Palaeobranchiostoma_hamatotergum.jpg/482px-Palaeobranchiostoma_hamatotergum.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/No_image_available.svg/1024px-No_image_available.svg.png",
]
KV = '''
<ContentNavigationDrawer>:
orientation: 'vertical'
FloatLayout:
size_hint_y: None
height: "200dp"
BoxLayout:
id: box_image
x: root.x
pos_hint: {"top": 1}
FloatLayout:
# Imagem de fundo
FitImage:
pos_hint: {'center_x': .5, 'center_y': .5}
source: "./images/menu.png"
# Imagem do usuário
FitImage:
pos_hint: {'center_x': .5, 'center_y': .5}
source: "./images/user.png"
size_hint: None, None
width: dp(150)
height: dp(150)
radius: [99, 99, 99, 99]
MDLabel:
text: "Coletor de dados Postgres"
size_hint_y: None
height: self.texture_size[1]
# Define a posição do Label
x: root.x + 10
y: root.height - box_image.height + dp(10)
ScrollView:
MDList:
OneLineIconListItem:
text: "Tela Inicial"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = 'tela_item'
IconLeftWidget:
icon: "view-list"
## Tamanho do ícone user_font_size: "72sp"
OneLineIconListItem:
text: "Outra Tela"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = 'tela_signup'
IconLeftWidget:
icon: "database-export-outline"
OneLineIconListItem:
text: "Imagem maior"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = 'tela_login'
IconLeftWidget:
icon: "database-export-outline"
<MainScreen>:
MDToolbar:
id: toolbar
pos_hint: {"top": 1}
elevation: 10
title: "Pesquise e compre."
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
right_action_items: [['lightbulb-outline', lambda x: app.color()], ['cart', lambda x: app.compras()]]
MDNavigationLayout:
x: toolbar.height
ScreenManager:
id: screen_manager
ScreenRecycleView
TelaLogin
TelaSingUp
TelaItem
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
<MyImageCard>:
name: 'my_image_card'
AsyncImage:
source: root.source
Label:
size_hint_y: None
height: dp(48)
text: root.text
color: 'black'
canvas:
Color:
rgba: 0, 0, 0, .3
Rectangle:
size: self.size
MDFlatButton:
text: "New Screen Here"
increment_width: "164dp"
#app.root.ids.sm.get_screen('main').ids.screenmanager.current = 'new_screen'
on_release:
root.screen_manager.current = 'tela_item' # <<<< HERE
<TelaItem>:
name: 'tela_item'
FloatLayout:
orientation: "vertical"
id: body_tela_item
FitImage:
pos_hint: {'center_x': .5, 'center_y': .85}
source: "./images/eu.jpeg"
size_hint: None, None
width: dp(90)
height: dp(90)
radius: [99, 99, 99, 99]
MDRaisedButton:
text: 'Add'
size_hint: .9, None
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: root.singup()
MDRaisedButton:
text: 'Back'
size_hint: .9, None
pos_hint: {'center_x': .5, 'center_y': .3}
on_release: root.singup()
<ScreenRecycleView>:
name: 'recycle_view'
FloatLayout:
MDBoxLayout:
pos_hint: {'center_x': 0.5, 'center_y': 0.4}
RecycleView:
id: rv
viewclass: "MyImageCard"
RecycleGridLayout:
cols: 2
row_default_height: (self.width - self.cols*self.spacing[0]) / self.cols
padding: dp(4), dp(4)
spacing: dp(4)
size_hint_y: None
height: self.minimum_height
default_size_hint: 1, None
default_size: None, dp(500)
<TelaLogin>:
name: 'tela_login'
FloatLayout:
orientation: "vertical"
id: body_tela_login
FitImage:
pos_hint: {'center_x': .5, 'center_y': .85}
source: "./images/eu.jpeg"
size_hint: None, None
width: dp(90)
height: dp(90)
radius: [99, 99, 99, 99]
MDRaisedButton:
text: 'Login'
size_hint: .9, None
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: root.login()
<TelaSingUp>:
name: 'tela_singup'
id: body_tela_singup
FloatLayout:
MDIconButton:
icon: "arrow-left-bold"
pos_hint: {"center_x": .1, "center_y": .9}
on_release: app.root.current = 'tela_login'
FitImage:
pos_hint: {'center_x': .5, 'center_y': .8}
source: "./images/user.png"
size_hint: None, None
width: dp(150)
height: dp(150)
radius: [99, 99, 99, 99]
MDRaisedButton:
text: 'Cadastrar'
size_hint: .9, None
pos_hint: {'center_x': .5, 'center_y': .1}
on_release: root.singup()
ScreenManager:
id: sm:
TelaLogin:
name: 'tela_login'
MainScreen:
name: 'main'
'''
class ContentNavigationDrawer(BoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class MainScreen(Screen):
pass
class TelaSingUp(Screen):
pass
class TelaItem(Screen):
pass
class TelaLogin(Screen):
def login(self):
self.manager.current = 'main'
class MyImageCard(RelativeLayout):
text = StringProperty()
source = StringProperty()
class ScreenRecycleView(Screen):
def on_kv_post(self, base_widget):
self.ids.rv.data = [
{
'source': choice(sample_images),
'text': ''.join(sample(ascii_lowercase, 6))
}
for x in range(50)
]
class MyApp(MDApp):
def build(self):
root = Builder.load_string(KV)
return root
MyApp().run()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
