【问题标题】:Kivy: pass data to another class popupKivy:将数据传递给另一个类弹出窗口
【发布时间】:2018-01-21 13:02:52
【问题描述】:

我正在尝试使用 Kivy(1.10) python 3.4.4 制作一个简单的 GUI,并使用弹出窗口显示另一个类的信息。但是当我使用函数 init 将变量从一个类(主体)传递到使用 popup(Seleccionador) 构造的类时遇到问题

使用该功能时崩溃 (abre_seleccionador)

main.py

import kivy
kivy.require('1.10.0')

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.config import Config

Config.set('graphics','width', 800)
Config.set('graphics','height',500)

class CustomPopup(Popup):
    pass

class Seleccionador(Popup):

    def __init__(self,idea):
        super().__init__()
        self.idea_texto =idea

class Principal(BoxLayout):
    def abre_popup(self):

        the_popup =CustomPopup()
        the_popup.open()

    def abre_seleccionador(self):
        idea ='idea brillante'
        popin= Seleccionador(idea)
        popin.open()

class KvpopApp(App):
    title = 'Pruebas de pop up y filechooser'
    def build(self):
        return Principal()

if __name__ == '__main__':
    KvpopApp().run()

kv 文件:

<Principal>:
    orientation:'vertical'
    canvas:
        Color:
            rgb: 1,1,1,
        Rectangle:
            pos: self.pos
            size: self.size
    Button:
        text: 'Abrir popup'
        on_press: root.abre_popup()
    Button:
        text: 'Abre seleccionador'
        on_press: root.abre_seleccionador()

<CustomPopup>:
    size_hint: 0.5,0.5
    auto_dismiss: False
    title: "Este es mi Popup"
    Button:
        text: 'Cerrar popup'
        on_press: root.dismiss()

<Seleccionador>:
    size_hint: 0.5,0.5
    auto_dismiss: False
    title: "Este es mi Popup"
    id: select
    BoxLayout:
        Button:
            text: 'Cerrar'
            on_press: root.dismiss()
        Label:
            id: idea
            text: select.idea_texto

【问题讨论】:

    标签: python python-3.x popup kivy


    【解决方案1】:

    Kv 语言规则在类初始化之前(在__init__ 方法执行之前)进行评估,因此此时实例属性不存在。最简单的就是你使用StringProperty

    from kivy.app import App
    from kivy.config import Config
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.popup import Popup
    from kivy.properties import StringProperty
    
    
    Config.set('graphics', 'width', 800)
    Config.set('graphics', 'height',500)
    
    
    class CustomPopup(Popup):
        pass
    
    
    class Seleccionador(Popup):
        idea_texto = StringProperty()    # <<<<<<<<<<<<<<<<<<<<
    
        def __init__(self, idea, **kwargs):
            super(Seleccionador, self).__init__(**kwargs)
            self.idea_texto = idea
    
    class Principal(BoxLayout):
        def abre_popup(self):
            the_popup = CustomPopup()
            the_popup.open()
    
        def abre_seleccionador(self):
            idea = 'idea brillante'
            popin = Seleccionador(idea)
            popin.open()
    
    
    class KvpopApp(App):
        title = 'Pruebas de pop up y filechooser'
    
        def build(self):
            return Principal()
    
    
    if __name__ == '__main__':
        KvpopApp().run()
    

    【讨论】:

      猜你喜欢
      • 2017-08-13
      • 2019-04-14
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2021-04-20
      • 2014-03-14
      相关资源
      最近更新 更多