【发布时间】:2021-08-11 20:20:43
【问题描述】:
所以我正在构建一个供个人使用的小应用程序,它包含一个日历,我可以在其中安排我的所有活动。几天来,我一直在修改代码,并且已经完成了管理数据所需的类和函数。尽管如此,我制作的屏幕仍然存在问题。我不知道如何通过 App 类访问它们以向它们添加新的小部件。在这种情况下,我想在每次安排新活动时更新 DateScreen...
这里有一些我认为可以帮助你更好地理解问题的代码。
Python 代码
import kivy
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.label import MDLabel
from kivymd.uix.bottomsheet import MDCustomBottomSheet
from kivy.factory import Factory
from kivy.uix.stacklayout import StackLayout
class MainScreen(Screen):
pass
class ActivitySheet(StackLayout):
pass
class DateScreen(Screen):
pass
class Manager(ScreenManager):
pass
class MainApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Light"
kv = Builder.load_file("layout2.kv")
return kv
def show_new_activity_menu(self):
"""Spreads a bottomsheet to enable the user to schedule a new activity
with the selected time and description."""
self.bottom_sheet_menu = MDCustomBottomSheet(screen=ActivitySheet())
self.bottom_sheet_menu.open()
def pass_activity_data(self, time, main, desc):
"""Takes all the data inputted by the user and passes it as arguments,
then it dismisses the bottomsheet. This is essential to allow the
AgendaManager to create a new Activity object."""
m.schedule_new_activity(time, main, desc)
self.update_screen()
self.bottom_sheet_menu.dismiss()
def update_screen(self):
schedule = m.show_schedule(m.working_with)
if schedule:
for time in schedule:
DateScreen().add_widget(MDLabel(text="test"))
if __name__ == "__main__":
MainApp().run()
千伏。代码
Manager:
MainScreen:
DateScreen:
<ActivitySheet>:
id: custom
orientation: "lr-tb"
size_hint_y: None
height: "200dp"
spacing: 0
BoxLayout:
orientation: "horizontal"
pos_hint: {"top": 0}
size_hint: (1, 0.25)
TextInput:
id: time
size_hint: (0.25, 1)
font_size: 26
halign: "center"
valign: "center"
text: "H"
TextInput:
id: main
font_size: 24
text: "Main"
size_hint: (0.75, 1)
valign: "center"
TextInput:
id: desc
size_hint: (1, 0.60)
text: ""
BoxLayout:
orientation: "horizontal"
size_hint: (1, 0.15)
Button:
text: "cancel"
Button:
id: day
text: "save"
on_release: app.pass_activity_data( root.ids.time.text, root.ids.main.text, root.ids.desc.text)
非常感谢您的帮助或建议。我真的被这个困住了。 感谢您的宝贵时间。
【问题讨论】:
标签: python kivy kivy-language kivymd