在 python 的字典中的 tkinter 程序中使用 winsound
use winsound in a tkinter program from within a dictionary in python
I have edited the code a bit would this new code work and if not what should i do to get it to work
import winsound
import tkinter as tk
from functools import partial # a quick way to make a callback function
import choose_your_own_adventure_game_pt2 as AG
import time
winsound.PlaySound('Adrenaline - Intense Suspense Music (No Copyright).wav', winsound.SND_ASYNC)
print('welcome to the')
AG.title()
time.sleep(1)
print('please read each line carefuly once the gui launches')
time.sleep(13)
class Situation(tk.Frame):
def __init__(self, master=None, story='',song=[], buttons=[], **kwargs):
tk.Frame.__init__(self, master, **kwargs)
story_lbl = tk.Label(self, text=story, justify=tk.LEFT, anchor=tk.NW, font=("Play", 10))
story_lbl.pack()
for btn_text, new_situation in buttons:
btn = tk.Button(self, text=btn_text, command=partial(self.quit_, new_situation),padx=10, pady=10)
btn.pack()
def song_startup(self, new_sound):
winsound.PlaySound(btn_song_filename, winsound.SND_ASYNC|winsound.SND_FILENAME)
for btn_song_filename, new_sound in song:
btn_song = tk.Button(self, text=btn_song_filename, command=partial(self.song_startup, new_sound))
btn_song.pack()
def quit_(self, new_situation):
self.destroy()
load(new_situation)
def load(situation=None):
frame = Situation(root, **SITUATIONS.get(situation))
frame.pack()
SITUATIONS = {
None:{
'song':
'intro to the game.wav',
'story':
"""
hello player you have been chosen to embark on your first adventure,
this as a button based game so all your decisions are based on a press of a button,
you will play as a character named Sarah who has a wolf goddess inside her, the goddess' name is Winter,
you as Sarah will have the powers given to you by Winter to get through this game but if you use too much
of that power at any given time your game will end,
pretty soon you will be asked a series of questions your job is to choose the answer that best suits your style of playing the game
but be warned you may end up loosing in the end, so without furthur addo lets start this thing
welcome to
MOON LANDING EXPIDITION
male scientist:Hello Sarah can you hear me?
""",
'buttons':[
('YES', 'situation_2'),
('NO','situation_2_1')
]
},
'situation_2':{
'story':
"""
male scientist:good... do you know where you are?
""",
'song':[
('backgroundmusic.wav')
],
'buttons' :[
('yes', 'situation_3'),
('no', 'situation_3_1')
]
},
'situation_2_1':{
'story':
"""
male scientist: hmm if you can't hear me
then you wouldn't have responded
""",
'buttons':[
('-_-',None)
]
},
'situation_3':{
'story':
"""
male scientist:alright so do you know why you are here?
""",
'buttons':[
('yes','situation_4'),
('no','situation_4_1')
]
},
'situation_3_1':{
'story':
"""
male scientist: you are in a top secret military space center
""",
'buttons':[
('alright','situation_3')
]
},
'situation_4':{
'story':
"""
male scientist:well then you seem to have the rare ability to read people's minds
(a second scientist enters the room and starts talking to the first on the other side of the glass)
female scientist:Dr. Smith there has been sightings of foreign aircrafts approaching our location
Dr. Smith: alright Susan we need to transport Sarah to the space explorer
Susan:Yes Sir
(Susan looks in your direction)
Susan:Sarah will you please head to the door behind you and follow the yellow lines on the floor
""",
'buttons':[
('what is going on?','situation_5'),
('do not press', 'never_gonna_give_you_up'),
('am i in danger?','situation_5_1'),
('ok','situation_5_2')
],
},
'situation_4_1':{
'story':
"""
male scientist:you were chosen out of five million individuals to take part in a space exploration mission to the planet nexu...
(a second scientist enters the room and starts talking to the first on the other side of the glass)
female scientist:Dr. Smith there has been sightings of foreign aircrafts approaching our location
Dr. Smith: alright Susan we need to transport Sarah to the space explorer
Susan:Yes Sir
(Susan looks in your direction)
Susan:Sarah will you please head to the door behind you and follow the yellow lines on the floor
""",
'buttons':[
('what is going on?','situation_5'),
('am i in danger?','situation_5_1'),
('ok','situation_5_2')
],
},
'never_gonna_give_you_up':{
'story':
"""
were no strangers to love...
you know the rules and so do i...
iiiiiiiii just wanna tell you how im feeling
gotta make you UNDERSTAND!!!
never gonna give you up never gonna let you down
*rick astly.exe has sotpped working you will die now*
""",
'buttons':[
('death', None)
],
},
}
def beginning():
start_button.destroy()
load() # load the first story
winsound.PlaySound(None, winsound.SND_ASYNC)
#WINDOW
root = tk.Tk()
root.geometry('500x500-500-300')
root.title('Moon landing expedition')
root.attributes('-fullscreen', True)
#START
start_button = tk.Button(root, text="START", command=beginning)
start_button.place(relx=.5, rely=.5, anchor='c')
#THE LOOP
root.mainloop()
thank you to everyone who has contributed to help me get my code to work I really appreciate all the help I'm receiving and I would like you guys to know that I try every suggestion that comes my way. i hope i can get my code to finaly work as it is really important for me to get it to work
在每个情况中添加一个song
项目,例如:
'situation_2': {
'story': ...
'song': 'situation_2.wav',
'buttons': [...]
},
然后在Situation.__init__()
中添加song
参数并使用PlaySound()
播放歌曲:
class Situation(tk.Frame):
def __init__(self, master=None, story='', buttons=[], song=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
story_lbl = tk.Label(self, text=story, justify=tk.LEFT, anchor=tk.NW, font=("Play", 10))
story_lbl.pack()
for btn_text, new_situation in buttons:
btn = tk.Button(self, text=btn_text, command=partial(self.quit_, new_situation), padx=10, pady=10)
btn.pack()
if song:
winsound.PlaySound(song, winsound.SND_ASYNC)
I have edited the code a bit would this new code work and if not what should i do to get it to work
import winsound
import tkinter as tk
from functools import partial # a quick way to make a callback function
import choose_your_own_adventure_game_pt2 as AG
import time
winsound.PlaySound('Adrenaline - Intense Suspense Music (No Copyright).wav', winsound.SND_ASYNC)
print('welcome to the')
AG.title()
time.sleep(1)
print('please read each line carefuly once the gui launches')
time.sleep(13)
class Situation(tk.Frame):
def __init__(self, master=None, story='',song=[], buttons=[], **kwargs):
tk.Frame.__init__(self, master, **kwargs)
story_lbl = tk.Label(self, text=story, justify=tk.LEFT, anchor=tk.NW, font=("Play", 10))
story_lbl.pack()
for btn_text, new_situation in buttons:
btn = tk.Button(self, text=btn_text, command=partial(self.quit_, new_situation),padx=10, pady=10)
btn.pack()
def song_startup(self, new_sound):
winsound.PlaySound(btn_song_filename, winsound.SND_ASYNC|winsound.SND_FILENAME)
for btn_song_filename, new_sound in song:
btn_song = tk.Button(self, text=btn_song_filename, command=partial(self.song_startup, new_sound))
btn_song.pack()
def quit_(self, new_situation):
self.destroy()
load(new_situation)
def load(situation=None):
frame = Situation(root, **SITUATIONS.get(situation))
frame.pack()
SITUATIONS = {
None:{
'song':
'intro to the game.wav',
'story':
"""
hello player you have been chosen to embark on your first adventure,
this as a button based game so all your decisions are based on a press of a button,
you will play as a character named Sarah who has a wolf goddess inside her, the goddess' name is Winter,
you as Sarah will have the powers given to you by Winter to get through this game but if you use too much
of that power at any given time your game will end,
pretty soon you will be asked a series of questions your job is to choose the answer that best suits your style of playing the game
but be warned you may end up loosing in the end, so without furthur addo lets start this thing
welcome to
MOON LANDING EXPIDITION
male scientist:Hello Sarah can you hear me?
""",
'buttons':[
('YES', 'situation_2'),
('NO','situation_2_1')
]
},
'situation_2':{
'story':
"""
male scientist:good... do you know where you are?
""",
'song':[
('backgroundmusic.wav')
],
'buttons' :[
('yes', 'situation_3'),
('no', 'situation_3_1')
]
},
'situation_2_1':{
'story':
"""
male scientist: hmm if you can't hear me
then you wouldn't have responded
""",
'buttons':[
('-_-',None)
]
},
'situation_3':{
'story':
"""
male scientist:alright so do you know why you are here?
""",
'buttons':[
('yes','situation_4'),
('no','situation_4_1')
]
},
'situation_3_1':{
'story':
"""
male scientist: you are in a top secret military space center
""",
'buttons':[
('alright','situation_3')
]
},
'situation_4':{
'story':
"""
male scientist:well then you seem to have the rare ability to read people's minds
(a second scientist enters the room and starts talking to the first on the other side of the glass)
female scientist:Dr. Smith there has been sightings of foreign aircrafts approaching our location
Dr. Smith: alright Susan we need to transport Sarah to the space explorer
Susan:Yes Sir
(Susan looks in your direction)
Susan:Sarah will you please head to the door behind you and follow the yellow lines on the floor
""",
'buttons':[
('what is going on?','situation_5'),
('do not press', 'never_gonna_give_you_up'),
('am i in danger?','situation_5_1'),
('ok','situation_5_2')
],
},
'situation_4_1':{
'story':
"""
male scientist:you were chosen out of five million individuals to take part in a space exploration mission to the planet nexu...
(a second scientist enters the room and starts talking to the first on the other side of the glass)
female scientist:Dr. Smith there has been sightings of foreign aircrafts approaching our location
Dr. Smith: alright Susan we need to transport Sarah to the space explorer
Susan:Yes Sir
(Susan looks in your direction)
Susan:Sarah will you please head to the door behind you and follow the yellow lines on the floor
""",
'buttons':[
('what is going on?','situation_5'),
('am i in danger?','situation_5_1'),
('ok','situation_5_2')
],
},
'never_gonna_give_you_up':{
'story':
"""
were no strangers to love...
you know the rules and so do i...
iiiiiiiii just wanna tell you how im feeling
gotta make you UNDERSTAND!!!
never gonna give you up never gonna let you down
*rick astly.exe has sotpped working you will die now*
""",
'buttons':[
('death', None)
],
},
}
def beginning():
start_button.destroy()
load() # load the first story
winsound.PlaySound(None, winsound.SND_ASYNC)
#WINDOW
root = tk.Tk()
root.geometry('500x500-500-300')
root.title('Moon landing expedition')
root.attributes('-fullscreen', True)
#START
start_button = tk.Button(root, text="START", command=beginning)
start_button.place(relx=.5, rely=.5, anchor='c')
#THE LOOP
root.mainloop()
thank you to everyone who has contributed to help me get my code to work I really appreciate all the help I'm receiving and I would like you guys to know that I try every suggestion that comes my way. i hope i can get my code to finaly work as it is really important for me to get it to work
在每个情况中添加一个song
项目,例如:
'situation_2': {
'story': ...
'song': 'situation_2.wav',
'buttons': [...]
},
然后在Situation.__init__()
中添加song
参数并使用PlaySound()
播放歌曲:
class Situation(tk.Frame):
def __init__(self, master=None, story='', buttons=[], song=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
story_lbl = tk.Label(self, text=story, justify=tk.LEFT, anchor=tk.NW, font=("Play", 10))
story_lbl.pack()
for btn_text, new_situation in buttons:
btn = tk.Button(self, text=btn_text, command=partial(self.quit_, new_situation), padx=10, pady=10)
btn.pack()
if song:
winsound.PlaySound(song, winsound.SND_ASYNC)