使用on_touch_up从下拉菜单选项实例化数据类 [英] Instantiate data class from dropdown menu options using on_touch_up

查看:64
本文介绍了使用on_touch_up从下拉菜单选项实例化数据类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用从下拉菜单中收集的信息填充MyData,这些下拉菜单在弹出式窗口中显示,并在AddTouch中显示为"on_touch_up".除了下拉数据之外,该数据还包括"on_touch_up"的位置.我可以打印AddTouch类中的位置,但是我很难使用(例如:print('from MyMainApp:{}'.format(MyData.pos)))在脚本中进一步获取数据. .

I would like to populate MyData with information gathered from dropdown menus that show up in a popup window with "on_touch_up" in AddTouch. That data includes the position of "on_touch_up", in addition to the dropdown data. I am able to print the position within the AddTouch class, but I am having a hard time getting the data further down in my script using (for example: print('from MyMainApp: {}'.format(MyData.pos))).

我也无法在弹出窗口中显示"mainbutton"或"dropdown".

I am also unable to get "mainbutton" or "dropdown" to show up in a popup window.

随意使用,我想到了以下哪个有效,但不能满足我的需求

Hacking around with this I came up with the following which works, but doesn't do what i need

.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'

我尝试根据

I tried adding a simple dynamic Popup class in the .kv file based on this, but again it says 'MyPopup' is not defined:

.kv

AddTouch
    on_touch_up:
        MyPopup

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

我缺少什么(经验,能力和一般智力除外)?

What am I missing (other than experience, ability, and general intelligence)?

推荐答案

除了添加以下行:

self.content = self.mainbutton

对于MyPopup __init__()方法,可以通过将.kv文件修改为以下方式来触发MyPopup创建:

to the MyPopup __init__() method, you can trigger the MyPopup creation by modifying your .kv file as:

#:import Factory kivy.factory.Factory

WindowManager:
    MainWindow:

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

这篇关于使用on_touch_up从下拉菜单选项实例化数据类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