python - 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()
解决方案
不确定您到底想做什么,但修改您selMus()
的方法以使用sel
in 您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)
推荐阅读
- python-3.x - Tkinter 窗口在一次运行后自动关闭
- python-3.x - 如何提取其母亲包含特定文本的元素?
- reactjs - router5 以编程方式导航浏览器历史记录
- javascript - 使用 Javascript 避免在 firebase 中重复
- javascript - cloudinary.v2.api.delete_folder 的替代方案是什么?由于不推荐使用此方法,我现在如何删除空文件夹?
- r - 用 n 个副本替换 data.frame 中的每个观察值
- akka - 了解在上游失败时如何处理 akka 引用
- python - 对 opencv 直播流应用多重效果
- kentico - Kentico:如何通过 API 处理内容分级?
- scala - 在 Spark 中的 groupby 之后跨列收集最常见的唯一值