我想让我的标题中的每个字母一个接一个地出现。

huangapple go评论67阅读模式
英文:

I want each letter of my title to appear one by one

问题

我正在尝试每次添加一个字母。
我有一个可行的方法,但我确信可以使用for循环来实现更简单的方式。

def set_title():
    global x
    window.after(x, titleset)

def titleset():
    global x
    title_label.config(text="C")
    window.after(x, titleset_a)

def titleset_a():
    global x
    title_label.config(text="CA")
    window after(x, titleset_e)

def titleset_e():
    global x
    title_label.config(text="CAE")
    window.after(x, titleset_s)

def titleset_s():
    global x
    title_label.config(text="CAES")
    window.after(x, titleset_a2)

def titleset_a2():
    global x
    title_label.config(text="CAESA")
    window.after(x, titleset_r)

def titleset_r():
    global x
    title_label.config(text="CAESAR")
    window.after(x, titleset_101)

def titleset_101():
    global x
    title_label.config(text="CAESAR'S")
    window.after(x, titleset_c2)

(此后的部分未提供,但足够传达我的意思)

英文:

I am trying to get it to add a letter each time.
I have a working method but I am sure there is a simpler way using a for loop.

def set_title():
    global x
    window.after(x,titleset)


def titleset():
    global x
    title_label.config(text="C")
    window.after(x, titleset_a)
def titleset_a():
    global x
    title_label.config(text="CA")
    window.after(x, titleset_e)
def titleset_e():
    global x
    title_label.config(text="CAE")
    window.after(x, titleset_s)
def titleset_s():
    global x
    title_label.config(text="CAES")
    window.after(x, titleset_a2)
def titleset_a2():
    global x
    title_label.config(text="CAESA")
    window.after(x, titleset_r)
def titleset_r():
    global x
    title_label.config(text="CAESAR")
    window.after(x, titleset_101)
def titleset_101():
    global x
    title_label.config(text="CAESAR'")
    window.after(x, titleset_s1)
def titleset_s1():
    global x
    title_label.config(text="CAESAR'S")
    window.after(x, titleset_c2)

<it goes on but thats enough to get my point across

答案1

得分: 2

你可以使用 after 方法来请求在延迟后调用一个函数。该函数可以重新安排自身运行,创建一个基于时间的循环。

以下是一个传递小部件和字符串给这样一个函数的解决方案。该函数将从字符串中取出一个字符,将其添加到小部件中,然后安排自身再次调用,直到所有字符都被显示出来。

def titleset(label, string):
    current_title = label.cget("text")
    label.configure(text=current_title + string[0])
    if len(string) > 1:
        label.after(500, titleset, label, string[1:])

这是一个使用这个函数的完整程序:

import tkinter as tk

def titleset(label, string):
    current_title = label.cget("text")
    label.configure(text=current_title + string[0])
    if len(string) > 1:
        label.after(500, titleset, label, string[1:])

root = tk.Tk()

title = "Hello, world!"
title_label = tk.Label(root, width=len(title))
title_label.pack(side="top")

titleset(title_label, title)
root.mainloop()
英文:

You can use the after method to request that a function be called after a delay. That function can reschedule itself to run, creating a time-based loop.

Here's a solution that passes a widget and a string to such a function. The function will pull one character off of the string, add it to the widget, and then arrange for itself to be called again until all characters have been displayed.

def titleset(label, string):
    current_title = label.cget(&quot;text&quot;)
    label.configure(text=current_title + string[0])
    if len(string) &gt; 1:
        label.after(500, titleset, label, string[1:])

Here is a complete program using this function:

import tkinter as tk

def titleset(label, string):
    current_title = label.cget(&quot;text&quot;)
    label.configure(text=current_title + string[0])
    if len(string) &gt; 1:
        label.after(500, titleset, label, string[1:])

root = tk.Tk()

title=&quot;Hello, world!&quot;
title_label = tk.Label(root, width=len(title))
title_label.pack(side=&quot;top&quot;)

titleset(title_label, title)
root.mainloop()

答案2

得分: 1

这是一个使用for循环和tkinter.after的示例解决方案:

import tkinter as tk


def write_name() -> None:
    global index  # 允许此函数更新 'index' 的值
    index += 1  # 增加索引
    label.config(text=name[0:index])  # 更新标签
    loop = root.after(250, write_name)  # 250毫秒后再次调用此函数
    if index == len(name):  # 当整个 'name' 字符串已经写出时...
        root.after_cancel(loop)  # 停止调用 'write_name'


root = tk.Tk()
name = "CAESAR'S"
index = 0
label = tk.Label(root, text='')  # 创建一个空标签
label.pack()
write_name()  # 开始更新标签


if __name__ == '__main__':
    root.mainloop()  # 启动应用程序
英文:

Here's an example solution using a for loop and tkinter.after

import tkinter as tk


def write_name() -&gt; None:
    global index  # allow this function to update the value of &#39;index&#39;
    index += 1  # increment the index
    label.config(text=name[0:index])  # update the label
    loop = root.after(250, write_name)  # call this function again after 250mS
    if index == len(name):  # when the entire &#39;name&#39; string has been written out...
        root.after_cancel(loop)  # stop calling &#39;write_name&#39;


