在KivyMD上的小部件之间添加数值

时间:2020-10-02 16:15:25

标签: python widget kivy add kivymd

我正在构建一个用作差旅费用管理器的应用程序。我目前正在尝试构建一个系统,在其中将请求的金额插入MDTextField并按一个按钮以确认请求。同时,在其他小部件上,我希望将请求的总金额加起来。 enter image description here

即如果我在TextField上写入1000,然后单击“ Ingresar Gasto”按钮,则标签“ MONTO TOTAL SOLICITADO”下方的值应更新,并且目标MDTextField和带有原始编号的MDTextField的格式均应正确设置为$ 1,000.00。 >

对于所有具有相同小部件的扩展面板,同样需要保持正确。

我尝试了以下操作:

扩展面板的基辅代码:

<MyContentAliment>:
    adaptive_height: True
    MDBoxLayout:
        orientation:'horizontal'
        adaptive_height:True
        size_hint_x:self.width
        pos_hint: {"center_x":0.5, "center_y":0.5}
        spacing: dp(10)
        padding_horizontal: dp(10)
        MDLabel:
            text: 'Monto:'
            multiline: 'True'
            halign: 'center'
            pos_hint: {"x":0, "top":0.5}
            size_hint_x: 0.15
            font_style: 'Button'
            font_size: 19

        MDTextField:
            id: monto_aliment_viaje
            hint_text: 'Monto a solicitar'
            pos_hint: {"x":0, "top":0.5}
            halign: 'left'
            size_hint_x: 0.3
            helper_text: 'Ingresar el monto a solicitar'
            helper_text_mode: 'on_focus'
            write_tab: False
            required: True
            on_text:
                root.limit_currency()

        MDRaisedButton:
            id: boton_aliment_viaje
            pos_hint: {"x":0, "top":0.5}
            text:'Ingresar Gasto'
            on_release:
                root.sumar_gasto()

MDCard的基辅代码(包含目标小部件):

<TravelManagerWindow>:
    BoxLayout:
        size_hint:1,0.85
        pos_hint: {"center_x": 0.5, "center_y":0.37}
        adaptive_height:True
        height: self.minimum_height

        ScrollView:
            adaptive_height:True

            GridLayout:
                id: container
                size_hint_y: None
                cols: 1
                row_default_height: root.height*0.10
                height: self.minimum_height 

                MDBoxLayout:
                    adaptive_height: True
                    orientation: 'horizontal'
                    GridLayout:
                        id: panel_container
                        size_hint_x: 0.6
                        cols: 1
                        adaptive_height: True
            
                    MDBoxLayout:
                        size_hint_x: 0.05       
                    MDCard:
                        id: resumen_solicitud
                        size_hint: None, None
                        size: "250dp", "300dp"
                        pos_hint: {"top": 0.9, "center_x": .5}
                        elevation: 0.1

                        MDBoxLayout:
                            orientation: 'vertical'
                            canvas.before:
                                Color:
                                    rgba: 0.8, 0.8, 0.8, 1
                                Rectangle:
                                    pos: self.pos
                                    size: self.size
                            MDLabel:
                                text: 'Monto Total Solicitado'
                                font_style: 'Button'
                                halign: 'center'
                                font_size: (root.width**2 + root.height**2) / 15.5**4
                                size_hint_y: 0.2
                            MDSeparator:
                                height: "1dp"
                            MDTextField:
                                id: suma_solic_viaje
                                text: "$ 0.00"
                                bold: True
                                line_color_normal: app.theme_cls.primary_color
                                halign: "center"
                                size_hint_x: 0.8

最后但并非最不重要的Python代码:

