0

我是 python 和 kivymd 的新手,我正在尝试开发一个数据输入程序。但是,当我为选择创建下拉菜单时,发生错误,并且在选择项目后我无法更新文本字段的值。这是python代码:

from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.properties import ObjectProperty
from kivymd.uix.menu import MDDropdownMenu
class AMRMenu(Screen):
    def GIbutton(self):
        sm.current = 'GI'
class GIWindow(Screen):
    weather = ObjectProperty(None)
    menu_weather_items = [{"text":"Sunny"},{"text":"Cloudy"},{"text":"Raining"}]
    menu_FeedResponse_items=[{"text":"High"},{"text":"Medium"},{"text":"Low"}]
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.menu = MDDropdownMenu(
                    items=self.menu_weather_items, 
                    width_mult=4,
                    caller = self.weather,
                    callback=self.set_item)
    def set_item(self, instance):
        def set_item(interval):
            self.weather.text = instance.text
            self.menu.dismiss()
        Clock.schedule_once(set_item, 0.5) 
class WindowManager(ScreenManager):
    pass
sm = WindowManager()    

class MainApp(MDApp):   
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        kv = Builder.load_file("FR.kv")
    def build(self):        
        screens = [AMRMenu(name = "menu"), GIWindow(name = "GI")]
        for screen in screens:
            sm.add_widget(screen)
        sm.current = "menu"
        return sm
if __name__ == "__main__":
    MainApp().run()

这是千伏。文件:

<AMRMenu>:
    name:"menu"
    BoxLayout:
        orientation: "vertical"
        MDToolbar:
            title: "Main Menu"
        MDList:
            OneLineListItem:
                text: "General Information"
                on_press: root.GIbutton()
            OneLineListItem:
                text: "Water Temperature"
            OneLineListItem
                text: "Feeding Amount"
            OneLineListItem: 
                text: "Residue and Feeding response"
            OneLineListItem: 
                text: "Dead fish"
            OneLineListItem:
                text: "Sell/Use"

<GIWindow>
    name: "GI"

    weather: weather
    ScrollView:
        id: screen
        MDBoxLayout:
            orientation: 'vertical'
            adaptive_height: True

            MDTextField:
                id: weather
                pos_hint: {'center_x': .5, 'center_y': .5}
                hint_text: "Weather"
                icon_right: "arrow-down-drop-circle-outline"
                input_filter: lambda text, from_undo: text[:5 - len(self.text)]
                on_focus: root.menu.open()

这是错误消息:

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/core/window/__init__.py", line 1297, in add_widget
 (widget, widget.parent)
 kivy.uix.widget.WidgetException: Cannot add <kivymd.uix.menu.MDDropdownMenu object at 0x7fd2fa31a6e0> to window, it already has a parent <kivy.core.window.window_sdl2.WindowSDL object at 0x7fd2f65826e0>

我不知道我为什么会发生这种情况。如果有人弄清楚了,它会很有帮助。感谢您阅读这个问题

4

1 回答 1

0

问题是您的kv线路:

on_focus: root.menu.open()

每次focus更改时都会打开菜单。因此,即使focus变为 false,它也会尝试打开菜单。一个简单的解决方法是在 True 时打开菜单focus is

on_focus: if self.focus: root.menu.open()
于 2020-08-07T13:40:34.497 回答