首页 > 解决方案 > 为什么全局变量在开始时没有更新,但在按下按钮时更新了?

问题描述

我开始使用 kivy 开发这个小型计算器,我有 2 个窗口。一个用计算器,一个用结果。我有 4 个操作按钮,当我按下其中一个按钮时,我想转到第二个窗口,在那里我有一个应该是结果的标签。我将结果存储在一个全局变量中。当我更改窗口时,标签会更新为全局的默认值,但如果我按下我的按钮,我可以更新为最新的。谁能解释为什么?

import time
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.properties import StringProperty

ResultVariable = -1

class CalculatorWindow(Screen):
    numberone = ObjectProperty(None)
    numbertwo = ObjectProperty(None)

    def addition(self):
        global ResultVariable
        sm.current="Result"
        ResultVariable=int(self.numberone.text)+int(self.numbertwo.text)
        ResultWindow().updatevalue()
        print(ResultVariable)


    def substraction(self):
        pass
    def multiplication(self):
        pass
    def division(self):
        pass



class ResultWindow(Screen):
    result_number = StringProperty()

    def __init__(self, **kwargs):
        super(ResultWindow, self).__init__(**kwargs)
        self.updatevalue()

    def updatevalue(self):
        print("It has entered the function")
        self.result_number = str(ResultVariable)

    def restart(self):
        self.result_number = str(ResultVariable)
        #sm.current="Calculator"


class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("graphics.kv")

sm = WindowManager()

screens = [CalculatorWindow(name="Calculator"),ResultWindow(name="Result")]
for screen in screens:
    sm.add_widget(screen)

sm.current = "Calculator"

class MyMainApp(App):
    def build(self):
        return sm


if __name__ == "__main__":
    MyMainApp().run()

print("program ended")

这就是基维

<CalculatorWindow>:
    name: "Calculator"

    numberone: firstone
    numbertwo: secondone

    FloatLayout:
        Label:
            text: "First number"
            pos_hint: {"x":0.0, "top":0.8}
            size_hint: 0.5, 0.1

        Label:
            text: "Second number"
            pos_hint: {"x":0.0, "top":0.6}
            size_hint: 0.5, 0.1

        TextInput:
            pos_hint: {"x":0.5, "top":0.8}
            size_hint: 0.4, 0.1
            id: firstone
            multiline: False

        TextInput:
            pos_hint: {"x":0.5, "top":0.6}
            size_hint: 0.4, 0.1
            id: secondone
            multiline: False

        Button:
            text: "+"
            pos_hint: {"x":0.04, "top":0.35}
            size_hint: 0.2, 0.2
            on_release:
                root.manager.transition.direction = "left"
                root.addition()

        Button:
            text: "-"
            pos_hint: {"x":0.28, "top":0.35}
            size_hint: 0.2, 0.2
            on_release:
                root.manager.transition.direction = "left"
                root.addition()

        Button:
            text: "*"
            pos_hint: {"x":0.52, "top":0.35}
            size_hint: 0.2, 0.2
            on_release:
                root.manager.transition.direction = "left"
                root.addition()

        Button:
            text: "/"
            pos_hint: {"x":0.76, "top":0.35}
            size_hint: 0.2, 0.2
            on_release:
                root.manager.transition.direction = "left"
                root.addition()


<ResultWindow>
    name: "Result"

    resultvariable:solution

    FloatLayout:
        Label:
            id: solution
            text: root.result_number
            pos_hint: {"x":0.0, "top":0.8}
            size_hint: 0.5, 0.1

        Button:
            text: "Reset"
            pos_hint: {"x":0.1, "top":0.6}
            size_hint: 0.8, 0.4
            on_release:
                root.manager.transition.direction = "right"
                root.restart()

标签: pythonkivy

解决方案


使用

ResultWindow().updatevalue()

ResultWindow您创建了与创建的显示窗口无关的类的新实例

screens = [CalculatorWindow(name="Calculator"), ResultWindow(name="Result")]

你必须使用

screens[1].updatevalue()

import time
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.properties import StringProperty

ResultVariable = -1

class CalculatorWindow(Screen):

    numberone = ObjectProperty(None)
    numbertwo = ObjectProperty(None)

    def addition(self):
        global ResultVariable
        sm.current = "Result"
        ResultVariable = int(self.numberone.text)+int(self.numbertwo.text)
        screens[1].updatevalue()
        print(ResultVariable)

    def substraction(self):
        pass

    def multiplication(self):
        pass

    def division(self):
        pass


class ResultWindow(Screen):

    result_number = StringProperty()

    def __init__(self, **kwargs):
        #super(ResultWindow, self).__init__(**kwargs)
        super().__init__(**kwargs)
        self.updatevalue()

    def updatevalue(self):
        print("It has entered the function")
        self.result_number = str(ResultVariable)

    def restart(self):
        self.result_number = str(ResultVariable)
        #sm.current="Calculator"


class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("graphics.kv")

sm = WindowManager()

screens = [CalculatorWindow(name="Calculator"), ResultWindow(name="Result")]
for screen in screens:
    sm.add_widget(screen)

sm.current = "Calculator"

class MyMainApp(App):
    def build(self):
        return sm


if __name__ == "__main__":
    MyMainApp().run()

编辑:您也可以尝试在每次 kivy 显示 Screen 时使用on_enter()in which 可能会更新,但您可能会看到滚动屏幕后值是如何变化的。ResultWindow()我试图做同样的事情,on_pre_enter()但它对我不起作用。查看屏幕事件

import time
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.properties import StringProperty

ResultVariable = -1

class CalculatorWindow(Screen):

    numberone = ObjectProperty(None)
    numbertwo = ObjectProperty(None)

    def addition(self):
        global ResultVariable
        sm.current = "Result"
        ResultVariable = int(self.numberone.text)+int(self.numbertwo.text)
        #screens[1].updatevalue()
        print(ResultVariable)

    def substraction(self):
        pass

    def multiplication(self):
        pass

    def division(self):
        pass


class ResultWindow(Screen):

    result_number = StringProperty()

    def __init__(self, **kwargs):
        #super(ResultWindow, self).__init__(**kwargs)
        super().__init__(**kwargs)
        self.updatevalue()

    def updatevalue(self):
        print("It has entered the function")
        self.result_number = str(ResultVariable)

    def restart(self):
        self.result_number = str(ResultVariable)
        #sm.current="Calculator"

    def on_enter(self):
        self.updatevalue()

class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("graphics.kv")

sm = WindowManager()

screens = [CalculatorWindow(name="Calculator"), ResultWindow(name="Result")]
for screen in screens:
    sm.add_widget(screen)

sm.current = "Calculator"

class MyMainApp(App):
    def build(self):
        return sm


if __name__ == "__main__":
    MyMainApp().run()

推荐阅读