Python Kivy Button和TextInput纹理在我与它们交互时出现故障

时间:2018-01-23 11:42:40

标签: python button kivy textinput

我创建了一个使用按钮和TextInputs的简单登录,注册和菜单屏幕。当我加载应用程序时,按钮和TextInputs具有正常纹理,但是当我与任何按钮或TextInputs交互时,应用程序中的所有按钮和TextInputs的纹理都会出现故障。

HandwritingRecognition.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
import DatabaseManagement

DatabaseManagement.Create()
USER = ''

class Login_Screen(Screen):
    def Login(self):
        global USER
        USER = self.ids.Username.text
        Found = DatabaseManagement.Find_User(self)
        if Found:
            print("Direct Entry")
            self.manager.current = "Login_Successful"
        else:
            print("Wrong Password")
            self.manager.current = "Login_Failed"
    def registration(self):
        self.manager.current = "Registration_Screen"

class Login_Successful(Screen):
    def ChangePassword(self):
         self.manager.current = "Change_Password"       
    def DeleteAccount(self):
        global USER
        DatabaseManagement.Delete_User(USER)
        self.manager.current = "Login_Screen"
    def MainScreen(self):
        self.manager.current = "Login_Screen"

class Change_Password(Screen):
    pass

class LoginConfirmationScreen(Screen):
    pass

class Registration_Screen(Screen):
    def MainScreen(self):
        DatabaseManagement.Data_Entry(self)
        self.manager.current = "Login_Screen"

class Login_Failed(Screen):
    def MainScreen(self):
        self.manager.current = "Login_Screen"

class RootWidget(ScreenManager):
    pass

class MainApp(App):
    def build(self):
        return RootWidget()

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

MainApp.kv

<RootWidget>:
    id: Main
    Login_Screen:
        id: login
        name: "Login_Screen"
    Change_Password:
        id: ChangePass
        name: 'Change_Password'
    Login_Failed:
        id: Failed
        name: "Login_Failed"
    Login_Successful:
        id: Success
        name: 'Login_Successful'
    Registration_Screen:
        id: register
        name: 'Registration_Screen'


<Login_Screen>:
    GridLayout:
        rows:3
        cols:2
        Label:
            text: "Username:"
            font_size: 20
        TextInput:
            id: Username
            multiline: False
            hint_text: 'Enter your Username'
        Label:
            text: "Password"
            font_size: 20
        TextInput:
            id: Passwrd
            multiline: False
            hint_text: 'Enter your Password'
            password: True
        Button:
            text: "Register"
            background_color: (1,0,0,1)
            on_press: root.registration()
        Button:
            text: "Sign In"
            on_press: root.Login()

<Registration_Screen>:
    GridLayout:
        rows:4
        cols:2
        Label:
            text: 'First Name:'
            font_size: 20
        TextInput:
            id: FirstName
            multiline: False
            hint_text: 'Enter your First Name'
        Label:
            text: 'Surname'
            font_size: 20
        TextInput:
            id: Surname
            multiline: False
            hint_text: 'Enter your Surname'
        Label:
            text: 'Password'
            font_size: 20
        TextInput:
            id: Passwrd
            multiline: False
            hint_text: 'Enter your Password'
            password: False
        Button:
            text: "Create Account"
            on_press: root.MainScreen()

<Change_Password>
    FloatLayout:
        Label:
            text: 'Change Password'
            pos_hint: {"center_x":0.5, "y":0.75}
            font_size: '25sp'
            size_hint: 0.5,0.25
        Label:
            text:'Old Password:'
            pos_hint: {"x":0.2, "y":0.6}
            font_size: '25sp'
            size_hint: 0.3,0.1           
        TextInput:
            pos_hint: {"x": 0.5,"y": 0.6}
            size_hint: 0.3,0.1
            id: OldPass
            multiline: False
            password: True



