python tkinter 滚动字幕


from tkinter import *
import time
root = Tk()
root.title("滚动字幕")
root.geometry("320x240+100+100")
show_str = StringVar(root)
show_str.set("")
source_str = "突破自己,努力加油!好好学习,天天向上!"
stopflag = True

pos = 0
def marquee(widget):
   textwidth = 10
   strlen = len(source_str)
   global pos

   if strlen - pos < 10:
       show_str.set(source_str[pos:pos+textwidth] + source_str[0:10 - strlen + pos])
   else:
       show_str.set(source_str[pos:pos+textwidth])
   pos += 1

   if pos > strlen:
       pos = 0

   global stopflag

   if stopflag:
       widget.after(300, marquee, widget)

show_lb = Label(root, textvariable=show_str)

show_lb.place(x=20, y=20, width=200, height=30)

def startmarque():
   global stopflag
   stopflag = True
   marquee(show_lb)
def stopmarquee():
   global stopflag
   stopflag = False
   
button1 = Button(root, text="start", command=startmarque)
button2 = Button(root, text="stop", command=stopmarquee)
button1.place(x=20, y=100, width=50, height=30)
button2.place(x=200, y=100, width=50, height=30)
root.mainloop()


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM