如何通过单击一个按钮来更新Kivy的应用程序标题

发布于 2025-01-25 05:53:18 字数 4472 浏览 0 评论 0原文

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

我的应用程序运行时的标题是“我的房子”。

但是,当我单击“更新顶级栏的名称“内部”信息“弹出窗口”时,我想更改它。

当我单击“更新顶部栏的名称”按钮“信息”弹出窗口时,我想使用appname.text更新主应用标题。 (AppName是“信息”弹出窗口中MDTEXTFIELD的ID)。 您可以将新应用名称输入到MDTEXTFIELD中。

我尝试的是,通过单击“更新顶部的名称”按钮,它将AppName.Text保存在文本文件中,然后杀死应用程序并重新运行应用程序。然后,它加载保存的文本文件,然后读取并将新的应用程序名称放入“ def Build(self):”的标题中。但是我不想杀死该应用程序并重新运行该应用程序。不过,我没有在下面的此代码中包含此逻辑。

如果有人可以帮助我在不重新运行该程序的情况下更改AwesoMeApp的主要标题,我将非常感谢。

问候,

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

''

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

'''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2025-02-01 05:53:18

如果要在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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文