简体   繁体   English

为什么我的 python kivy 登录表单不起作用?

[英]Why my python kivy login form doesn't work?

I create a Kivy login form using PostgreSQL.我使用 PostgreSQL 创建了一个 Kivy 登录表单。

    def login(self):
        # get email and password from TextInputs
        email = self.root.ids['login_screen'].ids['email']
        password = self.root.ids['login_screen'].ids['password'].text

        con = psycopg2.connect(
            host="localhost",
            database="abc",
            user="postgres",
            password="123")
        cur = con.cursor()
        #get password from db 
        sql = "SELECT password FROM regdata WHERE email=%s"
        val = [email.text]
        cur.execute(sql, val)
        rows = cur.fetchall()
        for row in rows:
            #tpassword is variable which helps to check password(if password = password we can sign in)
            tpassword = row[0]
            print(password)
            print(tpassword)
            if password == tpassword:
                self.change_screen('home_screen')

        con.commit()
        cur.close()
        con.close()

So, when I print variables password and password their values are equal, but I can't change screen.所以,当我打印变量密码和密码时,它们的值是相等的,但我不能改变屏幕。

Thanks for help!感谢帮助!

Doing this ScreenManager and Screen is much more efficient.这样做ScreenManagerScreen效率更高。

main.py主文件

from kivy.app import App 
from kivy.lang import Builder
from kivy.core.window import Window
import pyttsx3
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty, NumericProperty

Window.size = (800, 600) #setting screen size to 800x600

asset = Builder.load_file('styles.kv') #loading styles of widgets from 'styles.kv'

class ScreenOne(Screen): #making a screen
    pass

class Main(Screen): #making another screen
    pass

class Manager(ScreenManager): #making a manager to handler screens
    screen_one = ObjectProperty(None)

class MainApp(App): #making the main app
    def build(self):
        m = Manager(transition=NoTransition()) #setting the manager to manage screens for this app
        return m

    def login(self): #check function to check email and password
        email = self.root.ids.screen_one.ids['email']
        password = self.root.ids.screen_one.ids['password'].text

        con = psycopg2.connect(
            host="localhost",
            database="abc",
            user="postgres",
            password="123")
        cur = con.cursor()
        #get password from db 
        sql = "SELECT password FROM regdata WHERE email=%s"
        val = [email.text]
        cur.execute(sql, val)
        rows = cur.fetchall()
        for row in rows:
            #tpassword is variable which helps to check password(if password = password we can sign in)
            tpassword = row[0]
            print(password)
            print(tpassword)
            if password == tpassword:
                self.root.current = 'screen1'

        con.commit()
        cur.close()
        con.close()

MainApp().run() #running the app

styles.kv styles.kv

<Main>:
    FloatLayout:
        anchor_x: 'center'
        anchor_y: 'top'

        TextInput:
            id: email
            size_hint: (None, None)
            height: 60
            width: 250
            pos: 270, 400
            hint_text: 'Type your e-mail here'

        TextInput:
            id: password
            password: True
            size_hint: (None, None)
            height: 60
            width: 250
            pos: 270, 320
            hint_text: 'Type your password here'

        Button:
            id: submit
            size_hint: (None, None)
            size: 125, 50
            text: 'Submit'
            pos: 335, 250
            on_press: app.login()

<ScreenOne>:
    FloatLayout:
        anchor_x: 'center'
        anchor_y: 'top'

        TextInput:
            id: input
            size_hint: (None, None)
            hint_text: 'This is your home screen'
            height: 300
            width: 400
            pos: 200, 200

<Manager>:
    id: screen_manager
    screen_one: screen_one

    Main:
        id: screen_one
        name: "main"
        manager: screen_manager

    ScreenOne:
        id: screen_two
        name: "screen1"
        manager: screen_manager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM