【问题标题】:KivyMD - Cannot Update TextField's text when used in DialogBoxKivyMD - 在对话框中使用时无法更新文本字段文本
【发布时间】:2020-09-09 19:18:00
【问题描述】:

我使用 KivyMD 开发了一个屏幕,它显示特定项目的参数值(在 DialogBox 中)(我将它们列为 OnelinelistItem强>)。我还想为用户从对话框中更改参数值做准备。但显然我无法从对话框更新参数设置。对话框包含文本字段。任何人都可以通过查看代码来帮助我找出问题所在,并让我知道我在哪里做错了吗? TIA! :)

testingsetpointpage.py

'''

from kivy.config import Config
Config.set('kivy', 'keyboard_mode', 'systemanddock')
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
from kivymd.uix.dialog import MDDialog
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivymd.uix.button import MDFlatButton
from kivy.properties import StringProperty
Window.size = (1024, 600)

这里显示了构建器:

KV = """
#:kivy 1.11.0
#:import MDDropdownMenu kivymd.uix.menu.MDDropdownMenu
#:import MDRaisedButton kivymd.uix.button.MDRaisedButton
ScreenManager:
    MainScreen:
<MainScreen>:   
    name: 'mainscreen'
    NavigationLayout:
        ScreenManager:
            Screen:                
                name: 'homemain'
                BoxLayout:
                    orientation:"vertical"
                    halign:"center" 

                    #DOWN TAB          
                    MDBottomNavigation:                        
                        MDBottomNavigationItem:
                            name: 'setpoint'
                            text: 'Setpoints'
                            icon: 'network'
                              
                            BoxLayout:
                                orientation: "vertical"
                                
                                MDToolbar:
                                    title: 'Setpoints'
                                    pos_hint: {'center_x':0.5,'center_y':0.95}   
                                    right_action_items: [["wifi", lambda x: app.navigation_draw()]]
                                    left_action_items: [["menu", lambda x: nav_drawer.toggle_nav_drawer()]]
                                    elevation: 10
                                
                                BoxLayout:
                                    orientation:"vertical"
                                    padding: 5
                                    spacing: 5
                                    MDLabel:
                                        text: "     Functions: "
                                        halign:"left"
                                        theme_text_color: "Custom"
                                        text_color: 0, 0, 1, 1
                                        size_hint_y: 0.15
                                        canvas.before:
                                            Color:
                                                rgba: (211/255.0,211/255.0,211/255.0,1)
                                            Rectangle:
                                                size: self.size
                                                pos: self.pos
                                    SetpointContent:
                                    
#MAKE THE PARAMETER LIST                                            
<SetpointContent>: 
 
    BoxLayout:
        orientation: "vertical"
        padding: 5
        spacing: 5

        ScrollView:
            MDList:
                OneLineListItem:
                    text: "51P: Phase Time Overcurrent"
                    on_press: root.show_51Pdata()
                
    
<Content51P>
    alarmpick51p: alp51p
    alarmdelay51p: ald51p
    trippick51p: trp51p
    invcurve51p: inp51p
    orientation: "vertical"
    size_hint_y: None
    height: "200dp"
    GridLayout:
        rows: 4
        cols: 2
        spacing: 10
        
        MDLabel:
            text: "Alarm Pickup: "
            halign: "center"
            theme_text_color: "Custom"
            text_color: 0, 0, 1, 1
            size_hint_y: 0.15
            size_hint_x: 0.4
            width:100
        MDTextFieldRect:
            id: alp51p
            text: root.text1
            multiline: False
        MDLabel:
            text: "Alarm Delay: "
            halign: "center"
            theme_text_color: "Custom"
            text_color: 0, 0, 1, 1
            size_hint_y: 0.15
            size_hint_x: 0.4
            width:100
        MDTextFieldRect:
            id: ald51p
            text: str(app.Aldelay51P)
            multiline: False
        MDLabel:
            text: "Trip Pickup:  "
            halign: "center"
            theme_text_color: "Custom"
            text_color: 0, 0, 1, 1
            size_hint_y: 0.15
            size_hint_x: 0.4
            width:100
        MDTextFieldRect:
            id: trp51p
            text: str(app.Trpick51P)
            multiline: False
        MDLabel:
            text: "Inverse Curve:  "
            halign: "center"
            theme_text_color: "Custom"
            text_color: 0, 0, 1, 1
            size_hint_y: 0.15
            size_hint_x: 0.4
            width:100
        MDTextFieldRect:
            id: inp51p
            text: str(app.InverseCurve)
            multiline: False
                            #####                                                                         
"""

这里定义了类:

class Content51P(BoxLayout):
    app=MDApp.get_running_app()
    text1 = StringProperty("1")
    alarmpick51p = ObjectProperty()
    alarmdelay51p = ObjectProperty()
    trippick51p = ObjectProperty()
    invcurve51p = ObjectProperty()



class MainScreen(Screen):
    class SetpointContent(Screen):

        def show_51Pdata(self):
            self.dialog = MDDialog(title="51P Parameters:",
                                   type="custom",
                                   content_cls=Content51P(),
                                   buttons=[MDFlatButton(text='Close', on_release=self.close_dialog),
                                            MDFlatButton(text='Update', on_release=self.update51P)]
                                   )
            self.dialog.auto_dismiss = False
            self.dialog.open()

        def update51P(self, obj):
            duc = Content51P()
            app = MDApp.get_running_app()
            duc.text1 = duc.ids.alp51p.text
            print(duc.text1)
            app.Alpick51P = float(duc.text1)
            print(app.Alpick51P)

        def close_dialog(self, obj):
            self.dialog.auto_dismiss = True
            self.dialog.dismiss()


class MainApp(MDApp):
    Alpick51P = ObjectProperty("5")
    Aldelay51P = ObjectProperty("5")
    Trpick51P = ObjectProperty("5")
    InverseCurve = ObjectProperty("Very Inverse")

    def build(self):
        self.theme_cls.primary_palette = "Blue"
        screen = Builder.load_string(KV)
        return screen

    def navigation_draw(self):
        print("Navigation")

    def on_start(self):
        pass


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

''' The outlook looks something like this. I want to update the four parameters as the user clicks on the Update button and be able to view the value, the next time I open the DialogBx.

【问题讨论】:

    标签: python kivy kivy-language kivymd


    【解决方案1】:

    在 kv 规则中,在&lt;Content51P&gt; 下为MDTextFieldRect 文本输入添加:

    on_text: app.Alpick51P = self.text
    on_text: app.Aldelay51P = self.text
    on_text: app.Trpick51P = self.text
    on_text: app.InverseCurve = self.text
    

    on_text 方法应该转到相应的MDTextFieldRect

    更新update51P() 函数现在应该如下所示:

            def update51P(self, obj):
                app = MDApp.get_running_app()
                print(app.Alpick51P)
                print(app.Aldelay51P)
                print(app.Trpick51P)
                print(app.InverseCurve)
    

    这将打印来自文本输入字段的更新输入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2021-10-06
      • 2015-07-14
      • 2021-04-14
      • 2022-01-17
      • 2020-04-11
      相关资源
      最近更新 更多