D
D
djars5002022-04-04 12:55:25
Kivy
djars500, 2022-04-04 12:55:25

How to change screen on button click in kivy?

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from kivy.core.window import Window
from kivymd.uix.textfield import TextInput
from kivymd.uix.button import MDFillRoundFlatButton
import sqlite3
from kivymd.uix.list import OneLineListItem
from kivy.clock import Clock


con = sqlite3.connect('my.db')
c = con.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS post(
    title TEXT NOT NULL,
    main_text TEXT NOT NULL,
    result TEXT
    )""")
con.commit()
con.close()
    
        
Window.size = (412, 915)
Builder.load_string("""



<ScreenManager>:
    MenuScreen:  
    AddPost:
    ReadPost: 
        id: read_post
    ResultPost:
    DetailPost:

<MenuScreen>:
    MDBoxLayout:
        orientation: "vertical"
        
        MDToolbar:
            type: "top"
            title: "Скорочтение"
            
        MDBoxLayout:
            
            orientation: "vertical"
            padding:200, 550, 200, 550
            spacing:25
            
            MDRoundFlatIconButton:
            
                text: "Добавить запись"   
                width: root.width*0.6
                font_size: 40
                icon: "plus"
                on_release: root.manager.current = 'add'
                
                
            MDRoundFlatIconButton:
            
                text: "Читать на скорость"   
                width: root.width*0.6
                font_size: 40
                icon: "clock"
                on_press: root.manager.current = 'read'
                
                
            MDRoundFlatIconButton:
            
                width: root.width*0.6
                font_size: 40
                icon: "eye"
                text: "Результаты"    
                on_press: root.manager.current = 'result'
                
            
    
<AddPost>:
    MDBoxLayout:
        orientation: "vertical"
        MDToolbar:
            type: "top"
            title: "Добавить запись"
            left_action_items: 
            
        MDBoxLayout:
            orientation: "vertical"
            padding:100, 200, 100, 600
            spacing:45
            
            MDTextField:
                id: title
                hint_text:"Описание"
                pos_hint:{'center_x':.5,'center_y':.5}
                font_size:"18dp"
                line_color: "white"
                width: 350
                on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
            MDTextField:
                id: main_text
                hint_text:"Основной текст"
                pos_hint:{'center_x':.5}
                font_size:"18dp"
                line_color: "white"
                multiline: True
                size_hint: (1.0, None)
                max_height: 500
                on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
            MDRoundFlatButton:
                width: root.width * 0.75
                font_size: "18dp"
                text: "Сохранить"   
                halign: "center"
                on_press: app.submit(root.ids.title.text, root.ids.main_text.text)
                
            

<ReadPost>:
    ScrollView:
   
    MDBoxLayout:
        
        orientation: "vertical"
        MDToolbar:
            type: "top"
            title: "Читать текст"
            left_action_items: 
        ScrollView:

            MDList:
                id: container
            
<ResultPost>:
    ScrollView:
 
    MDBoxLayout:
        
        orientation: "vertical"
        MDToolbar:
            type: "top"
            title: "Результаты"
            left_action_items: 
        ScrollView:

            MDList:
                id: container
<DetailPost>:
    ScrollView:
          
    MDBoxLayout:
        id: detail
        orientation: "vertical"
        MDToolbar:
            type: "top"
            title: ""
            left_action_items: 
        ScrollView:

            MDList:
                id: container
                         
"""
)

class MenuScreen(Screen):
    
    pass

class AddPost(Screen):  
    
    pass
    
class ResultPost(Screen):
    pass

class DetailPost(Screen):
    pass
    
class ReadPost(Screen):
    
    def __init__(self, **kw):
        self.sm = MainApp()
        Clock.schedule_once(self.create_list)
        
        super().__init__(**kw)
    
    def change_screen(self):
        self.manager.current = 'result'
     
    
    @staticmethod
    def update(self,new_object):
        self.ids.container.add_widget(
            OneLineListItem(
                text=new_object,
                on_press=self.change_screen(),
                )
        )

    def create_list(self, *args):
        con = sqlite3.connect("my.db")
        c = con.cursor()
        data = """SELECT title FROM post"""
        c.execute(data)
        posts = c.fetchall() 
        
        
        for post in posts:
            self.ids.container.add_widget(
                OneLineListItem(text=post[0])
            )
        con.close()

class MainApp(MDApp):
  
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.read_post = ReadPost(name="read")
           
    def set_screen(self, name):
        
        self.root.current = name
      
    def build(self):
        
        self.sm = ScreenManager()
        self.sm.add_widget(MenuScreen(name="menu"))
        self.sm.add_widget(AddPost(name="add"))
        self.sm.add_widget(self.read_post)
        self.sm.add_widget(ResultPost(name="result"))
        self.sm.add_widget(DetailPost(name="detail"))
        return self.sm

    def submit(self, title, main_text):
        if title == '' or main_text == '':
            print('eror')
        else: 
            con = sqlite3.connect("my.db")
            c = con.cursor()
            c.execute("""INSERT INTO post(
                title, main_text)
                VALUES (?,?);""",
                (title, main_text))
            con.commit()
            
            data = """SELECT title FROM post WHERE title = title"""
            c.execute(data)
            post = c.fetchall()
            self.root.ids.read_post.ids.container.add_widget(
                OneLineListItem(text=f'{post}')
            )
                
            ReadPost.update(self.read_post,title)
            
            self.root.current = 'read'

MainApp().run()


@staticmethod
    def update(self,new_object):
        self.ids.container.add_widget(
            OneLineListItem(
                text=new_object,
                on_press=self.change_screen(),
                )
        )

When you press the button, the screen should change, please do not judge strictly

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question