【问题标题】:Volume Slider using Tkinter使用 Tkinter 的音量滑块
【发布时间】:2016-12-30 23:05:37
【问题描述】:

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

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

我知道我没有使用最佳编码实践,但这样我可以真正理解我所写的内容。

【问题讨论】:

  • 嗯。也许this 可能会有所帮助。
  • 是的,这让我对滑块有了更多的了解,但我更担心如何在滑动时更改音量?
  • 也许command 选项是您需要的?这是一个简单的示例(您在移动滑块时获取 scale 的值):pastebin.com/S292a2VL

标签: python tkinter python-3.5


【解决方案1】:

在创建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() 半解决了我的问题: i = vol.get() player.audio_set_volume(i) vol = Scale(window,from_ = 0,to = 100,orient = HORIZONTAL ,分辨率 = 1) vol.grid(row = 1, column = 2) vol_but = Button(window, text='Update Volume', command=show_value) vol_but.grid(row = 2, column = 2)
【解决方案2】:

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

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多