class TravelManagerWindow(Screen):
    viajeInicio = ObjectProperty(None)
    panel_container = ObjectProperty(None)
    travel_list = ObjectProperty(None)
    DateMDTextField = ObjectProperty(None)
    menu = ObjectProperty()

    # EXPANSION PANEL PARA SOLICITAR GV
    def set_expansion_panel(self):
        #FOOD PANEL
        self.ids.panel_container.add_widget(MDExpansionPanel(icon="food.png", 
                                     content=MyContentAliment(),
                                     panel_cls=MDExpansionPanelOneLine(
                                                 text="Alimentacion")))
        # CASETAS PANEL
        self.ids.panel_container.add_widget(MDExpansionPanel(
                               icon="casetas.png", 
                                     content=MyContentCasetas(),
                                           panel_cls=MDExpansionPanelOneLine(
                                                         text="Casetas")))
        # GAS PANEL
        self.ids.panel_container.add_widget(MDExpansionPanel(icon="gas.png", 
                          content=MyContentGasolina(),
                                panel_cls=MDExpansionPanelOneLine(
                                                         text="Gasolina")))
        # HOSPEDAJE PANEL
        self.ids.panel_container.add_widget(MDExpansionPanel(
                 icon="hospedaje.png", content=MyContentHosped(),                                                         
                                 panel_cls=MDExpansionPanelOneLine(
                                                         text="Hospedaje")))
        # VARIOS PANEL
        self.ids.panel_container.add_widget(MDExpansionPanel(
                icon="varios.png", content=MyContentVarios(),                                                  
                        panel_cls=MDExpansionPanelOneLine(
                                                         text="Varios")))

所有面板容器都具有相同的代码,仅针对不同的 小工具(未添加简单性)

class MyContentAliment(BoxLayout):
    def apply_currency_format(self):
        # if len <= 3
        if len(self.ids.monto_aliment_viaje.text) <= 3 and 
                (self.ids.monto_aliment_viaje.text).isnumeric():
            self.ids.monto_aliment_viaje.text = "$" + self.ids.monto_aliment_viaje.text + '.00'
        # n,nnn
        elif len(self.ids.monto_aliment_viaje.text) == 4 and 
                (self.ids.monto_aliment_viaje.text).isnumeric():
            self.ids.monto_aliment_viaje.text = "$" + self.ids.monto_aliment_viaje.text[0] + "," + \
                                            self.ids.monto_aliment_viaje.text[1:] + '.00'
        # nn,nnn
        elif len(self.ids.monto_aliment_viaje.text) == 5 and 
                (self.ids.monto_aliment_viaje.text).isnumeric():
            self.ids.monto_aliment_viaje.text = "$" + self.ids.monto_aliment_viaje.text[:2] + "," + \
                                            self.ids.monto_aliment_viaje.text[2:] + '.00'

    def limit_currency(self):
        if len(self.ids.monto_aliment_viaje.text) > 5 and 
                self.ids.monto_aliment_viaje.text.startswith('$') == False:
            self.ids.monto_aliment_viaje.text = self.ids.monto_aliment_viaje.text[:-1]

    def sumar_gasto(self):
        if self.ids.monto_aliment_viaje.text == "":
            pass
        else:
            travel_manager = TravelManagerWindow()
            monto_total = float(travel_manager.ids.suma_solic_viaje.text[2:])
            monto_total += float(self.ids.monto_aliment_viaje.text)
            travel_manager.ids.suma_solic_viaje.text= "$ " + str(monto_total)
            self.apply_currency_format()

### WINDOW MANAGER ################################

class WindowManager(ScreenManager):
    pass

ScreenManager().add_widget(LoginWindow(name='login'))
ScreenManager().add_widget(CreateAccountWindow(name='create'))
ScreenManager().add_widget(MainWindow(name='main'))
ScreenManager().add_widget(IngActivWindow(name='ingActiv'))
ScreenManager().add_widget(CronogramaWindow(name='cronograma'))
ScreenManager().add_widget(TravelManagerWindow(name='travelManager'))
ScreenManager().add_widget(SoporteTecnicoWindow(name='soporteTecnico'))

class powerApp2(MDApp):
    pass

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

由于apply_currency_format函数,当前我获得了正确的格式。但是,当我按下按钮时,目标文本字段的值保持不变。

非常感谢。

最小可复制示例的代码:

Python代码:

from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelOneLine
from kivy.uix.boxlayout import BoxLayout