root = tk.Tk()
name = &quot;CAESAR&#39;S&quot;
index = 0
label = tk.Label(root, text=&#39;&#39;)  # create an empty label
label.pack()
write_name()  # begin updating the label


if __name__ == &#39;__main__&#39;:
    root.mainloop()  # start the app

</details>



# 答案3
**得分**: 1

```python
title_index = 0

def set_title():
    global x, title_index

    title_index = 0
    window.after(x, titleset)

def titleset():
    global x, title_index

    title = "CAESAR'S"
    title_label.config(title[:title_index+1])
    if title_index < len(title):
        title_index += 1
        root.after(x, titleset)
英文:

How about:

title_index = 0

def set_title():
    global x, title_index

    title_index = 0
    window.after(x, titleset)

def titleset():
    global x, title_index

    title = &quot;CAESAR&#39;S&quot;
    title_label.config(title[:title_index+1])
    if title_index &lt; len(title):
        title_index += 1
        root.after(x, titleset)

答案4

得分: 1

以下是翻译好的内容:

但我确信使用for循环有一种更简单的方法

如果这是你想要的可以这样做它将重复执行

- 我将此处的`window.after(x, titleset_c2)`更改为`window.after(x, titleset)`
- 我添加`x = 1000`以进行延迟

代码片段

```python
from tkinter import *
x = 1000

window = Tk()
def set_title():
    global x
    window.after(x, titleset)

def titleset():
    global x
    title_label.config(text="C")
    window.after(x, titleset_a)

def titleset_a():
    global x
    title_label.config(text="CA")
    window.after(x, titleset_e)

def titleset_e():
    global x
    title_label.config(text="CAE")
    window.after(x, titleset_s)

def titleset_s():
    global x
    title_label.config(text="CAES")
    window.after(x, titleset_a2)

def titleset_a2():
    global x
    title_label.config(text="CAESA")
    window.after(x, titleset_r)

def titleset_r():
    global x
    title_label.config(text="CAESAR")
    window after(x, titleset_101)

def titleset_101():
    global x
    title_label.config(text="CAESAR&#39;")
    window.after(x, titleset_s1)

def titleset_s1():
    global x
    title_label.config(text="CAESAR&#39;S")
    window.after(x, titleset)

title_label = Label(window)
title_label.grid()

set_title()
window.mainloop()

截图:

我想让我的标题中的每个字母一个接一个地出现。


<details>
<summary>英文:</summary>

 

    but I am sure there is a simpler way using a for loop.

If this is what you want, like this. It will repeatedly.

 - I change this  `window.after(x, titleset_c2)` to `window.after(x,
   titleset)`    
 - I add `x = 1000` for delay.

 Snippet:

    from tkinter import *
    x = 1000
     
    window = Tk()
    def set_title():
        global x
        window.after(x,titleset)
    
    def titleset():
        global x
        title_label.config(text=&quot;C&quot;)
        window.after(x, titleset_a)
        
    def titleset_a():
        global x
        title_label.config(text=&quot;CA&quot;)
        window.after(x, titleset_e)
        
    def titleset_e():
        global x
        title_label.config(text=&quot;CAE&quot;)
        window.after(x, titleset_s)
        
    def titleset_s():
        global x
        title_label.config(text=&quot;CAES&quot;)
        window.after(x, titleset_a2)
        
    def titleset_a2():
        global x
        title_label.config(text=&quot;CAESA&quot;)
        window.after(x, titleset_r)
        
    def titleset_r():
        global x
        title_label.config(text=&quot;CAESAR&quot;)
        window.after(x, titleset_101)
        
    def titleset_101():
        global x
        title_label.config(text=&quot;CAESAR&#39;&quot;)
        window.after(x, titleset_s1)
        
    def titleset_s1():
        global x
        title_label.config(text=&quot;CAESAR&#39;S&quot;)
        window.after(x, titleset)
    
    title_label = Label(window)
    title_label.grid()
    
    set_title()
    window.mainloop()

Screenshot:

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/xznwa.gif

</details>



# 答案5
**得分**: 0

我为你准备的工作代码:

import tkinter as tk
from time import sleep

root = tk.Tk()

title = 'CAESAR\'S'

for i in range(len(title)):
    root.title(title[:i+1])
    root.update()
    sleep(1)
英文:

working code for me:

import tkinter as tk
from time import sleep

root = tk.Tk()

title = &#39;CAESAR\&#39;S&#39;

for i in range(len(title)):
    root.title(title[:i+1])
    root.update()
    sleep(1)

</details>



# 答案6
**得分**: 0

实际上,您可以使用`.after()`方法只使用一个函数来实现您想要的效果:

```python
import tkinter as tk

def set_title(title, idx=0, delay=200):
    if idx < len(title):
        idx += 1
        title_label.config(text=title[:idx])
        window.after(delay, set_title, title, idx, delay)

window = tk.Tk()
window.geometry("400x200")
title_label = tk.Label(window)
title_label.pack(fill="x")
# 启动标题动画
set_title("This title will be shown character by character")
window.mainloop()
英文:

Actually you can use only one function with .after() to achieve what you want:

import tkinter as tk

def set_title(title, idx=0, delay=200):
    if idx &lt; len(title):
        idx += 1
        title_label.config(text=title[:idx])
        window.after(delay, set_title, title, idx, delay)

window = tk.Tk()
window.geometry(&quot;400x200&quot;)
title_label = tk.Label(window)
title_label.pack(fill=&quot;x&quot;)
# start the title animation
set_title(&quot;This title will be shown character by character&quot;)
window.mainloop()

huangapple
  • 本文由 发表于 2023年2月23日 23:28:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定