Как сделать так, чтобы кнопка отклоняла всплывающие окна кивы - PullRequest
0 голосов
/ 28 марта 2020

Я знаю, что этот вопрос задают много, но я просто не могу понять ответы других людей. Я буду очень рад, если кто-нибудь может помочь сделать кнопку not_in_db_pop (), чтобы закрыть всплывающее окно, в котором оно находится. Я выложу и код python, и код:

Python код:

import kivy 
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from database import DataBase
from kivy.factory import Factory

import validity
from validity import Validity

""" the design of the popups"""

#to-do: add a registration page and connect the database to manager.py

#to-do: add a account management page

class loginPopupShow(FloatLayout):
    pass

class not_in_db_pop(FloatLayout):
   pass

class inc_pass(FloatLayout):
    pass

""" class for the login window """

class LoginWindow(Screen, FloatLayout, Widget):
    email = ObjectProperty(None)
    password = ObjectProperty(None)

    """ function to show the invalid login popup """

    def invalidLoginPopup(self):

        show = loginPopupShow()

        popupWindow = Popup(title="INVALID EMAIL OR PASSWORD", content = show, size_hint=(None, None), size=(400,400))

        popupWindow.open()

        """ function to show the already existing user popup """

    def not_in_db_pop_set(self):
        show = not_in_db_pop()

        popupWindow = Popup(title="EMAIL DOESN'T EXIST IN THE SYSTEM", content = show, size_hint=(None, None), size=(400,400))

        popupWindow.open()

    def incorrectPasswordPop(self):

        show = inc_pass()

        popupWindow = Popup(title="INCORRECT EMAIL OR PASSWORD", content = show, size_hint=(None, None), size=(400,400))
        #to-do: create design for inside 

        popupWindow.open()

    def check_input(self):
        valid_email = False
        valid_password = False
        correct_pass = False

        """ uses custom made funcition to check email and password """

        if validity.Validity.checkMail(self.email.text):
            print("Valid Email")
            valid_email = True
        else:
            print("Invalid Email")

        if validity.Validity.checkPassword(self.password.text):
            print("Valid Password")
            valid_password = True
        else:
            print("Invalid Password")

        if db.validate(self.email, self.password):
            correct_pass = True        

        if not (valid_email and valid_password):
            self.invalidLoginPopup()

        if valid_email and valid_password:
            self.email.text = ""
            self.password.text = ""

        if valid_email and valid_password and (correct_pass == False):
            if db.check_email(self.email):
                self.not_in_db_pop_set()
            else:
                self.incorrectPasswordPop()

            """ return True or False as str"""
        return valid_email and valid_password and correct_pass

class MainWindow(Screen, FloatLayout):
    pass

class WindowManager(ScreenManager, FloatLayout):
    pass

class RegPage(Screen, FloatLayout):
    pass

build = Builder.load_file("build.kv")

db = DataBase("users.txt")
wm = WindowManager()

class runApp(App):
    def build(self):
        return build

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

Kivy:

WindowManager:
    LoginWindow:
    MainWindow:
    RegPage:


<not_in_db_pop>:
    Label:
        text: "Would you like to create an account using this email?"
        size_hint: 0.6, 0.2
        pos_hint: {"x":0.2, "top":1}

    Button:
        text: "Register"
        on_release:
            app.root.current = "reg_page"
            size_hint: 0.6, 0.2
        pos_hint: {"x":0.2, "top":0.3}
<loginPopupShow>:
    Label:
        text: "\n\n\n\n\n\nA valid Email must be entered\nValid passwords include:\n at least one capital letter\n at least one number\n one lower cased letter\n and must be at least 6 characters long"
        size_hint: 0.6, 0.2
        pos_hint: {"x":0.2, "top":1}

<LoginWindow>:
    name: "login_page"

    email: email_id
    password: password_id

    Label: 
        text: "Email: "
        pos_hint: {"x": 0 , "top": 1}
        size_hint: 0.2, 0.1

    TextInput:
        id: email_id
        multiline:False
        pos_hint: {"x": 0.2, "top": 1}
        size_hint: 0.3, 0.1

    Label:
        text: "Password: "
        pos_hint: {"x": 0, "top": 0.9}
        size_hint: 0.2, 0.1


    TextInput:
        id: password_id
        multiline:False
        pos_hint: {"x": 0.2, "top": 0.9}
        size_hint: 0.3, 0.1

    Button:
        text: "Login"
        font_size: 50
        pos_hint: {"x": 0.5, "top": 1}
        size_hint: 0.3, 0.2
        on_release: 
            app.root.current = "main_page" if  root.check_input() else "login_page"
            root.manager.transition.direction = "left"

<MainWindow>:
    name: "main_page"

    Label:
        text: "Logged in - Welcome!"
        font_size: 14
        size_hint: 0.1, 0.1
        pos_hint: {"x": 0.2, "top": 1}

    Button:
        text: "<--- Logout <---" 
        pos_hint: {"x": 0, "top": 1}
        font_size: 12
        size_hint: 0.1, 0.1
        on_release:
            app.root.current = "login_page"
            root.manager.transition.direction = "right"


<RegPage>:
    name: "reg_page"

    Label:
        text: "Regestration Page"
        pos_hint: {"x": 0.5, "top": 1}
        font_size: 35
        size_hint: 0.2, 0.2

1 Ответ

0 голосов
/ 28 марта 2020

Вы можете добавить Button к своему правилу <loginPopupShow> в 'kv', которое отклонит Popup Примерно так:

<loginPopupShow>:
    Label:
        text: "\n\n\n\n\n\nA valid Email must be entered\nValid passwords include:\n at least one capital letter\n at least one number\n one lower cased letter\n and must be at least 6 characters long"
        size_hint: 0.6, 0.2
        pos_hint: {"x":0.2, "top":1}
    Button:
        text: 'Dismiss'
        size_hint: 0.4, 0.1
        pos_hint: {'center_x':0.5, 'y':0.2}
        on_press: root.popup.dismiss()

Это требует создания свойства popup для loginPopupShow класс:

class loginPopupShow(FloatLayout):
    popup = ObjectProperty(None)

и установка его для ссылки на содержащий Popup:

def invalidLoginPopup(self):

    show = loginPopupShow()

    popupWindow = Popup(title="INVALID EMAIL OR PASSWORD", content = show, size_hint=(None, None), size=(400,400))

    # set the popup property to reference the containing Popup
    show.popup = popupWindow

    popupWindow.open()
...