<Login_Successful>:
    FloatLayout:
        Label:
            text: 'Main Menu'
            pos_hint: {"center_x":0.5, "y":0.75}
            font_size: '25sp'
            size_hint: 0.5,0.25
        Button:
            text: 'Handwriting Recognition'
            pos_hint: {"center_x":0.5, "y":0.5}
            size_hint: 0.44,0.2
        Button:
            text: 'Change Password'
            pos_hint: {"center_x":0.38, "y":0.27}
            size_hint: 0.2,0.2
            on_press: root.ChangePassword()
        Button:
            text: 'Delete Account'
            pos_hint: {"center_x":0.38, "y":0.05}
            size_hint: 0.2,0.2
            on_press: root.DeleteAccount()
        Button:
            text: 'Log Out'
            pos_hint: {"center_x":0.62, "y":0.05}
            size_hint: 0.2,0.2
            on_press: root.MainScreen()

<Login_Failed>:
    BoxLayout:
        orientation: "vertical"
        Label:
            text: "Login Failed"
        Button:
            text: "Try again"
            on_press: root.MainScreen()

DatabaseManagement.py

import sqlite3
conn = sqlite3.connect('HandwritingRecognition.db')
c = conn.cursor()

def Create():
    c.execute('CREATE TABLE IF NOT EXISTS Users(FirstName TEXT, Surname TEXT, Username TEXT, Passwrd TEXT)')

def Data_Entry(self):
    FirstName = self.ids.FirstName.text
    Surname = self.ids.Surname.text
    Username = FirstName+Surname
    Passwrd = self.ids.Passwrd.text
    c.execute("INSERT INTO Users (FirstName,Surname,Username,Passwrd) VALUES(?,?,?,?)",(FirstName,Surname,Username,Passwrd))
    conn.commit()

def Find_User(self):
    finduser = ('SELECT * FROM Users WHERE Username = ? AND Passwrd = ?')
    c.execute(finduser,[(self.ids.Username.text),(self.ids.Passwrd.text)])
    data = c.fetchall()
    if data:
        return True
    else:
        return False

def Delete_User(USER):
    c.execute("DELETE FROM Users WHERE Username = ?",(USER,))
    conn.commit()

以下是在点击任何内容之前加载的应用程序的屏幕截图:

加载时的应用程序 https://i.stack.imgur.com/cJi27.png

以下是我与TextInput框交互后的屏幕截图: 交互后的应用 https://i.stack.imgur.com/HwZaz.png

我已将MainApp.kv编辑为仅使用FloatLayouts,但我仍然遇到同样的故障。

<RootWidget>:
    id: Main
    Login_Screen:
        id: login
        name: "Login_Screen"
    Change_Password:
        id: ChangePass
        name: 'Change_Password'
    Login_Failed:
        id: Failed
        name: "Login_Failed"
    Login_Successful:
        id: Success
        name: 'Login_Successful'
    Registration_Screen:
        id: register
        name: 'Registration_Screen'


<Login_Screen>:
    FloatLayout:
        Label:
            text: "Login"
            font_size: '30sp'
            pos_hint: {"center_x":0.5,"y":0.8}
            size_hint: 0.4,0.3
        Label:
            text: "Username:"
            font_size: '20sp'
            pos_hint: {"center_x": 0.25, "y":0.6}
            size_hint: 0.4, 0.25
        TextInput:
            id: Username
            multiline: False
            pos_hint: {"center_x": 0.75, "y":0.6}
            size_hint: 0.45, 0.15
            hint_text: 'Enter your Username'
        Label:
            text: "Password"
            font_size: '20sp'
            pos_hint: {"center_x": 0.25, "y":0.3}
            size_hint: 0.4, 0.15
        TextInput:
            id: Passwrd
            multiline: False
            pos_hint: {"center_x": 0.75, "y":0.3}
            size_hint: 0.45, 0.15
            hint_text: 'Enter your Password'
            password: True
        Button:
            text: "Register"
            pos_hint: {"center_x": 0.25, "y":0}
            size_hint: 0.4, 0.15
            #background_color: (1,0,0,1)
            on_press: root.registration()
        Button:
            pos_hint: {"center_x": 0.75, "y":0}
            size_hint: 0.4, 0.15
            text: "Sign In"
            on_press: root.Login()

