使用Tkinter的Volume Slider

时间:2016-08-24 00:33:28

标签: python tkinter python-3.5

我是编码的新手,我正在尝试为学校项目创建一个自动点唱机,但我正在努力创建一个可以编辑音量的滑块。我只是不确定在移动滑块时从哪里开始让音量实际变化。 我正在使用VLC lib。

import vlc
import random
from tkinter import *
import threading



song = ""
instance = vlc.Instance()

def get_songs():
    global song
    global x
    global songs
    songs = filedialog.askopenfilenames()
    x = 0
    song = songs[x]
    print(songs)
    commence(song)

def pause_resume():
    player.pause()

def commence(song):
    global player
    global x
    player = instance.media_player_new()
    media = instance.media_new(song)
    player.set_media(media)
    player.play()


def next_song():
    if x >= len(songs):
        print("Error: Can't go any further")
        x = 0
        return
    player.stop()
    song = songs[x]
    commence(song)







window = Tk()

window.geometry("600x600")
window.title('JukeBox')

#pause_button = Button(window, text = "Next", command = next_song)
#pause_button.grid(row=1, column = 2)
Button(window, text="Start", command=get_songs).grid(column=1,row=1)
Button(window, text="Next", command=next_song).grid(column=1,row=2)
pause_button = Button(window, text = "Pause/Resume", command = pause_resume)
pause_button.grid(row=3, column = 1)
menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_separator()
filemenu.add_command(label="Open", command=get_songs())
filemenu.add_command(label="Exit", command=window.destroy)
menubar.add_cascade(label="File", menu=filemenu)
window.config(menu=menubar)
vol = Scale(window,from_ = 0,to = 1,orient = HORIZONTAL ,resolution = .1,)
vol.grid(row = 1, column = 2)




window.mainloop()

我理解我没有使用最好的编码方法,但这样我才能真正理解我写的内容。

2 个答案:

答案 0 :(得分:0)

创建command小部件时设置Scale参数:

def set_volume(v):
    global vol
    global player
    # either get the new volume from given argument v (type: str):
    # value = int(v)
    # or get it directly from Scale widget (type: int)
    value = vol.get()
    player.audio_set_volume(value)

vol = Scale(..., command=set_volume)

答案 1 :(得分:0)

我的伙伴能够通过简单地在功能所需的参数中添加自我来帮助我,不知道为什么它有帮助并感谢大家的尝试,非常感谢。

def show_value(self):
    global player
    i = vol.get()
    player.audio_set_volume(i)

vol = Scale(window,from_ = 0,to = 100,orient = HORIZONTAL ,resolution = 1,command = show_value) 
    vol.place(x=75, y = 300)
    vol.set(50)