首页 > 解决方案 > python的kivy:弹出窗口和上一页之间的通信

问题描述

再次摆弄kivy并遇到问题。

如何将变量(来自微调器的文本)传递到弹出按钮(它有两个并且必须定制)或将所述变量传递到另一个页面?我需要这个来在钢琴上播放一首歌曲,其他脚本大部分都已经完成并且正在工作。

在此先感谢,这是弹出窗口的代码:


<Custpopup@Popup>:
    size_hint: .8, .8
    auto_dismiss: False
    title: 'Play me a song'

    BoxLayout:
        spacing: 10
        Button:
            id: play
            text: 'Play'
            on_press:
                # what to do here?
        Button:
            id: close
            text: 'Close popup'
            on_press:
                root.dismiss()
                print('closed')

编辑:这里最小的可重现示例:https ://pastebin.com/y7sW8ByH

from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.app import App
from os import listdir

DEBUG = False

Builder.load_string('''

<Custpopup@Popup>:
    size_hint: .7, .6
    auto_dismiss: False
    title: 'Play me a song'

    BoxLayout:
        spacing: 10
        Button:
            id: play
            text: 'Play'
            on_press:
                # what to do here?
        Button:
            id: close
            text: 'Close popup'
            on_press:
                root.dismiss()
                print('closed')

<JukeBoxMode>:
    name: 'juke'
    id: jukebox

    FloatLayout:
        id: layout

        Button:
            id: back
            text: 'Return'
            size_hint: None, None
            size: 150, 44
            pos: 0, layout.height - self.height
            on_press: print('Pressed RETURN button')
            on_release:
                print(app.root.current)

        Spinner:
            id: spin
            size_hint: None, None
            size: 400, 44
            pos: (layout.center_x - self.width/2, layout.height - self.height)
            text: 'Music'
            values: root.musicList
            on_text:
                root.selMus(self.text)

''')

class CustPopup(Popup):
    pass


class JukeBoxMode(Screen):

    musicList = []
    musdir = r'/home/pi/Desktop/Music21/midi/'
    muslist = listdir(musdir)
    for file in muslist:
        if file.endswith('.mid'):
            musicList.append(file[:-4])

    if DEBUG:
        print(musicList)

    musicList.sort()

    if DEBUG:
        print(musicList)

    def selMus(self,sel):

        custpop = CustPopup()

        if sel != 'Music':
            custpop.open()

    def playPiano(self):

        dicmusic = {self.musicList.index(self.ids['spin'].text): self.ids['spin'].text}

        if DEBUG:
            print(dicmusic)

        song, fin, instr, seq = infoMorceau(dicmusic[0])

        print(song, fin, instr, seq)


class HudPianoApp(App):
    def build(self):
        return JukeBoxMode()


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

标签: pythonkivy

解决方案


不确定您到底想做什么,但修改您selMus()的方法以使用selin 您CustPopup可能会有所帮助:

def selMus(self,sel):
    print(sel)

    custpop = CustPopup()

    if sel != 'Music':
        # Use 'sel' in the Popup
        custpop.ids.play.text = 'Play ' + sel
        custpop.ids.play.on_release = partial(self.do_something, sel)
        custpop.open()

def do_something(self, sel):
    # Do whatever you want with `sel` here
    print('do something with', sel)

推荐阅读