如何从kivy中的主文件访问弹出按钮并根据情况更改其文本

how to access popup button from main file in kivy and change its text according to situation

在我的登录系统中,我想创建一个弹出框,当用户名、密码或验证密码错误时显示该弹出框。谁能帮我在每次发生错误时如何使弹出窗口显示不同的标签

这是主文件的代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

class LoginWindow(Screen):
    def signIn(self):
        pass

    def signUp(self):
        # db=open('QuizData.txt','r')
        username=self.ids.uname.text
        password=self.ids.passw.text
        repassword=self.ids.repassw.text
        if password!=repassword:
           pass      #i want a popup box to appear if this is true
    
    def popup(self):
                pass
class QuizWindow(Screen):
    pass

class QuizScreenManager(ScreenManager):
    pass

kv = Builder.load_file('loginLayoutCSS.kv')

class Login(App):
    def build(self):
        return kv

if __name__=='__main__':
    Login().run()

这里是 .kv 文件的代码

#:kivy 2.1.0
#:import utils kivy.utils
#:import Factory kivy.factory.Factory

<MyPopup@Popup>
    auto_dismiss: True
    size_hint:0.6,0.3
    pos_hint:{'center_x':0.5}
    title: 'Error'
    Button:
        text:'close'
        fonr_size: 20
        on_release: root.dismiss()
<MyTextInput@TextInput>
    font_size:25
    # size_
    background_normal:''
    background_color: utils.get_color_from_hex('#8c8c8c')

QuizScreenManager:
    LoginWindow:
    QuizWindow:

<LoginWindow>:
    name:'login'

    BoxLayout:
        orientation:'vertical'
        size: root.width,root.height
        spacing: 20
        padding: 20

        Label:
            id:heading
            text:'Welcome back, sign in'
            font_size:32

        MyTextInput:
            id:uname
            text:'username'

        MyTextInput:
            id:passw
            text:'password'

        MyTextInput:
            id:repassw
            text:'re-enter Password'
            multiline:False
            size_hint_y: None
            height:0
            opacity:0

        Button:
            text:'Sign In'
            id:signin
            size_hint_y:0.6
            on_release: 
                app.root.current='quizwindow'
                root.manager.transition.direction='up'

        Button:
            text:'New here? Sign up Now! click here'
            id:signup
            on_press:
                # signin.size_hint_y = None
                repassw.size_hint_y = 0.6
                repassw.opacity = 1
                repassw.background_color = utils.get_color_from_hex('#8c8c8c')
                root.signUp()
                root.popup()
                signup.text = 'Sign Up'
                signin.opacity = 0
                

<QuizWindow>:
    name:'quizwindow'

    Button:
        text:'go back'
        on_release: 
            app.root.current='login'
            root.manager.transition.direction='down'

您可以将 id 添加到 MyPopup class 以允许您访问包含您的消息的 Label。这是 kv 文件那部分的修改版本:

<MyPopup@Popup>
    auto_dismiss: True
    size_hint:0.6,0.3
    pos_hint:{'center_x':0.5}
    title: 'Error'
    BoxLayout:
        orientation: 'vertical'
        Label:
            id: message
        Button:
            text:'close'
            fonr_size: 20
            size_hint: None, None
            size: self.texture_size
            on_release: root.dismiss()

然后您可以像这样在 python 代码中访问 Label

def signUp(self):
    # db=open('QuizData.txt','r')
    username = self.ids.uname.text
    password = self.ids.passw.text
    repassword = self.ids.repassw.text
    if password != repassword:
        popup = Factory.MyPopup()
        popup.ids.message.text = 'passwords do not match'
        popup.open()