无法让 kivy 标签通过变量更新
Can't get kivy label to update through variable
我试图让一个应用程序工作,我在 kivy recycleview 中获得目录中的文件夹列表。然后我想 select 其中一个文件夹,并将其名称显示在标签中。我是 kivy 的新手,也是 python 的新手,在两天多的时间里一遍又一遍地阅读帖子后,我无法设法让 selected 文件夹显示在标签。如果我通过 kv 中的按钮调用函数而不是通过 SelectableButton class.
我的 .py 文件:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ListProperty, StringProperty, ObjectProperty, BooleanProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.boxlayout import BoxLayout
prjct_list_clean = ['Folder1', 'Folder2', 'Folder3', 'Folder4', 'Folder5', 'Folder6']
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
""" Adds selection and focus behaviour to the view. """
selected_value = StringProperty('')
btn_info = ListProperty(prjct_list_clean)
class SelectableButton(RecycleDataViewBehavior, Button, Widget):
""" Add selection support to the Label """
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
#############################ProblemPart###################################
def on_press(self):
con_wksp = str(self.text)
self.ids.con_wksp_label = con_wksp
print(self.ids.con_wksp_label) #Value gets printet
###########################################################################
class RV(RecycleView):
rv_layout = ObjectProperty(None)
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data = []
for subject in prjct_list_clean:
self.data.append({'text':subject})
print(prjct_list_clean)
class SelectedWksp(Widget):
pass
class TroubleshootApp(BoxLayout, App,):
def build(self):
self.add_widget(RV())
self.add_widget(SelectedWksp())
return self
if __name__ == "__main__":
TroubleshootApp().run()
我的 .kv 文件:
#:kivy 2.0.0
<RV>:
rv_layout: rv_layout
bar_width: 0
viewclass: 'SelectableButton'
SelectableRecycleBoxLayout:
id: rv_layout
default_size: None, dp(56)
default_size_hint: 0.9, None
size_hint_y: None
height: self.minimum_height
orientation: "vertical"
<SelectedWksp>:
BoxLayout:
Label:
id: con_wksp_label
text: 'Default'
我确定这可能是一些简单的菜鸟错误,但我就是无法理解它。
谢谢大家抽出时间。
您可以将 on_press()
方法更改为:
def on_press(self):
App.get_running_app().set_label(self.text)
并将您的应用 class 修改为:
class TroubleshootApp(BoxLayout, App,):
def build(self):
self.add_widget(RV())
self.wksp = SelectedWksp() # save a reference to the SelecetedWksp instance
self.add_widget(self.wksp)
return self
def set_label(self, txt): # set the text of the label
self.wksp.ids.con_wksp_label.text = txt
我试图让一个应用程序工作,我在 kivy recycleview 中获得目录中的文件夹列表。然后我想 select 其中一个文件夹,并将其名称显示在标签中。我是 kivy 的新手,也是 python 的新手,在两天多的时间里一遍又一遍地阅读帖子后,我无法设法让 selected 文件夹显示在标签。如果我通过 kv 中的按钮调用函数而不是通过 SelectableButton class.
我的 .py 文件:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ListProperty, StringProperty, ObjectProperty, BooleanProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.boxlayout import BoxLayout
prjct_list_clean = ['Folder1', 'Folder2', 'Folder3', 'Folder4', 'Folder5', 'Folder6']
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
""" Adds selection and focus behaviour to the view. """
selected_value = StringProperty('')
btn_info = ListProperty(prjct_list_clean)
class SelectableButton(RecycleDataViewBehavior, Button, Widget):
""" Add selection support to the Label """
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
#############################ProblemPart###################################
def on_press(self):
con_wksp = str(self.text)
self.ids.con_wksp_label = con_wksp
print(self.ids.con_wksp_label) #Value gets printet
###########################################################################
class RV(RecycleView):
rv_layout = ObjectProperty(None)
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data = []
for subject in prjct_list_clean:
self.data.append({'text':subject})
print(prjct_list_clean)
class SelectedWksp(Widget):
pass
class TroubleshootApp(BoxLayout, App,):
def build(self):
self.add_widget(RV())
self.add_widget(SelectedWksp())
return self
if __name__ == "__main__":
TroubleshootApp().run()
我的 .kv 文件:
#:kivy 2.0.0
<RV>:
rv_layout: rv_layout
bar_width: 0
viewclass: 'SelectableButton'
SelectableRecycleBoxLayout:
id: rv_layout
default_size: None, dp(56)
default_size_hint: 0.9, None
size_hint_y: None
height: self.minimum_height
orientation: "vertical"
<SelectedWksp>:
BoxLayout:
Label:
id: con_wksp_label
text: 'Default'
我确定这可能是一些简单的菜鸟错误,但我就是无法理解它。
谢谢大家抽出时间。
您可以将 on_press()
方法更改为:
def on_press(self):
App.get_running_app().set_label(self.text)
并将您的应用 class 修改为:
class TroubleshootApp(BoxLayout, App,):
def build(self):
self.add_widget(RV())
self.wksp = SelectedWksp() # save a reference to the SelecetedWksp instance
self.add_widget(self.wksp)
return self
def set_label(self, txt): # set the text of the label
self.wksp.ids.con_wksp_label.text = txt