Skip to content
Advertisement

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

I’d like to change the title of the main App screen(AwesomeApp).

The title of my App when it runs is “My house”.

But i want to change it when I click “Update Top Bar’s name” inside “Information” popup window.

When i click “Update Top Bar’s name” button inside “Information” Popup window, I want to update the title of main App with appName.text. (appName is the id of MDTextField inside “Information” popup window). You can input a new app name into MDTextField.

What i have tried is that By clicking “Update Top Bar’s name” button, it saves appName.text in a text file and then Kill the App and re-run the app. then it loades the saved text file and read and put the new app name into the title inside “def build(self):”. But i don’t want to kill the app and re-run the app. I have not included this logic in this code below though.

If anyone could help me to change AwesomeApp’s main title without re-running this program, I would greatly appreciate it.

Regards,

python file ”’

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 file

”’

#: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()

”’

Advertisement

Answer

If you want to change it in kvlang you can do it as,

...
    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()
...

Or, from python,

First in 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.
...

Then in method 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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement