python - Tkinter 动画标签文本作为 html 品牌
问题描述
我想要一个文本首先移动到东方然后移动到西方等等......我尝试的代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tkinter as tk
root = tk.Tk(className='Papinhio player')
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))
#now playing bar
now_playing = tk.Label(root,text="Some text",anchor="w")
now_playing.grid(row=0,column=0,columnspan=20,padx=5,pady=5,ipadx=10,ipady=10)
#now_playing.config(width=root.winfo_screenwidth()-32)
global empty_characters
global now_playing_state
now_playing_state = "right"
empty_characters = ""
def animate_now_playing():
print("Running!")
text = "Some text"
global empty_characters
global now_playing_state
current_width = now_playing.winfo_width()
available_width = root.winfo_screenwidth()
print("Current width: "+str(current_width))
print("Available width: "+str(available_width))
if(now_playing_state=="right"):
if(current_width<available_width):
empty_characters+= " "
else:
now_playing_state="left"
if(now_playing_state=="left"):
if(len(empty_characters)!=0):
empty_characters = empty_characters[:-1]
else:
now_playing_state="right"
now_playing.config(text=empty_characters+text)
root.after(40, animate_now_playing)
# display the menu
#root.config(menu=menubar)
#root.grid_columnconfigure(0, weight=1)
root.after(100, animate_now_playing)
root.mainloop()
代码编辑:
文本正在向东移动,然后向西移动,但现在它是一个偏移问题。
我该如何解决?
提前致谢,
克里斯·帕帕斯
解决方案
替换了下面对我有用的代码。我可以调整窗口的大小,移动的文本会保留在其中。
available_width = root.winfo_screenwidth()
# Replace With
available_width = root.winfo_width()
推荐阅读
- python - 如何使用python设置时间限制
- html - 如何在正确渲染之前防止 Bootstrap-Slider 加载?
- hash - 如何将包含“+”的 SHA 哈希添加到内容安全策略 HTTP 标头
- java - 使用计数器向数组添加值
- python - 让 Flask 返回 xlsx 格式的 excel 文件作为输出
- python-3.x - 根据 .csv 重命名 fasta-header 的部分
- javascript - 读取数据产生 null
- python - Python - 如何在 pandas groupby 中使用计算函数?
- sql - Apache Derby 如何处理 WHERE IN () 子句中的空列表
- c++ - 如何分配参考链的末端?