from tkinter import *
import pygame.mixer
app = Tk()
app.title("Head First Mix")
sound_file = "50459_M_RED_Nephlimizer.wav"
mixer = pygame.mixer
mixer.init()
def track_start():
track.play(loops = -1)
def track_stop():
track.stop()
def shutdown():
track.stop()
app.destroy()
def track_toogle():
if track_playing.get() == 1:
track_start()
else:
track_stop()
def change_volume(v):
track.set_volume(volume.get())
track_playing = IntVar()
track = mixer.Sound(sound_file)
track_button = Checkbutton(app,
variable = track_playing,
command = track_toogle,
text = "50459_M_RED_Nephlimizer.wav")
track_button.pack(side = LEFT)
volume = DoubleVar()
volume.set(track.get_volume())
volume_scale = Scale(app,
variable = volume,
from_ = 0.0,
to = 1.0,
resolution = 0.1,
command = change_volume,
label = "Volume",
orient = HORIZONTAL)
volume_scale.pack(side = RIGHT)
app.protocol("WM_DELETE_WINDOW", shutdown)
app.mainloop()
目前我读了一本名为head first python的书。在我达到第9章之前,我一直在支持所有内容。如果我没有在change_volume函数括号中放置某些内容,为什么我的应用程序不起作用
为什么我需要“v” - def change_volume(v):
让你停下来,开始并调整音轨btw的音量
答案 0 :(得分:0)
代码
def change_volume(v):
track.set_volume(volume.get())
编写为需要参数v
。但是,如上所述,它实际上从未使用过该参数。相反,由于您在代码中的其他位置调用了volume = track.get_volume()
,除非您在其他位置修改volume
(或音轨的音量值),否则您的代码实际上等同于
def change_volume(v):
track.set_volume(track.get_volume())
如果要实际更改值,则需要使用参数值来设置音量:
def change_volume(v):
track.set_volume(v.get())