【问题标题】:Kivy function scope on button release (widget tree)按钮释放时的 Kivy 功能​​范围(小部件树)
【发布时间】:2023-04-06 00:30:01
【问题描述】:

我是 Kivy 的新手,在理解函数范围方面需要一些帮助。我已经构建了一个带有两个屏幕的简单应用程序。第一个屏幕有两个按钮,第二个屏幕的中心有一个文本标签。在第一个屏幕上,我使用了 app.root.current='new_screen_name' 和一个按钮的 on_release 属性,它工作正常。它带我进入下一个屏幕,这是预期的功能。对于第二个按钮,我使用了一个函数调用,该函数调用已在 Python 文件中第一个屏幕的类定义(按钮的根小部件)下定义。但是,此方法不起作用,应用程序窗口只会关闭。我想我在函数范围和调用中犯了一个错误但我不知道是什么。任何帮助将不胜感激。

Python 文件:

from kivy.config import Config
# Config.set should be used before importing any other Kivy module.
Config.set('kivy','window_icon','sivaicon.png')
# Config set for resizing image button
Config.set('graphics', 'resizable', True)
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.lang.builder import Builder


class SivaLoginScreen(Screen):
    def func_authentication(self):
        app.root.current='tabbed_screen'


class SivaTabbedScreen(Screen):
    pass


class SivaScreenManager(ScreenManager):
    pass


class ImageButton(ButtonBehavior, Image):
    pass


# Tell Kivy to directly load a file. If this file defines a root widget, it will be returned by the method.
root_widget = Builder.load_file('siva.kv')

class SivaApp(App):
    def build(self):
        # Initialize root widget
        return root_widget


if __name__ == '__main__':
    # Run application
    SivaApp().run()

Kivy 文件 (.kv):

SivaScreenManager:
    SivaLoginScreen:
    SivaTabbedScreen:


<ImageButton>:
    keep_ratio: True


<SivaLoginScreen>:
    name: 'login_screen'
    canvas.before:
        Color:
            rgba: 195/255, 60/255, 35/255, 1
        Rectangle:
            pos: self.pos
            size: self.size
    FloatLayout:
        size: root.width, root.height
        Image:
            id: login_logo_siva
            source: 'images/sivalogo4.png'
            keep_ratio: True
            size_hint: 0.2, 0.2
            pos_hint: {'center_x':0.5, 'center_y':0.75}
        Label:
            id: login_label_siva
            pos: self.x*0.5-4, self.y*0.5+15
            markup: True
            font_name: 'roboto/Roboto-Medium.ttf'
            text: '[color=#FDFD98]S.[/color][color=#B29DD9]I[/color][color=#FDFD98].[/color][color=#77DD77]V[/color][color=#FDFD98].[/color][color=#779ECB]A[/color]'
            font_size: '50sp'
        Label:
            id: login_label_slogan1
            pos: self.x*0.5-3, self.y*0.5-6
            markup: True
            font_name: 'roboto/Roboto-Regular.ttf'
            text: '[color=#FDFD98]SLOGAN TEXT[/color]'
            font_size: '15sp'
        Label:
            id: login_label_slogan2
            pos: self.x*0.5-3, self.y*0.5-20
            markup: True
            font_name: 'roboto/Roboto-Regular.ttf'
            text: '[color=#FDFD98]HEADLINE TEXT[/color]'
            font_size: '15sp'
        BoxLayout:
            id:login_button_layout
            orientation: 'horizontal'
            size_hint: 0.2, 0.2
            pos_hint: {'center_x':0.5, 'center_y':0.25}
            ImageButton:
                id: first_button
                source: {'normal': 'images/first.png', 'down': 'images/first-down.png'} [self.state]
                on_release: app.root.current='tabbed_screen'
            ImageButton:
                id: second_button
                source: {'normal': 'images/second.png', 'down': 'images/second-down.png'} [self.state]
                on_release: app.root.func_authentication()


<SivaTabbedScreen>:
    name: 'tabbed_screen'
    FloatLayout:
        size: root.width, root.height
        Label:
            pos: self.x*0.5, self.y*0.5
            text: 'SECOND SCREEN'
            font_size: '50sp'

【问题讨论】:

    标签: python python-3.x kivy kivy-language


    【解决方案1】:

    在您的情况下,app.root 链接到 SivaScreenManager,这是您的应用程序的根小部件。在这些课程中,没有func_authenticationfunction,为什么你的应用程序崩溃了。

    要在 KV 定义中引用一个类本身,您必须只使用root,所以正确的代码必须是:

    on_release: root.func_authentication()

    Kivy Language - Reserved Keywords

    func_authentication 的定义也不正确,app 未知。使用任一:

    App.get_running_app().root.current='tabbed_screen'

    self.manager.current='tabbed_screen'

    【讨论】:

    • 感谢您的回复,但没有奏效。 func_authentication() 的函数定义是否正确?如何从函数内部更改当前屏幕?
    • func_authentication 的定义也不正确,app 未知。使用App.get_running_app().root.current='tabbed_screen'self.manager..current='tabbed_screen'
    • 非常感谢您帮助我理解。这就像一个魅力!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 2015-02-23
    • 1970-01-01
    • 2016-11-30
    • 2021-06-08
    • 1970-01-01
    相关资源
    最近更新 更多