英文:
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("text")
label.configure(text=current_title + string[0])
if len(string) > 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("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()
答案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() -> None:
global index # allow this function to update the value of 'index'
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 'name' string has been written out...
root.after_cancel(loop) # stop calling 'write_name'
root = tk.Tk()
name = "CAESAR'S"
index = 0
label = tk.Label(root, text='') # create an empty label
label.pack()
write_name() # begin updating the label
if __name__ == '__main__':
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 = "CAESAR'S"
title_label.config(title[:title_index+1])
if title_index < 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'")
window.after(x, titleset_s1)
def titleset_s1():
global x
title_label.config(text="CAESAR'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="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)
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 = 'CAESAR\'S'
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 < 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")
# start the title animation
set_title("This title will be shown character by character")
window.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论