class MyContentAliment(BoxLayout):
    def apply_currency_format(self):
        # if len <= 3
        if len(self.ids.monto_aliment_viaje.text) <= 3 and self.ids.monto_aliment_viaje.text.isnumeric():
            self.ids.monto_aliment_viaje.text = "$" + self.ids.monto_aliment_viaje.text + '.00'
        # n,nnn
        elif len(self.ids.monto_aliment_viaje.text) == 4 and self.ids.monto_aliment_viaje.text.isnumeric():
            self.ids.monto_aliment_viaje.text = "$" + self.ids.monto_aliment_viaje.text[0] + "," + \
                                        self.ids.monto_aliment_viaje.text[1:] + '.00'
        # nn,nnn
        elif len(self.ids.monto_aliment_viaje.text) == 5 and self.ids.monto_aliment_viaje.text.isnumeric():
            self.ids.monto_aliment_viaje.text = "$" + self.ids.monto_aliment_viaje.text[:2] + "," + \
                                        self.ids.monto_aliment_viaje.text[2:] + '.00'

    def limit_currency(self):
        if len(self.ids.monto_aliment_viaje.text) > 5 and self.ids.monto_aliment_viaje.text.startswith('$') == False:
            self.ids.monto_aliment_viaje.text = self.ids.monto_aliment_viaje.text[:-1]

    def sumar_gasto(self):
        if self.ids.monto_aliment_viaje.text == "":
            pass
        else:
            travel_manager = TravelManagerWindow()
            monto_total = float(travel_manager.ids.suma_solic_viaje.text[2:])
            monto_total += float(self.ids.monto_aliment_viaje.text)
            travel_manager.ids.suma_solic_viaje.text = "$ " + str(monto_total)
            self.apply_currency_format()


class MyContentCasetas(BoxLayout):
    def apply_currency_format(self):
        # if len <= 3
        if len(self.ids.monto_casetas_viaje.text) <= 3 and self.ids.monto_casetas_viaje.text.isnumeric():
            self.ids.monto_casetas_viaje.text = "$" + self.ids.monto_casetas_viaje.text + '.00'
        # n,nnn
        elif len(self.ids.monto_casetas_viaje.text) == 4 and self.ids.monto_casetas_viaje.text.isnumeric():
            self.ids.monto_casetas_viaje.text = "$" + self.ids.monto_casetas_viaje.text[0] + "," + \
                                            self.ids.monto_casetas_viaje.text[1:] + '.00'
        # nn,nnn
        elif len(self.ids.monto_casetas_viaje.text) == 5 and self.ids.monto_casetas_viaje.text.isnumeric():
            self.ids.monto_casetas_viaje.text = "$" + self.ids.monto_casetas_viaje.text[:2] + "," + \
                                            self.ids.monto_casetas_viaje.text[2:] + '.00'

    def limit_currency(self):
        if len(self.ids.monto_casetas_viaje.text) > 5 and self.ids.monto_casetas_viaje.text.startswith('$') == False:
            self.ids.monto_casetas_viaje.text = self.ids.monto_casetas_viaje.text[:-1]


class MyContentGasolina(BoxLayout):
    def apply_currency_format(self):
        # if len <= 3
        if len(self.ids.monto_gas_viaje.text) <= 3 and self.ids.monto_gas_viaje.text.isnumeric():
            self.ids.monto_gas_viaje.text = "$" + self.ids.monto_gas_viaje.text + '.00'
        # n,nnn
        elif len(self.ids.monto_gas_viaje.text) == 4 and self.ids.monto_gas_viaje.text.isnumeric():
            self.ids.monto_gas_viaje.text = "$" + self.ids.monto_gas_viaje.text[0] + "," + \
                                        self.ids.monto_gas_viaje.text[1:] + '.00'
        # nn,nnn
        elif len(self.ids.monto_gas_viaje.text) == 5 and self.ids.monto_gas_viaje.text.isnumeric():
            self.ids.monto_gas_viaje.text = "$" + self.ids.monto_gas_viaje.text[:2] + "," + \
                                        self.ids.monto_gas_viaje.text[2:] + '.00'

    def limit_currency(self):
        if len(self.ids.monto_gas_viaje.text) > 5 and self.ids.monto_gas_viaje.text.startswith('$') == False:
            self.ids.monto_gas_viaje.text = self.ids.monto_gas_viaje.text[:-1]


