如何通过单击按钮更新kivy中应用程序的标题

How to update the title of an App in kivy by clicking a button

我想更改主应用程序屏幕(AwesomeApp)的标题。

我的App运行时的标题是“我家”。

但是我想在单击“信息”弹出窗口中的“更新顶部栏的名称”时更改它 window。

当我单击“信息”弹出窗口中的“更新顶部栏的名称”按钮时 window,我想用 appName.text 更新主应用程序的标题。 (appName 是“信息”弹出窗口中 MDTextField 的 ID window)。 您可以在 MDTextField 中输入新的应用程序名称。

我尝试过的是,通过单击“更新顶部栏的名称”按钮,它将 appName.text 保存在一个文本文件中,然后终止该应用程序和 re-run 该应用程序。然后它加载保存的文本文件并读取新的应用程序名称并将其放入“def build(self):”内的标题中。但我不想终止应用程序和 re-run 应用程序。不过,我没有在下面的这段代码中包含这个逻辑。

如果没有人能帮我更改 AwesomeApp 的主标题而不 re-run 宁这个程序,我将不胜感激。

此致,

python 文件 '''

from kivy.uix.widget import Widget
'''Setting the size of first window for program'''
from kivy.config import Config                 #another way of setting size of window
Config.set('graphics', 'width', '600')         # from kivy.core.window import Window
Config.set('graphics', 'height', '750')        # Window.size = ("600", "750")

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty

Builder.load_file('new_window_popup.kv')

class Dex(Popup):
    pass
    
class Remi(Popup):
    pass

class Info(Popup):

    def updateName(self):
        # This is where I need a logic to change title of this App with self.appName.text
        print(self.appName.text)
    pass

class MyLayout(Widget):
    pass
class AwesomeApp(MDApp):
    def build(self):
        self.title = "My house"
        return MyLayout()

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

'''

new_window_popup.kv 文件

'''

#:import Factory kivy.factory.Factory
#:import MDRaisedButton kivymd.uix.button

<Dex>:
    auto_dismiss: False
    size_hint: 1, 1

    title: "Weight-Based Dose Calculator "   
    canvas.before:
        Color:
            rgba: (0,1,0,1)
        Rectangle:
            pos:self.pos
            size:self.size
    
    BoxLayout:
        
        orientation: "vertical"
        size:root.width, root.height

        Label:
            text: "Dex 1" 
        Button:
            text: "Close"
            font_size: 24
            on_release: root.dismiss()    
    
<Remi>:
    auto_dismiss: False
    size_hint: 1, 1

    title: "Weight-Based Dose Calculator "   
    canvas.before:
        Color:
            rgba: (0,1,0,1)
        Rectangle:
            pos:self.pos
            size:self.size
    
    BoxLayout:
        
        orientation: "vertical"
        size:root.width, root.height

        Label:
            text: "Remi" 
        Button:
            text: "Close"
            font_size: 24
            on_release: root.dismiss()

<Info>:

    appName:appName
    auto_dismiss: False
    size_hint: 1, 1

    title: "Change Info"   
    canvas.before:
        Color:
            rgba: (0,1,0,1)
        Rectangle:
            pos:self.pos
            size:self.size
    
    BoxLayout:
        
        orientation: "vertical"
        size:root.width, root.height

        Label:
            text: "What is your App name?" 
        BoxLayout:
            orientation: "horizontal"
            
            MDTextField:
                id: appName
                hint_text: "App Name"
                color_mode: 'primary'
                current_hint_text_color: 1,1,1,1
                hint_text_color_focus: 1,1,1,.9 
                line_color_focus: 1,1,1,1
                font_size: '25sp'
                text_color_normal: 1,1,1,.9
                text_color_focus: 0,0,1,.9
                focus: True
                write_tab: False
            Button:
                text: "Update Top Bar\'s name"
                font_size: 24
                size_hint: .8, .2
                on_release: root.updateName()    
        Button:
            text: "Close"
            font_size: 24
            on_release: root.dismiss()

<MyLayout>
    MDBoxLayout:
        orientation:"vertical"
        size: root.width, root.height
        
        MDRaisedButton:
            text: "Dex"
            font_size: 32
            text_color: 0,0,0,.9
            size_hint: 1,.5
            on_press: Factory.Dex().open()
        MDRaisedButton:
            text: "Remi"
            font_size: 32
            size_hint: 1,.5
            on_press: Factory.Remi().open()
        MDRaisedButton:
            text: "Information"
            font_size: 32
            size_hint: 1,.2
            md_bg_color: 0.95,0.61,0.73,1
            on_press: Factory.Info().open()

'''

如果您想在 kvlang 中更改它,您可以这样做,

...
    BoxLayout:
        
        orientation: "vertical"
        size:root.width, root.height

        Label:
#            text: "What is your App name?" 
            text: "Your current App's name : "+app.title # I changed it just to display the title.
        BoxLayout:
            orientation: "horizontal"
            
            MDTextField:
                id: appName
                hint_text: "App Name"
                text: app.title
                color_mode: 'primary'
                current_hint_text_color: 1,1,1,1
                hint_text_color_focus: 1,1,1,.9 
                line_color_focus: 1,1,1,1
                font_size: '25sp'
                text_color_normal: 1,1,1,.9
                text_color_focus: 0,0,1,.9
                focus: True
                write_tab: False
            Button:
                text: "Update Top Bar\'s name"
                font_size: 24
                size_hint: .8, .2
                on_release: app.title = appName.text
        Button:
            text: "Close"
            font_size: 24
            on_release: root.dismiss()
...

或者,从 python

kvlang

第一名
...
            MDTextField:
                id: appName
                hint_text: "App Name"
                text: app.title
                color_mode: 'primary'
                current_hint_text_color: 1,1,1,1
                hint_text_color_focus: 1,1,1,.9 
                line_color_focus: 1,1,1,1
                font_size: '25sp'
                text_color_normal: 1,1,1,.9
                text_color_focus: 0,0,1,.9
                focus: True
                write_tab: False
            Button:
                text: "Update Top Bar\'s name"
                font_size: 24
                size_hint: .8, .2
                on_release: root.updateName(appName) # Pass the MDTextField instance.
...

然后在方法updateName

    def updateName(self, t_field):
        # Access the running App instance. 
        # Note that this happens to be very useful when you
        # need to access the App from anywhere in your code.
        app = MDApp.get_running_app()
        # Change its title using the text of the t_field (that has been passed).
        app.title = t_field.text