我开始使用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()
答案 0 :(得分:0)
使用
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()
编辑:您也可以尝试在on_enter()
中使用ResultWindow()
,当kivy显示Screen时,该值可能会每次更新,但您可能会看到滚动屏幕后值的变化。我尝试对on_pre_enter()
做同样的事情,但是对我来说不起作用。参见events for Screen
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()