class MyContentHosped(BoxLayout):
    def apply_currency_format(self):
        # if len <= 3
        if len(self.ids.monto_hosped_viaje.text) <= 3 and self.ids.monto_hosped_viaje.text.isnumeric():
            self.ids.monto_hosped_viaje.text = "$" + self.ids.monto_hosped_viaje.text + '.00'
        # n,nnn
        elif len(self.ids.monto_hosped_viaje.text) == 4 and self.ids.monto_hosped_viaje.text.isnumeric():
            self.ids.monto_hosped_viaje.text = "$" + self.ids.monto_hosped_viaje.text[0] + "," + \
                                           self.ids.monto_hosped_viaje.text[1:] + '.00'
        # nn,nnn
        elif len(self.ids.monto_hosped_viaje.text) == 5 and self.ids.monto_hosped_viaje.text.isnumeric():
            self.ids.monto_hosped_viaje.text = "$" + self.ids.monto_hosped_viaje.text[:2] + "," + \
                                           self.ids.monto_hosped_viaje.text[2:] + '.00'

    def limit_currency(self):
        if len(self.ids.monto_hosped_viaje.text) > 5 and self.ids.monto_hosped_viaje.text.startswith('$') == False:
            self.ids.monto_hosped_viaje.text = self.ids.monto_hosped_viaje.text[:-1]


class MyContentVarios(BoxLayout):
    def apply_currency_format(self):
        # if len <= 3
        if len(self.ids.monto_varios_viaje.text) <= 3 and self.ids.monto_varios_viaje.text.isnumeric():
            self.ids.monto_varios_viaje.text = "$" + self.ids.monto_varios_viaje.text + '.00'
        # n,nnn
        elif len(self.ids.monto_varios_viaje.text) == 4 and self.ids.monto_varios_viaje.text.isnumeric():
            self.ids.monto_varios_viaje.text = "$" + self.ids.monto_varios_viaje.text[0] + "," + \
                                           self.ids.monto_varios_viaje.text[1:] + '.00'
        # nn,nnn
        elif len(self.ids.monto_varios_viaje.text) == 5 and self.ids.monto_varios_viaje.text.isnumeric():
            self.ids.monto_varios_viaje.text = "$" + self.ids.monto_varios_viaje.text[:2] + "," + \
                                           self.ids.monto_varios_viaje.text[2:] + '.00'

    def limit_currency(self):
        if len(self.ids.monto_varios_viaje.text) > 5 and self.ids.monto_varios_viaje.text.startswith('$') == False:
            self.ids.monto_varios_viaje.text = self.ids.monto_varios_viaje.text[:-1]


class LoginWindow(Screen):
    pass

class TravelManagerWindow(Screen):
    panel_container = ObjectProperty(None)

    # EXPANSION PANEL PARA SOLICITAR GV
    def set_expansion_panel(self):
        # FOOD PANEL
        self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentAliment(),
                                                         panel_cls=MDExpansionPanelOneLine(text="Alimentacion")))
        # CASETAS PANEL
        self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentCasetas(),
                                                         panel_cls=MDExpansionPanelOneLine(text="Casetas")))
        # GAS PANEL
        self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentGasolina(),
                                                         panel_cls=MDExpansionPanelOneLine(text="Gasolina")))
        # HOSPEDAJE PANEL
        self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentHosped(),
                                                         panel_cls=MDExpansionPanelOneLine(text="Hospedaje")))
        # VARIOS PANEL
        self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentVarios(),
                                                         panel_cls=MDExpansionPanelOneLine(text="Varios")))

### WINDOW MANAGER ################################


class WindowManager(ScreenManager):
    pass

ScreenManager().add_widget(LoginWindow(name='login'))
ScreenManager().add_widget(TravelManagerWindow(name='travelManager'))


class reprodExample(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Teal"
        return WindowManager()


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

KV文件的代码:

<WindowManager>:
    LoginWindow:
    TravelManagerWindow:

<LoginWindow>:
    MDRaisedButton:
        text: 'Enter'
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}
        size_hint: None, None
        on_release:
            root.manager.transition.direction = 'up'
            root.manager.current = 'travelManager'

