在我正在编码的应用程序中,单击屏幕右下角的按钮会出现一个弹出窗口。在";完成";单击弹出窗口关闭(close_dialog
方法(,并显示一个新的列表项。列表项的文本是从弹出窗口的MDTextField中获取的。在List Items(列出项目(上,单击进入另一个屏幕<GroupScreen>
(goto_group
方法(。
所以我有两个问题:
- 据我所知,如果我们制作几个列表项,它们都会导致
<Group Screen>
的一个实例。我说得对吗 - 我希望每个创建的列表项都指向其唯一的
<GroupScreen>
实例。例如,我希望将列表文本复制到MDLabel(而不是"欢迎"文本(。我该怎么做
Code.py:
from kivy.lang import Builder
from kivy.core.window import Window
from kivymd.app import MDApp
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.textfield import MDTextField
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.list import TwoLineAvatarListItem
Window.size = (288, 511)
sm = ScreenManager()
class GroupScreen(Screen):
pass
class DialogContent(BoxLayout):
pass
class MainScreen(Screen):
dialog = None
def show_dialog(self, *args):
'''
Create group creation popup
'''
if not self.dialog:
self.dialog = MDDialog(
title="Create new group",
type="custom",
content_cls=DialogContent(),
)
self.dialog.open()
def close_dialog(self, *args):
'''
Close popup on Done click
'''
self.dialog.dismiss()
self.new_window()
def new_window(self, *args):
'''
Create new group button
'''
mylist = TwoLineAvatarListItem(text = self.dialog.content_cls.textfield.text,
on_release = self.goto_group)
self.mdlist.add_widget(mylist)
def goto_group(self, *args):
sm.current = 'group'
class grudget4App(MDApp):
def build(self):
sm.add_widget(MainScreen(name='main'))
sm.add_widget(GroupScreen(name='group'))
scroll = ScrollView()
return sm
if __name__ == '__main__':
grudget4App().run()
代码。kv:
ScreenManager:
MainScreen:
GroupScreen:
<DialogContent>:
textfield: textfield
orientation: "vertical"
spacing: "12dp"
size_hint_y: None
height: "120dp"
MDTextField:
id: textfield
hint_text: "Group name"
MDFlatButton:
id: btn1
text: "Done"
text_color: self.theme_cls.primary_color
on_release: app.root.get_screen('main').close_dialog()
<MainScreen>:
name: 'main'
mdlist: mdlist
FloatLayout:
size_hint: 1, 0.89
ScrollView:
MDList:
id: mdlist
MDFloatingActionButton:
pos_hint: {'right': 0.95, 'y': 0.05}
icon: "icon.png"
theme_text_color: "Custom"
text_color: app.theme_cls.primary_color
on_release:
root.show_dialog()
<GroupScreen>:
name: 'group'
MDLabel:
text: "Welcome" #app.root.ids["textfield"].text
halign: 'center'
MDRectangleFlatButton:
text: 'Back'
pos_hint: {'center_x': 0.5, 'center_y': 0.3}
on_release:
root.manager.current = 'main'
来自文档:
名称
屏幕的名称,在ScreenManager中必须是唯一的。
我没有看到任何代码创建新的GroupScreens
,但无论你把代码放在哪里,你都必须为它包含一个唯一的name="some_group_name"
。
根据我的经验,为了允许Buttons
在MDDialog
中操作,您必须将auto_dismiss=False
添加到其声明中:
def show_dialog(self, *args):
'''
Create group creation popup
'''
if not self.dialog:
self.dialog = MDDialog(
text="Create new group",
type="custom",
auto_dismiss=False, # needed to allow Buttons to operate
content_cls=DialogContent(),
)
self.dialog.open()
然后,new_window()
方法可以创建新的GroupScreen
和Button
:
def new_window(self, *args):
'''
Create new group button
'''
group_name = self.dialog.content_cls.textfield.text
mylist = TwoLineAvatarListItem(text=group_name,
on_release = partial(self.goto_group, group_name)) # use partial to pass group name
self.mdlist.add_widget(mylist)
sm.add_widget(GroupScreen(name=group_name)) # actually create the new GroupScreen and add it
goto_group()
方法变为:
def goto_group(self, group_name, *args):
sm.current = group_name # switch current screen to the passed in group
App
声明可以是:
class grudget4App(MDApp):
def build(self):
sm.add_widget(MainScreen(name='main'))
# sm.add_widget(GroupScreen(name='group'))
# scroll = ScrollView()
return sm
请注意以上对Screens
的唯一name
的要求。您应该注意用户不会创建重复的组名。