在 python 字典中的 tkinter 程序中使用 winsound

问题描述

我对代码进行了一些编辑,这个新代码是否能正常工作,如果不能,我应该怎么做才能使其正常工作

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'),'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
""",'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()

感谢所有为我的代码工作做出贡献的人我非常感谢我收到的所有帮助,我希望你们知道我会尝试每一个我提出的建议。我希望我能让我的代码最终工作,因为让它工作对我来说真的很重要

解决方法

在每个情况中添加一个 Private Sub Whole_Count_GotFocus() Dim Item_Filter As String DoCmd.OpenTable "item_Detail" Item_Filter = "[ItemId]=" & "'" & Me.Item.Value & "'" MsgBox (Item_Filter) ' just for testing need to delete Me.Units_in_UOM = DLookup("[QPC]","[Item_Detail]",Item_Filter) End Sub 项目,例如:

song

然后在 'situation_2': { 'story': ... 'song': 'situation_2.wav','buttons': [...] }, 中添加 song 参数并使用 Situation.__init__() 播放歌曲:

PlaySound()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...