<TravelManagerWindow>:
    name:'travelManager'
    on_pre_enter: root.set_expansion_panel()

    BoxLayout:
        orientation: 'vertical'
        size_hint:1,0.85
        pos_hint: {"center_x": 0.5, "center_y":0.37}
        adaptive_height:True
        height: self.minimum_height

        ScrollView:
            adaptive_height:True

            GridLayout:
                size_hint_y: None
                cols: 1
                row_default_height: root.height*0.10
                height: self.minimum_height

                BoxLayout:
                    adaptive_height: True
                    orientation: 'horizontal'

                    GridLayout:
                        id: panel_container
                        size_hint_x: 0.6
                        cols: 1
                        adaptive_height: True

                    BoxLayout:
                        size_hint_x: 0.05
                    MDCard:
                        id: resumen_solicitud
                        size_hint: None, None
                        size: "250dp", "300dp"
                        pos_hint: {"top": 0.9, "center_x": .5}
                        elevation: 0.1

                        MDBoxLayout:
                            orientation: 'vertical'
                            canvas.before:
                                Color:
                                    rgba: 0.8, 0.8, 0.8, 1
                                Rectangle:
                                    pos: self.pos
                                    size: self.size
                            MDLabel:
                                text: 'Monto Total Solicitado'
                                font_style: 'Button'
                                halign: 'center'
                                font_size: (root.width**2 + root.height**2) / 15.5**4
                                size_hint_y: 0.2
                            MDSeparator:
                                height: "1dp"
                            MDTextField:
                                id: suma_solic_viaje
                                text: "$ 0.00"
                                bold: True
                                line_color_normal: app.theme_cls.primary_color
                                halign: "center"
                                size_hint_x: 0.8
                                pos_hint: {'center_x': 0.5, 'center_y': 0.5}
                            MDSeparator:
                                height: "1dp"
                            BoxLayout:
                                id: expense_graph
                                halign: 'center'

<MyContentAliment>:
    adaptive_height: True
    MDBoxLayout:
        orientation:'horizontal'
        adaptive_height:True
        size_hint_x:self.width
        pos_hint: {"center_x":0.5, "center_y":0.5}
        spacing: dp(10)
        padding_horizontal: dp(10)
        MDLabel:
            text: 'Monto:'
            multiline: 'True'
            halign: 'center'
            pos_hint: {"x":0, "top":0.5}
            size_hint_x: 0.15
            font_style: 'Button'
            font_size: 19

        MDTextField:
            id: monto_aliment_viaje
            hint_text: 'Monto a solicitar'
            pos_hint: {"x":0, "top":0.5}
            halign: 'left'
            size_hint_x: 0.3
            helper_text: 'Ingresar el monto a solicitar'
            helper_text_mode: 'on_focus'
            write_tab: False
            required: True
            on_text:
                root.limit_currency()

        MDRaisedButton:
            id: boton_aliment_viaje
            pos_hint: {"x":0, "top":0.5}
            text:'Ingresar Gasto'
            on_release:
                root.sumar_gasto()

### CASETAS
<MyContentCasetas>:
    adaptive_height: True
    MDBoxLayout:
        orientation:'horizontal'
        adaptive_height:True
        size_hint_x:self.width
        pos_hint: {"center_x":0.5, "center_y":0.5}
        spacing: dp(10)
        padding_horizontal: dp(10)
        MDLabel:
            text: 'Monto:'
            multiline: 'True'
            halign: 'center'
            pos_hint: {"x":0, "top":0.5}
            size_hint_x: 0.15
            font_style: 'Button'
            font_size: 19

        MDTextField:
            id: monto_casetas_viaje
            hint_text: 'Monto a solicitar'
            pos_hint: {"x":0, "top":0.5}
            halign: 'left'
            size_hint_x: 0.3
            helper_text: 'Ingresar el monto a solicitar'
            helper_text_mode: 'on_focus'
            write_tab: False
            #input_filter: 'float'
            required: True
            on_text:
                root.limit_currency()

        MDRaisedButton:
            id: boton_casetas_viaje
            pos_hint: {"x":0, "top":0.5}
            text:'Ingresar Gasto'
            on_release:
                root.apply_currency_format()

        BoxLayout:
            size_hint_x: 0.05

### GASOLINA
<MyContentGasolina>:
    adaptive_height: True
    MDBoxLayout:
        orientation:'horizontal'
        adaptive_height:True
        size_hint_x:self.width
        pos_hint: {"center_x":0.5, "center_y":0.5}
        spacing: dp(10)
        padding_horizontal: dp(10)
        MDLabel:
            text: 'Monto:'
            multiline: 'True'
            halign: 'center'
            pos_hint: {"x":0, "top":0.5}
            size_hint_x: 0.15
            font_style: 'Button'
            font_size: 19

        MDTextField:
            id: monto_gas_viaje
            hint_text: 'Monto a solicitar'
            pos_hint: {"x":0, "top":0.5}
            halign: 'left'
            size_hint_x: 0.3
            helper_text: 'Ingresar el monto a solicitar'
            helper_text_mode: 'on_focus'
            write_tab: False
            #input_filter: 'float'
            required: True
            on_text:
                root.limit_currency()

        MDRaisedButton:
            id: boton_gas_viaje
            pos_hint: {"x":0, "top":0.5}
            text:'Ingresar Gasto'
            on_release:
                root.apply_currency_format()

        BoxLayout:
            size_hint_x: 0.05

 ### HOSPEDAJE
