【发布时间】:2023-03-09 18:57:01
【问题描述】:
这是我的 .py 文件的一部分。每次切换按钮时,我都希望 on_state 运行,但它不起作用。我把打印语句放入测试每个部分,它会打印“ONSTATE!!!”每次我切换按钮。但是“向下”或“正常”状态都不会打印出来。
class MusicScreen(Screen):
enter = ObjectProperty(None)
text_input = ObjectProperty(None)
stop = ObjectProperty(None)
musicbutton = ToggleButton()
class MainApp(App):
def build(self):
return presentation
def on_state(self, state, filename):
print("ONSTATE!!!")
sound = SoundLoader.load('/users/nolandonley/desktop/MP3/mus/' + filename + '.m4a')
if state == "down":
print("DOWN")
sound.volume = .5
sound.play()
if state == "normal":
print("normal")
sound.stop()
这是我的项目的 .kv 文件。
<MusicScreen>:
text_input: text_input
id: "music"
name: "music"
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
rootpath: "/Users/nolandonley/desktop/MP3/mus"
on_selection: text_input.text = self.selection and self.selection[0] or ''
TextInput:
id: text_input
size_hint_y: None
height: 50
multiline: False
ToggleButton:
#pos: 44, 30
size_hint: 1, .2
text: "Play/Stop By File"
ToggleButton:
id: musicbutton
#pos: 44, 30
size_hint: 1, .2
text: "Play/Stop By Title"
on_state: app.on_state(self, text_input.text)
【问题讨论】:
标签: python-3.x kivy togglebutton