首页 > 解决方案 > 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()

代码编辑:

文本正在向东移动,然后向西移动,但现在它是一个偏移问题。

我该如何解决?

提前致谢,

克里斯·帕帕斯

标签: pythonanimationtkinter

解决方案


替换了下面对我有用的代码。我可以调整窗口的大小,移动的文本会保留在其中。

available_width = root.winfo_screenwidth()
# Replace With
available_width = root.winfo_width()

推荐阅读