How to use a variable from external functions inside Kivy-Labels?
本问题已经有最佳答案,请猛点这里访问。
我是一个python/kivy初学者,我想得到一个值的总和??A和B在"OutputWindow"标签上,有人能帮我吗?谢谢!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | class Example(App): def build(self): layout = BoxLayout(orientation='vertical') self.otputWindow = Label(text="...") self.aClick = Button(text="Calc >>") self.aClick.bind(on_press=self.first_number) self.aClick.bind(on_press=self.second_number) layout.add_widget(self.otputWindow) layout.add_widget(self.aClick) return layout def first_number(self, *args): a = 5 def second_number(self, *args): b = 10 if __name__ == '__main__': Example().run() |
我自己没有使用过kivy,我对python位有一个评论:
您在方法中使用的变量
这里有一个关于您的代码的建议(尽管它可能不完整,因为我不知道您要做什么):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class Example(App): def build(self): layout = BoxLayout(orientation='vertical') self.first_number() self.second_number() self.otputWindow = Label(text=str(self.a + self.b)) self.aClick = Button(text="Calc >>") self.aClick.bind(on_press=self.first_number) self.aClick.bind(on_press=self.second_number) layout.add_widget(self.otputWindow) layout.add_widget(self.aClick) return layout def first_number(self, *args): self.a = 5 def second_number(self, *args): self.b = 10 if __name__ == '__main__': Example().run() |