[英]Volume Slider using Tkinter
我是編碼的新手,我正在嘗試為學校項目創建一個自動點唱機,但我很難創建一個可以編輯音量的滑塊。 我只是不確定在移動滑塊時從哪里開始實際改變音量。 我正在使用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()
我知道我沒有使用最好的編碼實踐,但這樣我才能真正理解我所寫的內容。
創建“ Scale
小部件時設置command
參數:
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)
我的伙伴能夠通過簡單地在功能所需的參數中添加自我來幫助我,不知道為什么它有幫助並感謝大家的嘗試,非常感謝。
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.