Search code examples
pythonkivykivymd

Can you say me what am i doing wrong in this code


(Sorry, but my english is weak. So please coprate)I am making a pokemon game for practice. I am new to kivy. I am making login screen and signup screen. I want to write code if username,emailid,password box are blank then make mdtextfield red means error. And show dialog box.but it is not working.means it is not giving error but whenever i run this code and i leave every box blank in sign up page and click on signup button. It is not showing dialog box and not making box red means error.

I want whenever i leave blank any box then show dialog box and make box red color means error.but it is not showing dialog box and not making box red color means error. Can you help me why my code is not running very well This is my py file

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.clock import Clock
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton

class Manager(ScreenManager):
    pass

class First(Screen):
    def __init__(self,**kwargs):
       super().__init__()
       Clock.schedule_once(self.My_CallBack, 1)
    def My_CallBack(self,dt):
        self.manager.current = 'login'

class Login (Screen):
    def New (self):
        self.manager.current = 'sign'

class Signup(Screen):
        def log (self):
            self.manager.current = 'login'


class Pokemon(MDApp):
    def build (self):
        self.strng  = Manager()
        return self.strng
    def check (self):
        emailid = self.strng.get_screen('sign').ids.emailid.text
        userid = self.strng.get_screen('sign').ids.signuse.text
        passid = self.strng.get_screen('sign').ids.passid.text
        def close_username_dialog(self,obj):
            self.dialog.dismiss()
        
        if emailid.split() == [] or passid.split() == [] or userid.split() == []:
            cncl = MDFlatButton(text = 'Retry',on_release = self.close_username_dialog)
            self.dialog = MDDialog(title = 'Invalid Input',text = 'Please Enter a valid Input',size_hint = (0.7,0.2),buttons = [cncl])
            self.dialog.open()
        
        
        if len(userid.split())>1:
            cncl = MDFlatButton(text = 'Retry',on_release = self.close_username_dialog)
            self.dialog = MDDialog(title = 'Invalid Username',text = 'Please enter username without space',size_hint = (0.7,0.2),buttons = [cncl])
            self.dialog.open()
        
if __name__ == '__main__':
    Builder.load_file('main.kv')
    Pokemon().run()

And this is kv file.

<Manager>:
    First:
    Login:
    Signup:
<First>:
    Screen:
        Image:
            source : "/storage/emulated/0/Pokemon : New Card/Assests/bgc.png"
            allow_stretch: True
        
        MDLabel:
            text : 'RD6RD'
            font_style : 'H4'
            font_size : '90sp'
            halign : 'center'
            theme_text_color : 'Error'
<Login>:
    name : 'login'
    
    Screen:
        Image:
            source : "/storage/emulated/0/Pokemon : New Card/Assests/bgc.png"
            allow_stretch: True
    
      
    MDTextField:
        hint_text: 'Enter Username'
        helper_text : 'Please enter username only in alphabets and less then 8 digit'
        max_text_length : 8
        multiline : False
        icon_left : 'account'
        pos_hint : {'center_x':.5,'center_y':.7}
        
    MDTextField:
        hint_text: 'Enter Password'
        multiline : False
        icon_left : 'lock-off'
        pos_hint : {'center_x':.5,'center_y':.6}
    MDRaisedButton:
     text: "          log in          "
     md_bg_color: 0, 0.7, 1, 1
        increment_width: "164dp"
        pos_hint:{'center_x':.5,'center_y':.5}
    MDTextButton:
        text : "if you haven't account sign up"
        pos_hint: {'center_x':.5,'center_y':.1}
        on_release : root.New()

        
<Signup>:
    name : 'sign'   
    Screen:
        Image:
            source : "/storage/emulated/0/Pokemon : New Card/Assests/bgc.png"
            allow_stretch: True
        MDTextField:
            id : signuse
            hint_text: 'Enter Username'
            helper_text : 'Please enter username unique and less then 8 digit'
            max_text_length : 8
            multiline : False
            icon_left : 'account'
            pos_hint : {'center_x':.5,'center_y':.7}
            
        
    MDTextField:
        id : signem
        hint_text: 'Enter email '
        multiline : False
        icon_left : 'gmail'
        pos_hint : {'center_x':.5,'center_y':.6}
    MDTextField:
        id : signpass
        hint_text: 'Enter Password'
        multiline : False
        icon_left : 'lock-off'
        pos_hint : {'center_x':.5,'center_y':.5}
    MDRaisedButton:
        id : ok
     text: "          sign up          "
     md_bg_color: 0, 0.7, 1, 1
        increment_width: "164dp"
        pos_hint:{'center_x':.5,'center_y':.4}
        on_press : app.check
    MDTextButton:
        text : "if you have already account login"
        pos_hint : {'center_x':.5,'center_y':.1}
        on_release : root.log()

Please help me why it is not working.


Solution

  • You are missing the parenthesis on the app.check() function call, you're retrieving the wrong IDs for the text inputs and the close_username_dialog() method needs to be in the Pokemon class.