<MyContentHosped>:
    adaptive_height: True
    MDBoxLayout:
        orientation:'horizontal'
        adaptive_height:True
        size_hint_x:self.width
        pos_hint: {"center_x":0.5, "center_y":0.5}
        spacing: dp(10)
        padding_horizontal: dp(10)
        MDLabel:
            text: 'Monto:'
            multiline: 'True'
            halign: 'center'
            pos_hint: {"x":0, "top":0.5}
            size_hint_x: 0.15
            font_style: 'Button'
            font_size: 19

        MDTextField:
            id: monto_hosped_viaje
            hint_text: 'Monto a solicitar'
            pos_hint: {"x":0, "top":0.5}
            halign: 'left'
            size_hint_x: 0.3
            helper_text: 'Ingresar el monto a solicitar'
            helper_text_mode: 'on_focus'
            write_tab: False
            #input_filter: 'float'
            required: True
            on_text:
                root.limit_currency()

    MDRaisedButton:
        id: boton_hosped_viaje
        pos_hint: {"x":0, "top":0.5}
        text:'Ingresar Gasto'
        on_release:
            root.apply_currency_format()

        BoxLayout:
            size_hint_x: 0.05

### VARIOS
<MyContentVarios>:
    adaptive_height: True
    MDBoxLayout:
        orientation:'horizontal'
        adaptive_height:True
        size_hint_x:self.width
        pos_hint: {"center_x":0.5, "center_y":0.5}
        spacing: dp(10)
        padding_horizontal: dp(10)
        MDLabel:
            text: 'Monto:'
            multiline: 'True'
            halign: 'center'
            pos_hint: {"x":0, "top":0.5}
            size_hint_x: 0.15
            font_style: 'Button'
            font_size: 19

        MDTextField:
            id: monto_varios_viaje
            hint_text: 'Monto a solicitar'
            pos_hint: {"x":0, "top":0.5}
            halign: 'left'
            size_hint_x: 0.3
            helper_text: 'Ingresar el monto a solicitar'
            helper_text_mode: 'on_focus'
            write_tab: False
            #input_filter: 'float'
            required: True
            on_text:
                root.limit_currency()

        MDRaisedButton:
            id: boton_varios_viaje
            pos_hint: {"x":0, "top":0.5}
            text:'Ingresar Gasto'
            on_release:
                root.apply_currency_format()

        BoxLayout:
            size_hint_x: 0.05

1 个答案:

答案 0 :(得分:1)

在您的num_train方法中,该行:

sumar_gasto()

正在创建travel_manager = TravelManagerWindow() 的新实例,该实例与GUI中显示的实例无关。因此,您对该实例所做的任何更改都不会影响您的GUI。

没有看到更多代码,我无法猜测您将如何访问GUI中实际的TravelManagerWindow实例。

因此,您可以自己解决这个问题,也可以发布minimal, complete, verifiable example

因此,在发布了其他代码之后,我认为您可以替换:

TravelManagerWindow

具有:

travel_manager = TravelManagerWindow()

之所以可行,是因为travel_manager = MDApp.get_running_app().root.get_screen('travelManager') 的{​​{1}}小部件是root

您还应该在MDApp中为WindowManager添加一个name

LoginWindow

我还注意到您有一些不必要的代码:

kv

以上几行无效,原因与<LoginWindow>: name: 'login' MDRaisedButton: text: 'Enter' pos_hint: {'center_x': 0.5, 'center_y': 0.5} size_hint: None, None on_release: root.manager.transition.direction = 'up' root.manager.current = 'travelManager' 代码相同。调用ScreenManager().add_widget(LoginWindow(name='login')) ScreenManager().add_widget(TravelManagerWindow(name='travelManager')) 会创建一个不在您的GUI中的travel_manager = TravelManagerWindow()的新实例,并且向该实例中添加一个ScreenManager()对您的GUI无效。 ScreenManager开头的Widget规则将构建两个WindowManager,然后添加到kv