制作Tkinter音阶控制音量?

时间:2013-11-18 04:03:43

标签: python-2.7 tkinter

我希望能够在下面的代码中使用Tkinter Scale控制音量:这是我正在制作的MP3播放器,我正在使用mp3play。 这也是我第一次处理这样的事情,因此,如果你们可以提出更好的组织方式,那就太棒了。

谢谢。

我的代码:

# -*- 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()

1 个答案:

答案 0 :(得分:3)

您的代码可能如下所示:

# -*- coding: utf-8 -*-

import Tkinter

import os
import sys
import mp3play

class Core():

    def __init__(self):

        self.Music = os.listdir(os.path.join(os.path.expanduser('~'), 'Desktop', 'NarutoLibMin'))
        self.MusicPath = os.path.join(os.path.expanduser('~'),'Desktop', 'NarutoLibMin')
        print self.Music

        self.count = ''
        self.mp3 = ''
        self.VolumeSlider = ''
        self.count = 0

        self.createRoot()
        self.createButtons()

        self.LoadSong(False)

    #---------------------------------------------

    def createRoot(self, w = 729, h = 170):

        self.root = Tkinter.Tk()
        self.root.option_add('*Font', 'courier 12')
        self.root.option_add('*Background', 'light blue')
        self.root.configure(bg='light blue')

        ws = self.root.winfo_screenwidth()
        hs = self.root.winfo_screenheight()
        x = (ws/2) - (w/2)    
        y = (hs/2) - (h/2)
        self.root.geometry('%dx%d+%d+%d' % (w, h, x, y))

    #---------------------------------------------

    def createButtons(self):

        Tkinter.Button(self.root, height=2, width=10, text='◄◄', borderwidth=10,command=self.BackwardSong, fg = 'black', bg='light blue').grid(row=0,column=0)
        Tkinter.Button(self.root, height=2, width=10, text='►', borderwidth=10,command=self.PlayButton, fg = 'black', bg='light blue').grid(row=0,column=1)

        self.buttonPause = Tkinter.Button(self.root, height=2, width=10, text='║║', borderwidth=10,command=self.PauseButton, fg = 'black', bg='light blue')
        self.buttonPause.grid(row=0,column=2)

        Tkinter.Button(self.root, height=2, width=10, text='■', borderwidth=10,command=self.StopButton, fg = 'black', bg='light blue').grid(row=0,column=3)
        Tkinter.Button(self.root, height=2, width=10, text='►►', borderwidth=10,command=self.ForwardSong, fg = 'black', bg='light blue').grid(row=0,column=4)
        Tkinter.Button(self.root, height=2, width=10, text='Quit', borderwidth=10,command=self.Quit, fg = 'black', bg='light blue').grid(row=1,column=1)
        self.VolumeSlider = Tkinter.Scale(self.root, length = 140, label='  Volume ', orient = 'horizontal', fg = 'black', bg='light blue', command = self.VolAdj).grid(row=1, column=2)    

    #---------------------------------------------

    def LoadSong(self, play=True):
        self.mp3 = self.mp3play.load(os.path.join(self.MusicPath, self.Music[self.count]))
        if play:
            self.mp3.play()  

    #---------------------------------------------

    def PlayNextSongAuto(self):

        track = 0
        while track < self.mp3.seconds():
            self.root.after(3600)
            track += 1
        self.count = 0
        self.LoadSong()  

    #---------------------------------------------

    def ForwardSong(self):
        Stop = len(self.Music) - 2
        print Stop
        if self.count > Stop:
            print 'End of Play List'
            self.count = 0
            raw_input('')
            self.Quit()    
        self.StopButton()        
        self.count += 1
        self.LoadSong()  

    #---------------------------------------------

    def BackwardSong(self):
#        if self.count > -3:            
#            print 'End of Play List'
#            self.Quit()        
        self.StopButton()
        self.count -= 1
        self.LoadSong()  

    #---------------------------------------------

    def PlayButton(self):
        self.buttonPause.configure(text = '║║', command=self.PauseButton)
        self.mp3.play()

    #---------------------------------------------

    def PauseButton(self):
        self.buttonPause.configure(text = 'Unpause', command=self.UnPauseButton)
        if self.mp3.isplaying():
            self.mp3.pause()

    #---------------------------------------------

    def UnPauseButton(self):
        self.buttonPause.configure(text = '║║', command=self.PauseButton)
        if self.mp3.ispaused():
            self.mp3.unpause() 
    #---------------------------------------------

    def StopButton(self):
        self.buttonPause.configure(text = '║║', command=self.PauseButton)
        self.mp3.stop()

    #---------------------------------------------

    def Quit(self):
        self.StopButton()
        sys.exit('')

    #---------------------------------------------

    def VolAdj(self, val):
        self.mp3.volume(val)

    #---------------------------------------------

    def Run(self):
        self.root.mainloop()

#---------------------------------------------------------------------

Core().Run()

我无法测试它,因为我使用Linux和mp3play(可能)仅适用于Windows。