【问题标题】:How do you make a button dismiss popup kivy你如何让按钮关闭弹出窗口kivy
【发布时间】:2020-03-28 21:55:21
【问题描述】:

我知道这个问题被问了很多,但我似乎无法理解其他人的答案。如果有人可以帮助制作 not_in_db_pop() 上的按钮以关闭弹出窗口,我将非常高兴。不要在它们来自自定义类时使用了一些奇怪的方法。我将同时发布 python 代码和 kivy:

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()

基维:

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

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    您可以在“kv”中将Button 添加到您的&lt;loginPopupShow&gt; 规则中,这将消除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()
    

    这需要为loginPopupShow 类创建一个popup 属性:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      相关资源
      最近更新 更多