2017-02-21 2 views
0

사용자 이름과 비밀번호를 허용하는 로그인 페이지를 만들고 싶습니다. 두 가지 모두를 확인한 후 "로그인 성공"메시지를 보여주는 팝업 메시지가 표시되거나 "로그인 실패"라는 팝업 메시지가 표시됩니다. 나는 그것을 시도했다. 그러나 나는 잘못 갔는지 모른다. 어떤 도움이라도 대단히 감사 할 것입니다. 내 코드는 다음과 같이 (Login.py로 저장)입니다 : 당신이 sign_in 버튼을 눌러에 대한 검증을하기 때문에 모든Kivy 로그인 화면과 팝업 오류

from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.textinput import TextInput 
from kivy.lang import Builder 
from kivy.uix.widget import Widget 
from kivy.uix.popup import Popup 

Builder.load_string(""" 

<[email protected]>: 
canvas: 

    Rectangle: 
     source: 'b.png' 
     pos: self.pos 
     size: self.size 
Label: 
    text: "PLEASE LOGIN OR SIGN UP" 
    center_x: (root.width/2) 
    top: (root.top/2)+ 200 
    font_size: 25 

TextInput: 
    id: txtuname 
    center_x: (root.width/2) 
    top: (root.top/2)+ 100 
    size_hint: None,None 
    multiline: False 
    hint_text: "username" 
    size: 250, 40 
    max_lines: 1 
    valign: 'middle' 
    halign: 'center' 
    on_text_validate: root.validate(); 

TextInput: 
    id: txtpswd 
    multiline: False 
    center_x: (root.width/2) 
    top: (root.top/2)+ 50 
    size_hint: None,None 
    hint_text: "password" 
    size: 250, 40 
    max_lines: 1 
    valign: 'middle' 
    halign: 'center' 
    on_text_validate: root.validate(); 
    password: True 

Button: 
    id: btnlogin 
    size: 90,35 
    pos: 300, 250 
    font_size: 18 
    background_color: (1,1,1,0.1) 
    text: "Login" 
    on_press: root.validate(txtuname.text,txtpswd.text) 

Button: 
    text: "Sign Up" 
    size: 90,35 
    pos: 400, 250 
    font_size: 18 
    background_color: (1,1,1,0.1) 

<CustomPopup>: 
Button: 
    id: btnpopup 
    text: "Login successfull" 
    size_hint: .5, .5 
    on_press: root.dismiss() 
""") 

class LoginScreen(Widget): 
    def validate(self,txtuname,txtpswd): 
     if txtuname == "username" and txtpswd == "password": 
      print(txtuname,txtpswd) 
      b = Button(on_press=self.show_popup) 
      return b 
     else: 
      print("Login failed") 

class CustomPopup(Popup): 
    def show_popup(self, b): 
     p = CustomPopup() 
     p.open() 

class LoginApp(App): 
    def build(self): 
     return LoginScreen() 


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

답변

1

먼저 두 TextInput의의 on_text_validate: root.validate();를 제거합니다. 당신은 팝업으로이 같은 클래스를 만들 수 있습니다 : 다음

from kivy.uix.anchorlayout import AnchorLayout 
from kivy.uix.popup import Popup  
from kivy.uix.label import Label 
from kivy.uix.button import Button 
from kivy.core.window import Window 


class Alert(Popup): 

    def __init__(self, title, text): 
     super(Alert, self).__init__() 
     content = AnchorLayout(anchor_x='center', anchor_y='bottom') 
     content.add_widget(
      Label(text=text, halign='left', valign='top') 
     ) 
     ok_button = Button(text='Ok', size_hint=(None, None), size=(Window.width/5, Window.height/5)) 
     content.add_widget(ok_button) 

     popup = Popup(
      title=title, 
      content=content, 
      size_hint=(None, None), 
      size=(Window.width/3, Window.height/3), 
      auto_dismiss=True, 
     ) 
     ok_button.bind(on_press=popup.dismiss) 
     popup.open() 

당신의 validate() 방법 나는 kivy에 새로 온 사람

def validate(self,txtuname,txtpswd): 
    # if inputs are valid: 
     Alert(title='yeah!', text='inputs are valid') 
    # else: 
     Alert(title='oops!', text='invalid inputs') 
+0

작동합니다! 고마워요 :) – AS15

0

에. 내 코딩이 도움이 될 것 같아요.

def check_user(self): 
    obj=CustomPopup() 
    if self.ids.unam.text=='' or self.ids.pas.text=='': 
     obj.call_pops('Fill up please!','Some fields are empty!') 
    else: 
     if self.ids.unam.text=='sri' and self.ids.pas.text=='sri': 
      self.manager.current='Feed' 
      t="Success" 
      c="Logged in successfully!" 
      obj.call_pops(t,c) 
      self.ids.unam.text='' 
      self.ids.pas.text='' 
     else: 
      print "Warning" 
      t="Warning" 
      c="Wrong User or Password!" 
      obj.call_pops(t,c) 


class CustomPopup(Popup): 
    def call_pops(self,tit,conten): 
     cont=Button(text=conten) 
     pop=Popup(title=tit,content=cont,size_hint=(None, None), size=(250, 150),auto_dismiss=True) 
     pop.open() 
     cont.bind(on_press=pop.dismiss)