python 网络音乐播放器(二):tkinter 实现歌词同步滚动

问题

在我用 tkinter 做网络音乐播放器时,想实现滚动歌词的效果。如下图所示:
在这里插入图片描述

解决方案

我们采用 tkinter 的 Listbox 来实现滚动歌词效果。

lrc_list = Listbox(lrc_frame, 
                   relief='flat',	# 无边框
                   font=('微软雅黑', 12),	# 设置字体
                   highlightthickness=0,# 无高亮边框
                   selectmode='single',	# 单选
                
  • 9
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
以下是一个简单的实现歌词同步滚动Tkinter代码,你可以根据自己的需求进行修改: ```python import tkinter as tk class Lyric(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.pack() self.create_widgets() def create_widgets(self): self.lyric_text = tk.Text(self, height=10, width=50) self.lyric_text.pack() self.scrollbar = tk.Scrollbar(self) self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y) self.lyric_text.config(yscrollcommand=self.scrollbar.set) self.scrollbar.config(command=self.lyric_text.yview) self.lyric_text.bind("<Configure>", self.on_lyric_configure) self.lyrics = [ "Verse 1", "I'm waking up to ash and dust", "I wipe my brow and sweat my rust", "I'm breathing in the chemicals", "", "Pre-Chorus", "I'm breaking in, shaping up, then checking out on the prison bus", "This is it, the apocalypse, whoa", "", "Chorus", "I'm waking up, I feel it in my bones", "Enough to make my systems blow", "Welcome to the new age, to the new age", "Welcome to the new age, to the new age", "Whoa, whoa, I'm radioactive, radioactive", "Whoa, whoa, I'm radioactive, radioactive", "", "Verse 2", "I raise my flags, don my clothes", "It's a revolution, I suppose", "We'll paint it red to fit right in", "", "Pre-Chorus", "I'm breaking in, shaping up, then checking out on the prison bus", "This is it, the apocalypse, whoa", "", "Chorus", "I'm waking up, I feel it in my bones", "Enough to make my systems blow", "Welcome to the new age, to the new age", "Welcome to the new age, to the new age", "Whoa, whoa, I'm radioactive, radioactive", "Whoa, whoa, I'm radioactive, radioactive", ] def on_lyric_configure(self, event): self.lyric_text.delete('1.0', tk.END) for i, line in enumerate(self.lyrics): self.lyric_text.insert(tk.END, line + '\n') if i % 4 == 0: self.lyric_text.insert(tk.END, '\n') if __name__ == '__main__': root = tk.Tk() root.geometry('300x200') app = Lyric(root) app.mainloop() ``` 这个代码创建了一个包含歌词Text 组件,并且使用 Scrollbar 组件来实现滚动。在 `on_lyric_configure` 方法中,我们将歌词逐行写入 Text 组件,并且在每四行歌词之间添加一个行。你可以根据自己的需求来修改这个方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值