kivy - 在 Python Kivy 中更改屏幕后如何关闭 ModalView
问题描述
我有 ModelView 类和屏幕类,当打开模式视图并在更改为第二个屏幕类后在第一个屏幕类上做一些事情但模式关闭不起作用时
/导入模块/
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty, partial
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.modalview import ModalView
from kivy.uix.screenmanager import Screen
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.scrollview import ScrollView
from kivy.uix.stacklayout import StackLayout
/*我的滚动视图,用于将按钮放到屏幕1 */
class SView(ScrollView):
def __init__(self, **kwargs):
super(SView, self).__init__(**kwargs)
Clock.schedule_interval(self.update, 1)
self.MM = MyModal()
self.MM.MyButton=self.MyButton
def MyButton(self, *args):
print("Hello")
def MMDismiss(self, *args):
pass
def ModalOpen(self, instance, button, *args):
self.MM.open()
self.MM.MyButton = self.MyButton
self.MMDismiss = self.MM.dismiss
def update(self, *args):
layout = GridLayout(width=480, padding=10, cols=7, spacing=10, size_hint_y=None)
layout.bind(minimum_height=layout.setter('height'))
self.buttons=["One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three","One", "Two", "Three"]
for key in self.buttons:
stacklayout = StackLayout(size_hint_y=None, size_hint_x=None, height=130, width=100, orientation='lr-tb')
btn = Button(text=key, size_hint_y=None, size_hint_x=None, height=130, width=100)
stacklayout.add_widget(btn)
btn.bind(on_release=partial(self.ModalOpen, key))
layout.add_widget(stacklayout)
self.clear_widgets()
self.add_widget(layout)
/*当按下screen1的按钮时打开模态*/
class MyModal(ModalView):
text = StringProperty("text")
def __init__(self, **kwargs):
super(MyModal, self).__init__(**kwargs)
self.size_hint = (None, None)
self.size = (200, 200)
self.layout=GridLayout(cols=1,padding=10,spacing=10)
self.layout.add_widget(Label(text=self.text))
self.btn=Button(text="My Button")
self.bind(on_press=self.MyButton)
self.layout.add_widget(self.btn)
self.add_widget(self.layout)
def MyButton(self, *args):
pass
/我的屏幕1 /
class Screen1(Screen):
def __init__(self, **kwargs):
super(Screen1, self).__init__(**kwargs)
self.sview=SView()
self.mylayout = GridLayout(cols=1)
self.mylayout.add_widget(self.sview)
self.add_widget(self.mylayout)
self.sview.MyButton = self.MyButton
def MyButton(self, instance, button):
self.manager.current = "screen2"
print("screen 2")
class Screen2(Screen):
def __init__(self, **kwargs):
super(Screen2, self).__init__(**kwargs)
/我的构建器字符串/
Builder.load_string('''
<SM>:
Screen1:
name: "screen1"
Screen2:
name: "screen2"
''')
/我的屏幕管理器/
class SM(ScreenManager):
def __init__(self, **kwargs):
super(SM, self).__init__(**kwargs)
self.current = 'screen1'
/我的应用程序/
class Main(App):
def __init__(self, **kwargs):
super(Main, self).__init__(**kwargs)
def build(self):
self.title = "MAVA"
self.icon = "./images/logo/logo.png"
return SM()
if __name__ == "__main__":
Main().run()
解决方案
在您的MyModal
课程中,您可以将其添加dismiss
到Button
:
class MyModal(ModalView):
text = StringProperty("text")
def __init__(self, **kwargs):
super(MyModal, self).__init__(**kwargs)
self.size_hint = (None, None)
self.size = (200, 200)
self.layout=GridLayout(cols=1,padding=10,spacing=10)
self.layout.add_widget(Label(text=self.text))
self.btn=Button(text="My Button", on_press=self.dismiss) # add dismiss action
self.layout.add_widget(self.btn)
self.add_widget(self.layout)
我还删除了这一行:self.bind(on_press=self.MyButton)
。我不认为那有什么作用。
推荐阅读
- ionic3 - 无法将 NavController 添加到我的 app.component.ts
- javascript - 基本的 Chrome 扩展破坏网站 HTML
- go - 在单独的进程上运行 Goroutines(多进程)
- shell - 参数化以使用 Shell 脚本从 Oracle 表中动态生成提取文件
- excel - 根据数据透视表中的值列(计数)计算平均值
- laravel - 下表的 Laravel 关系设置
- java - maven-site 插件 3.3 java.lang.ClassNotFoundException: org.apache.maven.doxia.siterenderer.DocumentContent
- angularjs - AngularJS & NodeJS - 处理用户角色
- python - 是否可以恢复(返回旧方法)python猴子补丁
- javascript - CSS字体大小保持相同的字宽