kivy for python:弹出窗口和上一页之间的通信



再一次摆弄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()

不确定要做什么,但修改selMus()方法以在CustPopup中使用sel可能会有所帮助:

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)

最新更新