python - Kivy - 如何从 Python 代码触发模型视图
问题描述
我想在加载应用程序时强制启动 ModalView,但我不知道该怎么做。
当我按下按钮时它工作正常,但我不知道如何从 python 文件触发事件。
我如何从代码中触发事件(按钮)?
.kv文件
<Controller>
<BoxLayout>
***a lot of code ***
Button:
id: my_id
text: "text"
color: txt_color
size_hint_y: .05
background_color: btn_color_not_pressed if self.state=='normal' else btn_color_pressed
on_release:
Factory.About().open()
<About@ModalView>
id: about
auto_dismiss: True
*** some more code ****
如何从我的 main.py 调用事件?
** 来自@john Anderson 的解决方案之后 **
文件:main.py
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.factory import Factory
kivy.require("1.11.1")
class Controller(BoxLayout):
def __init__(self):
super(Controller, self).__init__()
Factory.About().open()
class mainApp(App):
def build(self):
return Controller()
mainApp().run()
文件:main.kv
#:import Factory kivy.factory.Factory
<Controller>
BoxLayout:
orientation: "vertical"
Label:
text: "THIS IS THE MAIN TEKST"
color: 1,0,0,1
size_hint_y:.8
Button:
text: "About"
size_hint_y: .2
on_release: Factory.About().open()
<About@ModalView>
size_hint: .8,.5
BoxLayout
orientation: "vertical"
size: self.width, self.height
pos_hint: {"center_x": .5}
size_hint: .8,.6
Label:
text: "ABOUT MY APP"
color: 0,1,0,1
Button:
text: "Back"
size_hint_y: .2
on_release: root.dismiss()
解决方案
您About
ModalView
是在__init__()
您的根Controller
小部件的方法中创建的。这意味着它是在您的Controller
. About
一个简单的解决方法是将小部件的创建延迟到App
启动之后。您可以使用该App
方法on_start()
来做到这一点:
class Controller(BoxLayout):
pass
class mainApp(App):
def build(self):
return Controller()
def on_start(self):
Factory.About().open()
推荐阅读
- laravel - 提供程序中的错误类初始化?
- java - Java Selenium - 从 Google Maps Live 交通视图中选择一个元素
- c - 反汇编 C 代码中的冗余操作
- memory-management - 结合`mmap`和UVM特性
- mysql - 如何将日志文件的每一行拆分为 SQL 列?(也许使用正则表达式进行拆分)
- c++ - C++:将 chrono::high_resolution_clock 转换为 time_t
- c++ - OpenCL C++ 包装器。是否可以从 Context 类中提取平台和设备?
- razor - 使用 ajax() 发布的 asp.net 核心不显眼验证
- laravel - 如何使用 Homestead、Parallels 和 vagrant 设置 Laravel 开发环境
- apache-kafka - kafka 是否在重新平衡时对传输的分区应用压缩