使用 on_touch_up 从下拉菜单选项实例化数据 class

Instantiate data class from dropdown menu options using on_touch_up

我想用从下拉菜单中收集的信息填充 MyData,这些信息显示在 AddTouch 中 window 和 "on_touch_up" 的弹出窗口中。除了下拉数据外,该数据还包括 "on_touch_up" 的位置。我能够打印 AddTouch class 中的位置,但我很难使用(例如:print('from MyMainApp: {}'.format(MyData.pos)))。

我也无法让 "mainbutton" 或 "dropdown" 出现在弹出窗口 window 中。

this 一起胡思乱想,我想出了以下可行的方法,但没有满足我的需要

.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.dropdown import DropDown
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.graphics import Color, Rectangle

class AddTouch(Widget):
    def __init__(self, **kwargs):
        super(AddTouch, self).__init__(**kwargs)
        with self.canvas:
            Color(1, 0, 0, 0.5, mode="rgba")
            self.rect = Rectangle(pos=(0, 0), size=(10, 10))
    def on_touch_down(self, touch):
        self.rect.pos = touch.pos
    def on_touch_move(self, touch):
        self.rect.pos = touch.pos
    def on_touch_up(self, touch):
        # final position
        self.pos = touch.pos
        print(self.pos)

class MyPopup(Popup):
    def __init__(self, **kwargs):
        super(MyPopup, self).__init__(**kwargs)
        # create a main button
        self.mainbutton = Button(text='Hello', size_hint=(None, None))
        # create a dropdown with 10 buttons
        self.dropdown = DropDown()
        for index in range(10):
            btn = Button(text='Value %d' % index, size_hint_y=None, height=44)
            btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
            self.dropdown.add_widget(btn)
        self.mainbutton.bind(on_release=self.dropdown.open)
        self.dropdown.bind(on_select=lambda instance, x: setattr(self.mainbutton, 'text', x))

class MyData:
    def __init__(self, **kwargs):
        super(MyData, self).__init__(**kwargs)
        self.pos=AddTouch.pos

# using kivy screen for consistency
class MainWindow(Screen):
    pass   
class WindowManager(ScreenManager):
    pass    
kv = Builder.load_file("dropd.kv")
class MyMainApp(App):
    def build(self):
        return kv
        print('from MyMainApp: {}'.format(MyData.pos))

if __name__ == "__main__":
    MyMainApp().run()

.kv

WindowManager:
    MainWindow:

<MainWindow>:
    name: "main"
    AddTouch:
        on_touch_up:
            #MyPopup gives 'MyPopup' is not defined, even if I add <MyPopup>: below
            #root.MyPopup gives 'MainWindow' object has no attribute 'MyPopup'

我尝试在基于 this 的 .kv 文件中添加一个简单的动态 Popup class,但它再次显示 'MyPopup' 未定义:

.kv

AddTouch
    on_touch_up:
        MyPopup

<MyPopup@Popup>:
    auto_dismiss: False
    Button:
        text: 'Close me!'
        on_release: root.dismiss()

我还缺少什么(除了经验、能力和一般智力)?

除了添加行:

self.content = self.mainbutton

MyPopup __init__() 方法,您可以通过修改 .kv 文件来触发 MyPopup 创建:

#:import Factory kivy.factory.Factory

WindowManager:
    MainWindow:

<MainWindow>:
    name: "main"
    AddTouch:
        on_touch_up:
            Factory.MyPopup().open()