我是kivy的新用户。
我需要链接.kv界面中的复选框,以便它对底层python应用程序产生影响。
我为我的应用程序尝试了以下代码:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.checkbox import CheckBox
import os
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
class Root(FloatLayout):
loadfile = ObjectProperty(None)
checkbox = CheckBox()
def dismiss_popup(self):
self._popup.dismiss()
def show_load(self):
content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
self._popup = Popup(title="Load file", content=content, size_hint=(0.9, 0.9))
self._popup.open()
def load(self, path, filename):
print 'user chose: %s' % os.path.join(path, filename[0])
print self.checkbox.active
self.dismiss_popup()
def activate_checkbox(self):
print 'activate checkbox'
self.checkbox._toggle_active()
class Chooser(App):
pass
Factory.register('Root', cls=Root)
Factory.register('LoadDialog', cls=LoadDialog)
if __name__ == '__main__':
Chooser().run()
使用以下chooser.kv
文件:
#:kivy 1.1.0
Root:
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: 90
height: 30
Button:
text: 'Load'
on_release: root.show_load()
BoxLayout:
size_hint_y: 90
height: 30
CheckBox:
center: self.parent.center
on_active: root.activate_checkbox
Label:
font_size: 20
center_x: self.parent.width / 4
top: self.parent.top - 50
text: 'Faster'
<LoadDialog>:
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
BoxLayout:
size_hint_y: None
height: 30
Button:
text: "Cancel"
on_release: root.cancel()
Button:
text: "Load"
on_release: root.load(filechooser.path, filechooser.selection)
不幸的是,它无济于事:似乎CheckBox用户界面元素的状态对Root Class中的复选框的状态没有任何影响。
有没有简单的方法来链接这两个?
答案 0 :(得分:1)
on_active: root.activate_checkbox
这不会做任何事情,你想要root.activate_checkbox()
。