首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从另一个类库中的文本字段中获取文本

如何从另一个类库中的文本字段中获取文本
EN

Stack Overflow用户
提问于 2022-01-06 16:29:12
回答 1查看 50关注 0票数 0

我陷入了这个问题,我使用一个对话框以便用户可以更改他的密码,但是我不能从DashboardGerente类访问textfield文本.

main.py

代码语言:javascript
复制
class Content(BoxLayout):
    old_pass_tf = ObjectProperty()
    new_pass_tf = ObjectProperty()
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.app = MDApp.get_running_app()

    def show_passnew(self,widget):
        if widget.state == "normal":
            self.ids.new_pass.password = True
        else:
            self.ids.new_pass.password = False
    def show_passold(self,widget):
        if widget.state == "normal":
            self.ids.old_pass.password = True
        else:
            self.ids.old_pass.password = False

class DashboardGerente(Screen):
    dialog = None
    cont = Content()
    def Logout(self):
        wm = MDApp.get_running_app().root
        wm.current = "Login"
    def teste(self):
        print(Login.username)
    def show_alert_dialog(self):
        if not self.dialog:
            self.dialog = MDDialog(
                title = "Alterar palavra-passe",
                text = "Palavra-passe antiga",
                content_cls=Content(),
                type="custom",
                buttons=[
                    MDFlatButton(
                        text="Cancelar",
                        text_color=rgba(0,0,0,1),
                        on_release = self.cancelar
                    ),
                    MDFlatButton(
                        text="Alterar",
                        text_color = rgba(0,0,0,1),
                        on_release =  self.change_pass
                    ),
                ],
            )
        self.dialog.open()

    def cancelar(self,obj):
        self.dialog.dismiss()

    def change_pass(self, obj):
        #i want to access the textfield text here 
        pass

.kv文件

代码语言:javascript
复制
<Content>:
    orientation: "vertical"
    spacing: "12dp"
    size_hint_y: None
    height: "120dp"
    id: content_dialog_change_pass
    MDTextField:
        id: old_pass
        hint_text: "Palavra-passe antiga"
        mode: "rectangle"
        required: False
        color: 1, 0, 1, 1
        line_color_focus: 0.5, 0, 1, 1
        icon_right: "shield-key"
        size_hint: .9, None
        password: True

    GridLayout:
        cols:2
        MDCheckbox:
            on_press: root.show_passold(self)
            size_hint: None, None
            height: 5
            width: 20

        MDLabel:
            text: "Mostrar Palavra-passe"
            font_size: "13px"
            color: 0,0,0,.4

    MDTextField:
        id: new_pass
        hint_text: "Palavra-passe nova"
        mode: "rectangle"
        required: False
        color: 1, 0, 1, 1
        line_color_focus: 0.5, 0, 1, 1
        icon_right: "shield-key"
        size_hint: .9, None
        password: True

    GridLayout:
        cols:2
        MDCheckbox:
            on_press: root.show_passnew(self)
            size_hint: None, None
            height: 10
            width: 20

        MDLabel:
            text: "Mostrar Palavra-passe"
            font_size: "13px"
            color: 0,0,0,.4

<DashboardGerente@FloatLayout>:
    name: "DashboardGerente"
    MDBottomNavigation:
        panel_color: get_color_from_hex("#c300ff")
        text_color: 0,0,0,1
        MDBottomNavigationItem: ## Perfil do utilizador
            name: 'Profile'
            text: 'Perfil'
            icon: 'account'
            badge_icon: "numeric-10"
            MDCard:
                border_radius: 20
                radius: [15]
                size_hint:None, None
                size: 300, 400
                pos_hint: {"center_x": 0.5, "center_y": 0.5}
                elevation: 10
                orientation: "vertical"
                AnchorLayout:
                    anchor_x: "left"
                    anchor_y: "top"
                    MDIconButton:
                        id:logout
                        text_color: 0,0,0,1
                        on_release: root.Logout(), root.teste()
                        icon: "account-arrow-left"

                GridLayout:
                    cols: 2
                    padding: 40,0,0,0
                    MDLabel:
                        text: "ID: "
                    MDLabel:
                        text: "5000"
                    MDLabel:
                        text: "Nome: "
                    MDLabel:
                        text: "nome"
                    MDLabel:
                        text: "Cargo: "
                    MDLabel:
                        text: "cargo"
                    MDLabel:
                        text: "Salário: "
                    MDLabel:
                        text: "1000"
                    MDLabel:
                        text: "Secção: "
                    MDLabel:
                        text: "Frutaria"
                MDRaisedButton:
                    text: "Mudar palavra-passe"
                    md_bg_color: 0.5, 0, 1, 1
                    on_press: root.teste()
                    pos_hint: {"center_x": 0.5, "center_y": 0.5}
                    on_release: root.show_alert_dialog()

        MDBottomNavigationItem:
            name: 'Providers'
            text: 'Fornecedores'
            icon: 'truck'
            MDLabel:
                text: 'Mail'
                halign: 'center'

        MDBottomNavigationItem:
            name: 'Products'
            text: 'Produtos'
            icon: 'shopping'
            badge_icon: "numeric-10"
            MDLabel:
                text: 'Mail'
                halign: 'center'

        MDBottomNavigationItem:
            name: 'Employes'
            text: 'Empregados'
            icon: 'account-multiple'
            badge_icon: "numeric-10"
            MDRaisedButton:
                text: "Adicionar funcionários"
                md_bg_color: 0.5, 0, 1, 1
                pos_hint: {"center_x": 0.5, "center_y": 0.5}

        MDBottomNavigationItem:
            name: 'Sections'
            text: 'Secções'
            icon: 'storefront'
            badge_icon: "numeric-10"
            MDLabel:
                text: 'Mail'
                halign: 'center'

这是我的项目文件,现在重要的是,如何从.text类中的old_pass textfield中获取DashboardGerente值?

EN

回答 1

Stack Overflow用户

发布于 2022-01-06 18:00:43

我建议如下,

在您的user_input = StringProperty("")子类中创建一个属性,比方说,在Content类的kv中,

代码语言:javascript
复制
MDTextField:
    id: old_pass
    on_text: app.user_input = self.text

现在,在DashboardGerente的任何方法中使用它作为MDApp.get_running_app().user_input

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70610335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档