这篇文章继续学习下文字滚动显示
1、文字滚动显示
截图是静态的,动态的请尝试运行代码
from tkinter import *
import time
root = Tk()
root.title("文字滚动效果")
root.geometry("320x240+100+100")
show_str = StringVar(root)
show_str.set("this")
source_str = "欢迎关注公众号<自学测试之道>,一起体验测试的味道!"
stopflag = True
pos = 0
def marquee(widget):
textwidth = 20
strlen = len(source_str)
global pos
if strlen - pos < 20:
show_str.set(source_str[pos:pos+textwidth] + source_str[0:20 - 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, bg='red', compound='center')
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="开始", command=startmarque)
button2 = Button(root, text="停止", command=stopmarquee)
button1.place(x=20, y=100, width=50, height=30)
button2.place(x=200, y=100, width=50, height=30)
root.mainloop()
2、获取系统当前实时时间
import time
import tkinter as tk
class App:
def __init__(self):
self.root = tk.Tk()
self.root.title("获取系统时间")
self.label = tk.Label(text="")
self.label.pack()
self.update_clock()
self.root.mainloop()
def update_clock(self):
now = time.strftime("%Y-%m-%d %H:%M:%S")
self.label.configure(text = now)
self.root.after(1000,self.update_clock)
app = App()
3、其他实时数据显示
https://blog.csdn.net/u013468614/article/details/58689735