Python,Kivy:从不同类/屏幕调用函数的问题 [英] Python, Kivy: Problem with calling functions from different classes/screens

查看:0
本文介绍了Python,Kivy:从不同类/屏幕调用函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在正确调用不同类的函数时遇到问题。

我正在做一个简单的游戏,它使用清除关卡所需的时间来计算分数。有一个秒表在后台运行,我想在弹出菜单中添加一个暂停按钮,并在此弹出菜单中添加一个恢复按钮。

问题是,当从弹出菜单中调用暂停函数时,它也会在弹出菜单中返回,而不是在主小工具中返回。

以下是代码的简化版本:

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
from kivy.clock import Clock

root_widget = Builder.load_file('app.kv')


class ExampleWidget(Widget):
    time = NumericProperty(0)
    paused = False
    stop = False

    # Keeping time
    def increment_time(self, interval):

        self.time += .1
        print(self.time)  # To check if stopwatch is running or not

    # Stop should mean that the stopwatch must reset when it starts again.
    # When paused it should resume when it starts again
    def stop_start_or_pause(self):

        # stop stopwatch
        if self.stop:
            Clock.unschedule(self.increment_time)
            print('Stopped')

        # Make sure time is 0 when restarting
        elif not self.stop and not self.paused:
            # Keeping time
            self.time = 0
            Clock.schedule_interval(self.increment_time, .1)

        # Pause stopwatch
        elif self.paused:
            Clock.unschedule(self.increment_time)
            print("!!", self.time)  # To make it easier to see if stopwatch actually resumes where it left off
            print('unscheduled')  # Just to confirm and to make it a bit easier to see

        # resume stopwatch
        elif not self.paused:
            Clock.schedule_interval(self.increment_time, .1)


class PopupMenu(Popup):
    example = ExampleWidget()


class MyApp(App):
    ExampleWidget = ExampleWidget()

    def build(self):
        return ExampleWidget()


MyApp().run()

.kv文件:

#:import Factory kivy.factory.Factory
<PopupMenu@Popup>
    auto_dismiss: False
    size_hint_y: .8
    size_hint_x: .9
    title: 'Pause'
    example: app.ExampleWidget

    BoxLayout:
        Button:
            text: 'resume'
            on_press: root.example.paused = False
            on_release: root.dismiss(); root.example.stop_start_or_pause()
            size: self.size

<ExampleWidget>:
    GridLayout:
        col: 2
        rows: 3
        size: root.size
        Button:
            text: 'start'
            size: self.size
            on_press: root.stop = False; root.stop_start_or_pause()
        Button:
            text: 'stop'
            size: self.size
            on_press: root.stop = True; root.stop_start_or_pause()
        Button:
            text: 'Pause menu'
            size: self.size
            on_press: root.paused = True
            on_release: Factory.PopupMenu().open(); root.stop_start_or_pause()
        Label:
            text: str(round(root.time))
            size: self.size

我尝试创建一个函数并使用Clock.Schedule.Interval()来不断检查是否已暂停==True,但它一直返回:

AttributeError: 'float' object has no attribute 'stopped'

无论如何,这似乎不是一个有效的解决方案,所以我不想在这个函数上花费太多时间。我还试着找出"愚蠢的"错误(例如,‘,’而不是‘’。)但那是在我意识到恢复按钮返回的是‘秒’秒表而不是更新我真正想要使用的秒表之前。

我希望有人能帮忙,我的问题很清楚。英语不是我的母语,所以有时我很难找到最好的方式来解释/提问。

提前感谢!

推荐答案

如果我理解您的问题,问题出在您的MyApp类:

class MyApp(App):
    ExampleWidget = ExampleWidget()

    def build(self):
        return ExampleWidget()

此代码创建ExampleWidget的两个实例。一个在build()方法中返回,另一个保存为MyAppExampleWidget属性。现在,当您使用MyAppExampleWidget属性时,您引用的不是您的图形用户界面的根目录ExampleWidget,因此它对屏幕上显示的内容没有影响。修复方法是只创建ExampleWidget的单个实例,如下所示:

class MyApp(App):
    ExampleWidget = ExampleWidget()

    def build(self):
        return self.ExampleWidget

这篇关于Python,Kivy:从不同类/屏幕调用函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