猿问

将我的自定义小部件连接到我的屏幕管理器

我正在尝试使用 Python 和 kivy 构建一个应用程序。在我的 kv 文件中,我想创建一个可以被其他屏幕引用的自定义小部件 (MenuFloatLayout)。它基本上是每个屏幕上的菜单栏。此栏由几个切换按钮组成,如果您当前在此按钮链接到的屏幕上,这些按钮处于向下状态并禁用。


这被引用: state: "down" if root.manager.current == 'Screenname' else "normal"


问题是:root.manager.current 不再链接到通常的屏幕管理器,因为我的自定义小部件现在是根。


有解决办法吗?或者是否有更简单的方法将切换按钮状态链接到用户所在的屏幕?


我是编程和 Python 的新手,很高兴您能给我任何帮助或提示!谢谢!


Python文件:


from kivy.app import App

from kivy.lang import Builder

from kivy.uix.screenmanager import Screen, ScreenManager



class StartWindow(Screen):

    pass



class PortfolioOverview(Screen):

    pass



class Portfolio(Screen):

    pass



class Market(Screen):

    pass



class Economics(Screen):

    pass



class PortfolioTools(Screen):

    pass



class WindowManager(ScreenManager):

    pass



kv = Builder.load_file("vigiles.kv")



class VigilesApp(App):

    def build(self):

        return kv



if __name__ == "__main__":

    VigilesApp().run()

目标是将我的自定义小部件链接回我的屏幕管理器,或者找到更简单的解决方案将切换按钮状态链接到当前屏幕。

AttributeError:“MenuFloatLayout”对象没有属性“manager”


弑天下
浏览 157回答 2
2回答

holdtom

我会将您的根小部件设为布局小部件(GridLayout、、BoxLayout或FloatLayout),并让您的屏幕管理器仅占用实际屏幕的一部分。尝试改变WindowManager:    StartWindow:    PortfolioOverview:    Portfolio:到:GridLayout:    # Play with using a FloatLayout or BoxLayout instead of GridLayout if you want    cols: 1    MenuFloatLayout:        id: the_menu_id    WindowManager:        StartWindow:        PortfolioOverview:        Portfolio:这样,您的菜单将持续存在,并且屏幕管理器仅更改实际屏幕的一部分。通过给出MenuFloatLayoutanid您可以引用它。要从 Python 端self.root.ids.the_menu_id引用它,请在从App对象引用它时使用。如果您在其他类型的对象中,您可以通过首先导入App( import App) 然后使用App.get_running_app().root.ids.the_menu_id要从屏幕上引用它,您可以使用app关键字。例如:app.root.ids.the_menu_id引用菜单的所有三种方式id基本相同。

翻过高山走不出你

