如何使用 python 和 kivy 获取函数外的值
Posted
技术标签:
【中文标题】如何使用 python 和 kivy 获取函数外的值【英文标题】:How to get value outside function using python and kivy 【发布时间】:2020-09-28 00:10:17 【问题描述】:这是一个简单的 python kivy 应用程序,只有文本标签和滑块。此代码运行良好。
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivymd.app import MDApp
from kivymd.theming import ThemeManager
from kivymd.uix.slider import Slider
from kivy.properties import NumericProperty
from kivymd.uix.label import MDLabel
class Screen1(Screen):
def __init__(self, **kwargs):
super(Screen1, self).__init__(**kwargs)
layout = FloatLayout()
self.hours_value = MDLabel(text = "0", halign = "center", pos_hint = 'center_x':.5, 'center_y':.80, theme_text_color = 'Error')
self.hours_slider = Slider(min = 0, max = 24, pos_hint = 'center_x':.5, size_hint_x = .7)
self.hours_slider.bind(value = self.on_value_hours)
layout.add_widget(self.hours_value)
layout.add_widget(self.hours_slider)
self.add_widget(layout)
def on_value_hours(self, instance, slider):
self.hours_value.text = "%d"%slider
print(instance)
print("%d"%slider)
return slider
class myapp(MDApp):
def build(self):
self.theme_cls = ThemeManager()
self.theme_cls.primary_palette = 'Orange'
self.theme_cls.accent_pallete = 'Blue'
self.theme_cls.theme_style = 'Dark'
sm = ScreenManager()
sm.add_widget(Screen1(name = 's1'))
return sm
if __name__ == "__main__":
myapp().run()
但是现在我想在我的函数def on_value_hours(self, instance, slider):
之外的滑块上获得价值
所以我添加此代码my_value_slider = on_value_hours()
但我收到一个错误 on_value_hours() 缺少 3 个必需的位置参数:“self”、“instance”和“slider”
我尝试添加my_value_slider = on_value_hours(1,1,1)
但这对我也不起作用:(
给我一个错误AttributeError: 'int' object has no attribute 'hours_value'
这是一个完整的代码,但仍然给我一个错误。我想在我的职能之外获得价值。请帮帮我。
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivymd.app import MDApp
from kivymd.theming import ThemeManager
from kivymd.uix.slider import Slider
from kivy.properties import NumericProperty
from kivymd.uix.label import MDLabel
class Screen1(Screen):
def __init__(self, **kwargs):
super(Screen1, self).__init__(**kwargs)
layout = FloatLayout()
self.hours_value = MDLabel(text = "0", halign = "center", pos_hint = 'center_x':.5, 'center_y':.80, theme_text_color = 'Error')
self.hours_slider = Slider(min = 0, max = 24, pos_hint = 'center_x':.5, size_hint_x = .7)
self.hours_slider.bind(value = self.on_value_hours)
layout.add_widget(self.hours_value)
layout.add_widget(self.hours_slider)
self.add_widget(layout)
def on_value_hours(self, instance, slider):
self.hours_value.text = "%d"%slider
print(instance)
print("%d"%slider)
return slider
my_value_slider = on_value_hours(1,1,1)
class myapp(MDApp):
def build(self):
self.theme_cls = ThemeManager()
self.theme_cls.primary_palette = 'Orange'
self.theme_cls.accent_pallete = 'Blue'
self.theme_cls.theme_style = 'Dark'
sm = ScreenManager()
sm.add_widget(Screen1(name = 's1'))
return sm
if __name__ == "__main__":
myapp().run()
【问题讨论】:
【参考方案1】:你可以这样做:
class Screen1(Screen):
# slider value
my_value_slider = NumericProperty(0)
def __init__(self, **kwargs):
super(Screen1, self).__init__(**kwargs)
layout = FloatLayout()
self.hours_value = MDLabel(text = "0", halign = "center", pos_hint = 'center_x':.5, 'center_y':.80, theme_text_color = 'Error')
self.hours_slider = Slider(min = 0, max = 24, pos_hint = 'center_x':.5, size_hint_x = .7)
self.hours_slider.bind(value = self.on_value_hours)
layout.add_widget(self.hours_value)
layout.add_widget(self.hours_slider)
self.add_widget(layout)
def on_value_hours(self, instance, slider):
self.hours_value.text = "%d"%slider
print(instance)
print("%d"%slider)
# save value
self.my_value_slider = slider
return slider
【讨论】:
非常感谢!这对我有用 :) 你是神 这很好,但只能在这个类中使用,如何使它成为另一个类的访问权限? 您可以通过对Screen1
的引用来做到这一点,这可以从您的ScreenManager
获得。
如果我输入另一个类 s = Screen1()
the_value = s.my_value_slider
print(the_value)
这给我一个 0 值
是的,因为这不是访问变量的正确方法。代码s = Screen1()
创建Screen1
的新实例,并且不引用GUI 中的实例。你应该可以做类似App.get_running_app().root.get_screen('s1').my_value_slider
的事情。【参考方案2】:
另一种方法更像是您的初始代码,其中my_slider_value
将是一个类变量(而不是实例变量):
class Screen1(Screen):
# class variable
my_value_slider = 0
def __init__(self, **kwargs):
super(Screen1, self).__init__(**kwargs)
layout = FloatLayout()
self.hours_value = MDLabel(text = "0", halign = "center", pos_hint = 'center_x':.5, 'center_y':.80, theme_text_color = 'Error')
self.hours_slider = Slider(min = 0, max = 24, pos_hint = 'center_x':.5, size_hint_x = .7)
self.hours_slider.bind(value = self.on_value_hours)
layout.add_widget(self.hours_value)
layout.add_widget(self.hours_slider)
self.add_widget(layout)
def on_value_hours(self, instance, slider):
self.hours_value.text = "%d"%slider
print(instance)
print("%d"%slider)
# update value of class variable
Screen1.my_value_slider = slider
return slider
然后您可以以Screen1.my_value_slider
的身份访问my_value_slider
。请注意,在该访问中没有使用括号。这种方式的缺点是my_value_slider
不是Property
,所以不能绑定到它。
【讨论】:
以上是关于如何使用 python 和 kivy 获取函数外的值的主要内容,如果未能解决你的问题,请参考以下文章