温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python如何实现文本滚动播放器

发布时间:2021-04-26 14:50:21 来源:亿速云 阅读:301 作者:小新 栏目:开发技术

小编给大家分享一下Python如何实现文本滚动播放器,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

Python主要用来做什么

Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。

效果

Python如何实现文本滚动播放器

Python如何实现文本滚动播放器

双击开始播放,继续双击可以加速播放

右键可以弹出菜单:播放、暂停、退出

左键可以拖动窗口

代码

from tkinter import *
import time
 
import tkinter as tk
 
file = "待播放文本.txt"
text=" "
 
bgcolor = '#000000'
fgcolor = '#FFFFFF'
 
def getText():
    global text
    # 读
    with open(file, "r",encoding='utf-8') as f:
        # 按字节读
        text = f.read()    
#获取一行
getText()
root = Tk()
# 窗口设定为无边框
root.overrideredirect(True)
# 窗口前置
root.wm_attributes("-topmost", 1)
# 窗口属性 透明度设置
root.attributes("-alpha", 0.8)
# 窗口标题
# root.title("文本播放器")
# 窗口大小
root.geometry("200x35+100+100")
# 更新显示文本
show_str = StringVar(root)
# 初始显示文本
show_str.set("双击播放")
# 源字符
source_str = text
# 播放标记
playflag = True
 
# 播放位置
pos = 0
# 滚动
def marquee(widget):
    #字符宽度
    textwidth = 18
    # 源字符长度
    strlen = len(source_str)
    # 引用全局变量
    global pos
    # 如果字符长度-播放位置<textwidth
    if strlen - pos < textwidth:
        # 设定显示的字符串为源字符串的(播放位置,播放位置+文本宽度)+ 源字符串的(0,10-字符串长度+播放位置)
        show_str.set(source_str[pos:pos+textwidth] + source_str[0:textwidth - strlen + pos])
    else:
        # 如果大于textwidth,则播放(播放位置,播放位置+文本宽度)的字符
        show_str.set(source_str[pos:pos+textwidth])
    #播放位置+1
    pos += 1
    #如果播放位置大于字符串长度
    if pos > strlen:
        #播放位置设为0
        pos = 0
    # 引用全局变量
    global stopflag
    # 如果当前为播放状态
    if playflag:
        # 睡眠0.3秒后执行滚动函数
        widget.after(300, marquee, widget)
        
# 创建标签
show_lb = Label(root, textvariable=show_str,width=300, fg=fgcolor, bg=bgcolor, text=text, font=("Consolas", 10))
# 设定标签位置
show_lb.place(x=0, y=0, width=200, height=35)
 
def doubleClicktoPlay(event):
   global playflag
   # 播放
   playflag = True
   marquee(show_lb)
 
def playStart():
   global playflag
   # 播放
   playflag = True
   marquee(show_lb)
   
def playStop():
   global playflag
   # 暂停播放
   playflag = False
 
# 创建弹出式菜单
menu = tk.Menu(root, tearoff=0)
# 为菜单添加命令标签
menu.add_command(label="播放", command=playStart) 
menu.add_command(label="暂停", command=playStop)
menu.add_command(label="退出", command=exit)
 
def popUpMenu(event):
        #在鼠标点击的位置弹出菜单
        menu.post(event.x_root, event.y_root)
 
# 为消息事件(按键、点击)绑定函数
root.bind_all("<ButtonRelease-3>", popUpMenu) 
 
def moveStart(event):
    global startX, startY
    #获取鼠标的点击位置的x、y
    startX = event.x
    startY = event.y
 
def move(event):
     #新坐标=鼠标点击坐标+窗口坐标-初始坐标
    new_x = (event.x) + root.winfo_x() - startX
    new_y = (event.y) + root.winfo_y() - startY
    s = "200x35+" + str(new_x) + "+" + str(new_y)
    # 重新设置窗口大小及其位置
    root.geometry(s)
    
# 为消息事件(按键、点击)绑定函数
root.bind_all("<Button-1>", moveStart)  
root.bind_all("<B1-Motion>", move)
root.bind_all("<Double-Button-1>", doubleClicktoPlay) 
root.mainloop()

注:

如果文本有换行符,切换不会很流畅

可用此方法删除换行符

以上是“Python如何实现文本滚动播放器”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI