(上下文)我正在创建一个游戏,其中涉及通过选择python从音频文件列表中随机生成的音符来重新创建旋律。我在Tkinter的回调函数中检索和使用信息时遇到麻烦。相关代码如下:
def PlayRandomNote(self):
NoteIndex = random.randint(0, 6)
Note = ListOfSamples[NoteIndex]
self.PlayFile(Note)
return Note
def Screen3(self,Melody):
GuessedMelody = []
self.PlayNote = ttk.Button(self.Main, text = "Play random note", command = self.PlayRandomNote)
self.Question = Label(self.Main, text = "Was that the correct note?", bg = "black", fg = "white")
self.Yes = ttk.Button (self.Main, text = "Yes", command = lambda: self.ThreeToFour(Melody, GuessedMelody))
for Widget in self.Main.winfo_children():
Widget.pack()
((问题)我想这样,当用户按下“是”按钮时,将通过按下“播放随机音符”按钮播放的最后一个音符添加到我的GuessedMelody列表中,然后将该列表添加为我的ThreeToFour函数的参数(该函数将从当前屏幕转换到下一个屏幕)。有办法吗?
否,而不是传递所有内容,您必须使用对象来保存信息。下面的代码是对您的目标的粗略猜测。没有其他代码,很难说。传递似乎更有效,但是它不起作用,在python中没有必要。例如,您在PlayRandomNote(self)中返回了音符...它没有处可走,因此您必须准备好一个变量并将其分配给该变量。
def PlayRandomNote(self):
NoteIndex = random.randint(0, 6)
Note = ListOfSamples[NoteIndex]
self.PlayFile(Note)
self.curent_random_note
def Screen3(self):
self.PlayNote = ttk.Button(self.Main, text = "Play random note", command = self.PlayRandomNote)
self.Question = Label(self.Main, text = "Was that the correct note?", bg = "black", fg = "white")
self.Yes = ttk.Button (self.Main, text = "Yes", command = self.ThreeToFour)
for Widget in self.Main.winfo_children():
Widget.pack()