Я хотел бы иметь возможность управлять громкостью звука с помощью шкалы Tkinter в моем коде ниже: Это mp3-плеер, который я делаю, и я использую для этого mp3play
. И это мой первый раз, когда мы имеем дело с чем-то подобным, поэтому, если вы, ребята, могли бы предложить лучший способ организации, тогда это было бы здорово.Сделайте Tkinter Scale Control Sound Volume?
спасибо.
Мой код:
# -*- coding: utf-8 -*-
from Tkinter import *
import os, sys, mp3play, Tkinter, tkMessageBox, time
Music = os.listdir(os.path.join(os.path.expanduser('~'), 'Desktop', 'NarutoLibMin'))
MusicPath = os.path.join(os.path.expanduser('~'),'Desktop', 'NarutoLibMin')
count = ''
mp3 = ''
VolumeSlider = ''
print Music
def Core():
global mp3; global count; global VolumeSlider
count = 0
root = Tkinter.Tk()
root.option_add('*Font', 'courier 12')
root.option_add('*Background', 'light blue')
root.configure(bg='light blue')
mp3 = mp3play.load(os.path.join(MusicPath, Music[count]))
def centerRoot(w = 729, h = 170):
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
def PlayNextSongAuto():
global mp3; global count; global root
track = 0
while track < mp3.seconds():
root.after(3600)
track += 1
count = 0
mp3 = mp3play.load(os.path.join(MusicPath, Music[count]))
mp3.play()
def ForwardSong():
global mp3; global count; global Music
Stop = len(Music) - 2
print Stop
if count > Stop:
print 'End of Play List'
count = 0
raw_input('')
Quit()
StopButton()
count += 1
mp3 = mp3play.load(os.path.join(MusicPath, Music[count]))
mp3.play()
def BackwardSong():
global mp3; global count
# if count > -3:
# print 'End of Play List'
# Quit()
StopButton()
count -= 1
mp3 = mp3play.load(os.path.join(MusicPath, Music[count]))
mp3.play()
def PlayButton():
global count
mp3.play()
Tkinter.Button(root, height=2, width=10, text='║║', borderwidth=10,command=PauseButton).grid(row=0,column=2)
def PauseButton():
Tkinter.Button(root, height=2, width=10, text='Unpause', borderwidth=10,command=UnPauseButton).grid(row=0,column=2)
if mp3.isplaying() == True:
mp3.pause()
def UnPauseButton():
Tkinter.Button(root, height=2, width=10, text='║║', borderwidth=10,command=PauseButton).grid(row=0,column=2)
if mp3.ispaused() == True:
mp3.unpause()
def StopButton():
mp3.stop()
Tkinter.Button(root, height=2, width=10, text='║║', borderwidth=10,command=PauseButton).grid(row=0,column=2)
def Quit():
StopButton()
sys.exit('')
def VolAdj(val):
mp3.volume(val)
Tkinter.Button(root, height=2, width=10, text='◄◄', borderwidth=10,command=BackwardSong, fg = 'black', bg='light blue').grid(row=0,column=0)
Tkinter.Button(root, height=2, width=10, text='►', borderwidth=10,command=PlayButton, fg = 'black', bg='light blue').grid(row=0,column=1)
Tkinter.Button(root, height=2, width=10, text='║║', borderwidth=10,command=PauseButton, fg = 'black', bg='light blue').grid(row=0,column=2)
Tkinter.Button(root, height=2, width=10, text='■', borderwidth=10,command=StopButton, fg = 'black', bg='light blue').grid(row=0,column=3)
Tkinter.Button(root, height=2, width=10, text='►►', borderwidth=10,command=ForwardSong, fg = 'black', bg='light blue').grid(row=0,column=4)
Tkinter.Button(root, height=2, width=10, text='Quit', borderwidth=10,command=Quit, fg = 'black', bg='light blue').grid(row=1,column=1)
VolumeSlider = Tkinter.Scale(root, length = 140, label=' Volume ', orient = 'horizontal', fg = 'black', bg='light blue', command = VolAdj).grid(row=1, column=2)
centerRoot()
root.title('Music Player')
root.mainloop()
Core()
Сделайте класс Core(), поместите глобальную переменную в __init __ (self) и используйте self. (для exmple self.root и т. д.) – furas