问题实例太多MenuFloatLayout使用大量资源,例如内存,这会使应用程序变大且性能不佳解决方案创建一个实例MenuFloatLayout每个屏幕引用的唯一一个实例MenuFloatLayout使用嵌套ScreenManager.&nbsp;这首先ScreenManager用于控制身份验证/登录屏幕。第二个ScreenManager用于不同屏幕之间的导航。创建一个动态类ToggleButton例子main.pyfrom kivy.app import Appfrom kivy.lang import Builderfrom kivy.uix.screenmanager import Screen, ScreenManagerclass StartWindow(Screen):&nbsp; &nbsp; passclass PortfolioOverview(Screen):&nbsp; &nbsp; passclass Portfolio(Screen):&nbsp; &nbsp; passclass Market(Screen):&nbsp; &nbsp; passclass Economics(Screen):&nbsp; &nbsp; passclass PortfolioTools(Screen):&nbsp; &nbsp; passclass WindowManager(ScreenManager):&nbsp; &nbsp; passBuilder.load_file("main.kv")class TestApp(App):&nbsp; &nbsp; def build(self):&nbsp; &nbsp; &nbsp; &nbsp; return WindowManager()if __name__ == "__main__":&nbsp; &nbsp; TestApp().run()main.kv - kv 文件<WindowManager>:&nbsp; &nbsp; sm2: sm2&nbsp; &nbsp; StartWindow:&nbsp; &nbsp; Screen:&nbsp; &nbsp; &nbsp; &nbsp; name: 'connect'&nbsp; &nbsp; &nbsp; &nbsp; ScreenManager:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: sm2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PortfolioOverview:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Portfolio:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Market:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Economics:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PortfolioTools:&nbsp; &nbsp; &nbsp; &nbsp; MenuFloatLayout:<CustomToggleButton@ToggleButton>:&nbsp; &nbsp; # dynamic class&nbsp; &nbsp; group: "pmenu"&nbsp; &nbsp; state: "normal" if app.root is None else "down" if app.root.sm2.current == self.text.lower() else "normal"&nbsp; &nbsp; background_disabled_down: "atlas://data/images/defaulttheme/button_pressed"&nbsp; &nbsp; disabled_color: 1, 1, 1, 1&nbsp; &nbsp; on_state:&nbsp; &nbsp; &nbsp; &nbsp; if self.state == "down": self.disabled = True&nbsp; &nbsp; &nbsp; &nbsp; else: self.disabled = False&nbsp; &nbsp; on_release:&nbsp; &nbsp; &nbsp; &nbsp; app.root.sm2.current = self.text.lower()<MenuFloatLayout@FloatLayout>:&nbsp; &nbsp; # dynamic class&nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; text: "Portfolio"&nbsp; &nbsp; &nbsp; &nbsp; markup: True&nbsp; &nbsp; &nbsp; &nbsp; size_hint: 0.5, None&nbsp; &nbsp; &nbsp; &nbsp; height: 30&nbsp; &nbsp; &nbsp; &nbsp; pos_hint:{"top":1, "left":1}&nbsp; &nbsp; TextInput:&nbsp; &nbsp; &nbsp; &nbsp; hint_text: "Search"&nbsp; &nbsp; &nbsp; &nbsp; multiline: False&nbsp; &nbsp; &nbsp; &nbsp; size_hint: 0.5, None&nbsp; &nbsp; &nbsp; &nbsp; height: 30&nbsp; &nbsp; &nbsp; &nbsp; pos_hint:{"top":1, "right":1}&nbsp; &nbsp; ScrollView:&nbsp; &nbsp; &nbsp; &nbsp; size_hint: None, None&nbsp; &nbsp; &nbsp; &nbsp; do_scroll_y: False&nbsp; &nbsp; &nbsp; &nbsp; do_scroll_x: True&nbsp; &nbsp; &nbsp; &nbsp; size: 500, 150&nbsp; &nbsp; &nbsp; &nbsp; GridLayout:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rows: 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size_hint_y: None&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CustomToggleButton:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'Overview'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state: 'down'&nbsp; &nbsp;# default&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CustomToggleButton:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'Portfolio'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CustomToggleButton:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'Market'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CustomToggleButton:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'Economics'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CustomToggleButton:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'Tools'<StartWindow>:&nbsp; &nbsp; name: "start"&nbsp; &nbsp; BoxLayout:&nbsp; &nbsp; &nbsp; &nbsp; canvas:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size: self.size&nbsp; &nbsp; &nbsp; &nbsp; color: 1, 1, 1, 0&nbsp; &nbsp; &nbsp; &nbsp; id: login_layout&nbsp; &nbsp; &nbsp; &nbsp; orientation: 'vertical'&nbsp; &nbsp; &nbsp; &nbsp; padding: [10,10,10,10]&nbsp; &nbsp; &nbsp; &nbsp; spacing: 30&nbsp; &nbsp; &nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'some text'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font_size: 32&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: 0, 0, 0, 1&nbsp; &nbsp; &nbsp; &nbsp; BoxLayout:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orientation: 'vertical'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'Login'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font_size: 18&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; halign: 'left'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text_size: root.width-20, 20&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: 0, 0, 0, 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextInput:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: login&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; multiline:False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font_size: 28&nbsp; &nbsp; &nbsp; &nbsp; BoxLayout:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orientation: 'vertical'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'Password'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; halign: 'left'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font_size: 18&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text_size: root.width-20, 20&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: 0, 0, 0, 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextInput:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: password&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; multiline:False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password:True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font_size: 28&nbsp; &nbsp; &nbsp; &nbsp; Button:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'Connect'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font_size: 24&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; on_release:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root.manager.current = 'connect'<PortfolioOverview>:&nbsp; &nbsp; name: "overview"&nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; text: 'Screen - Overview'<Portfolio>:&nbsp; &nbsp; name: "portfolio"&nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; text: 'Screen - Portfolio'<Market>:&nbsp; &nbsp; name: "market"&nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; text: 'Screen - Market'<Economics>:&nbsp; &nbsp; name: "economics"&nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; text: 'Screen - Economics'<PortfolioTools>:&nbsp; &nbsp; name: "tools"&nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; text: 'Screen - Portfolio Tools'输出
随时随地看视频慕课网APP

相关分类

Python
我要回答