当前位置: 代码迷 >> python >> 来自使用 python 和 kivy 的小部件类(例如下拉菜单)的值
  详细解决方案

来自使用 python 和 kivy 的小部件类(例如下拉菜单)的值

热度:44   发布时间:2023-07-16 10:49:53.0

我想使用在代码其他地方的下拉菜单中选择的值,特别是填充 MyData 类。

一些背景: 回答我最初关于如何让按钮工作的问题(谢谢)。 答案显示了如何在选择时打印值。 但是,除了on_release=lambda btn: print(btn.text)语句之外,我似乎无法访问或打印选择。

下面的代码有一些简单的打印语句来显示我试图跟踪的值。 上面的注释解释了输出。

我的主要问题是:

1) 为什么每次调用 MyPopup 时都会打印默认的 pos 值? (注意:我在调用 MyPopup 时传递的值会在之后立即打印。)

2) 如何从 MyColor 获得颜色选择?

3) 'self.drop.bind(on_select=lambda instance, x: setattr(self, 'text', x))' 到底做了什么? 如果它将 'self.text' 设置为选择,我如何访问该值(注意:'self.text' 不给我颜色)。

4) 如何将 pos 和 color 输入 MyData?

.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
from kivy.factory import Factory

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.rect.pos = touch.pos
        MyPopup(pos=touch.pos)


class MyPopup(Popup):
    def __init__(self, pos=[0,0]):
        super(MyPopup, self).__init__(pos=[0,0])
        self.pos = pos
        self.title = 'Select Color'
        self.color = MyColor.text
        # when MyPopup is called this first prints:
        #   from MyPopup [0, 0], <StringProperty name=text>
        # then immediately prints (for example)
        #   from MyPopup [758.0, 500.0], <StringProperty name=text>
        print('from MyPopup {}, {}'.format(self.pos, self.color))

    def save(self):
        # when save button is clicked this prints:
        #    from MyPopup.save [0, 0], <StringProperty name=text>
        print('from MyPopup.save {}, {}'.format(self.pos,self.color))
        MyData(self.pos, self.color)


class MyColor(Button):
    def __init__(self, **kwargs):
        super(MyColor, self).__init__(**kwargs)
        self.drop = DropDown()
        colorlist = ['black', 'red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink', 'white']
        for index in colorlist:
            btn = Button(text='%s' % index, size_hint_y=None, height=44)
            btn.bind(on_release=lambda btn: self.drop.select(btn.text))
            self.drop.add_widget(btn)
        self.bind(on_release=self.drop.open)
        # I thought this would 'setattr' of self.text to the selected value
        self.drop.bind(on_select=lambda instance, x: setattr(self, 'text', x))

class MyData:
    def __init__(self, pos,color):
        # this prints :
        #    from MyData [0, 0], <StringProperty name=text>
        print('from MyData {}, {}'.format(pos,color))

# using kivy screen for consistency
class MainWindow(Screen):
    pass
class WindowManager(ScreenManager):
    pass
kv = Builder.load_file("dropdown.kv")
class MyMainApp(App):
    def build(self):
        return kv

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

.kv:

#:import Factory kivy.factory.Factory

WindowManager:
    MainWindow:

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

<MyPopup>:
    text:'Enter Data'
    auto_dismiss: False
    FloatLayout:
        MyColor:
            text: 'Value'
            size_hint: 1,0.5
            pos_hint: {"x": 0,"top":1}
        Button:
            text: 'Save'
            size_hint: 1,0.5
            pos_hint: {"x": 0,"top":0.5}
            on_press: root.save()
            on_release: root.dismiss()

您可以使用 id 输入您的 subwidgets:

<MyPopup>:
    text:'Enter Data'
    auto_dismiss: False
    FloatLayout:
        MyColor:
            id: MyColor
            text: 'Value'
            size_hint: 1,0.5
            pos_hint: {"x": 0,"top":1}
        Button:
            id: Button1
            text: 'Save'
            size_hint: 1,0.5
            pos_hint: {"x": 0,"top":0.5}
            on_press: root.save()
            on_release: root.dismiss()

现在在您的 .py 文件中,您可以像这样访问小部件:

class MyPopup(Popup):
    def some_function(self):
        print(self.ids.Button1.text)
        print(self.ids.MyColor.size_hint)

替代语法:

print(self.ids["Button1"].text)