<Registration_Screen>:
    FloatLayout:
        Label:
            text: 'Create an Account'
            font_size: '20sp'
            pos_hint: {"center_x": 0.5, "y": 0.8}
            size_hint: 0.4,0.2
        Label:
            text: 'First Name:'
            font_size: '20sp'
            pos_hint: {"center_x": 0.25, "y": 0.6}
            size_hint: 0.2, 0.15
        TextInput:
            id: FirstName
            multiline: False
            pos_hint: {"center_x": 0.6, "y": 0.6}
            size_hint: 0.4, 0.15
            hint_text: 'Enter your First Name'
        Label:
            text: 'Surname'
            font_size: '20sp'
            pos_hint: {"center_x": 0.25, "y": 0.45}
            size_hint: 0.2, 0.15
        TextInput:
            id: Surname
            multiline: False
            hint_text: 'Enter your Surname'
            pos_hint: {"center_x": 0.6, "y": 0.45}
            size_hint: 0.4, 0.15
        Label:
            text: 'Password'
            font_size: '20sp'
            pos_hint: {"center_x": 0.25, "y": 0.3}
            size_hint: 0.2, 0.15
        TextInput:
            id: Passwrd
            multiline: False
            pos_hint: {"center_x": 0.6, "y": 0.3}
            size_hint: 0.4, 0.15
            hint_text: 'Enter your Password'
            password: True
        Label:
            text: 'Confirm Password'
            font_size: '20sp'
            pos_hint: {"center_x": 0.25,"y": 0.15}
            size_hint: 0.2, 0.15
        TextInput:
            id: PasswrdConfirm
            multiline: False
            pos_hint: {"center_x": 0.6, "y": 0.15}
            size_hint: 0.4,0.15
            hint_text: "Please confirm password"
            password: True
        Button:
            text: "Create Account"
            pos_hint: {"center_x": 0.5, "y": 0}
            size_hint: 0.6, 0.15
            on_press: root.MainScreen()

<Change_Password>
    FloatLayout:
        Label:
            text: 'Change Password'
            pos_hint: {"center_x":0.5, "y":0.75}
            font_size: '25sp'
            size_hint: 0.5,0.25
        Label:
            text:'Old Password:'
            pos_hint: {"x":0.2, "y":0.6}
            font_size: '25sp'
            size_hint: 0.3,0.1           
        TextInput:
            pos_hint: {"x": 0.5,"y": 0.6}
            size_hint: 0.3,0.1
            id: OldPass
            multiline: False
            password: True



<Login_Successful>:
    FloatLayout:
        Label:
            text: 'Main Menu'
            pos_hint: {"center_x":0.5, "y":0.75}
            font_size: '25sp'
            size_hint: 0.5,0.25
        Button:
            text: 'Handwriting Recognition'
            pos_hint: {"center_x":0.5, "y":0.5}
            size_hint: 0.44,0.2
        Button:
            text: 'Change Password'
            pos_hint: {"center_x":0.38, "y":0.27}
            size_hint: 0.2,0.2
            on_press: root.ChangePassword()
        Button:
            text: 'Delete Account'
            pos_hint: {"center_x":0.38, "y":0.05}
            size_hint: 0.2,0.2
            on_press: root.DeleteAccount()
        Button:
            text: 'Log Out'
            pos_hint: {"center_x":0.62, "y":0.05}
            size_hint: 0.2,0.2
            on_press: root.MainScreen()

<Login_Failed>:
    FloatLayout:
        Label:
            text: "Login Failed"
            pos_hint: {"center_x": 0.5, "y": 0.75}
            size_hint: 0.5,0.25
        Button:
            text: "Try again"
            pos_hint: {"center_x":0.5, "y": 0.05}
            size_hint: 0.44, 0.2
            on_press: root.MainScreen()

1 个答案:

答案 0 :(得分:0)

Screen Widget是RelativeLayout SubClass。它往往会遇到很多问题。我建议首先将所有Screen子类化为FloatLayout:

<Login_Screen>:
    FloatLayout:
        GridLayout:
            rows:3
            cols:2

这通常可以解决屏幕带来的最不可预测性。