有没有办法使用Tkinter在Python 3.4.1中逐字母打印(在帧更新之间重绘)?
使用这段代码在普通的IDLE窗口中可以这样做:
import time
string = "hello world"
for char in string:
print(char, end='')
time.sleep(.25)
但我不能在GUI Tkinter窗口中这样做。在我看来,我可以在tkinter窗口上显示一个标签,在字符串中添加一个字母,然后在添加另一个字母(大约0.25秒)之前等待一段时间,其思路与上面的代码相同。 任何解决方案?
例如:
要显示的字符串 ='abc'
第一个框架:'a'
第二个框架:'ab'
第三个框架:'abc'
提前致谢!
答案 0 :(得分:0)
你没有详细说明,所以我猜你会喜欢这样的东西:
import time
import tkinter as tk
def printString(string):
for char in string:
Label.configure(text=Label.cget('text') + char)
Label.update()
time.sleep(.25)
root = tk.Tk()
text = "Hello world!"
Label = tk.Label(root)
Label.pack()
printString(text)
root.mainloop()
更新:
相同的原则,但canvas
:
import time
import tkinter as tk
def printString(item, string):
if item:
for char in string:
idx = c.index(item, tk.END)
c.insert(item, idx, char)
c.update()
time.sleep(.25)
root = tk.Tk()
text = "Hello world!"
c = tk.Canvas(root)
c_txt = c.create_text(50,50)
c.pack()
c.update()
printString(c_txt, text)
root.mainloop()
答案 1 :(得分:0)
您可以使用after
定期调用方法来更新GUI。 label
不易于使用,因为它阻止了GUI运行的主线程。
您可以定期 替换 try:
import tkinter as tk
except:
import Tkinter as tk
def update_label_letter():
global label
label['text'] = label.string[label.current_char_index]
label.current_char_index += 1
label.current_char_index %= len(label.string)
label.after(250, update_label_letter)
if __name__ == '__main__':
root = tk.Tk()
label = tk.Label(root)
label.string = "Hello World!"
label.current_char_index = 0
root.after(250, update_label_letter)
label.pack()
root.mainloop()
的单个字符文字 字母串中的字母 :
label
一种方法是定期 追加 单 try:
import tkinter as tk
except:
import Tkinter as tk
def update_label_letters():
global label
_no_of_space_replacements = label['text'].count('\n')
_last_char_index = len(label['text']) - _no_of_space_replacements
if _last_char_index < len(label.string):
label['text'] += label.string[_last_char_index].replace(" ", " \n")
else:
label['text'] = ""
label.after(250, update_label_letters)
if __name__ == '__main__':
root = tk.Tk()
label = tk.Label(root, wraplength=1)
label.string = "Hello World!"
root.after(250, update_label_letters)
label.pack()
root.mainloop()
的字母。
<强>垂直强>
try:
import tkinter as tk
except:
import Tkinter as tk
def update_label_letters():
global label
_last_char_index = len(label['text'])
if _last_char_index < len(label.string):
label['text'] += label.string[_last_char_index]
else:
label['text'] = ""
label.after(250, update_label_letters)
if __name__ == '__main__':
root = tk.Tk()
label = tk.Label(root)
label.string = "Hello World!"
root.after(250, update_label_letters)
label.pack()
root.mainloop()
<强>水平强>
letter_labels
另一种方法是 定期将字母 写为空,但显示为try:
import tkinter as tk
except:
import Tkinter as tk
def update_label_letters():
global current_char_index, my_string, letter_labels
if current_char_index < len(my_string):
_current_char = my_string[current_char_index]
letter_labels[current_char_index]['text'] = _current_char
letter_labels[current_char_index].after(250, update_label_letters)
current_char_index += 1
if __name__ == '__main__':
root = tk.Tk()
my_string = "Hello World!"
letter_labels = list()
for index, value in enumerate(my_string):
letter_labels.append(tk.Label(root))
letter_labels[index].pack()
current_char_index = 0
root.after(250, update_label_letters)
root.mainloop()
。
<强>垂直强>
try:
import tkinter as tk
except:
import Tkinter as tk
def update_label_letters():
global current_char_index, my_string, letter_labels
if current_char_index < len(my_string):
_current_char = my_string[current_char_index]
letter_labels[current_char_index]['text'] = _current_char
letter_labels[current_char_index].after(250, update_label_letters)
current_char_index += 1
if __name__ == '__main__':
root = tk.Tk()
my_string = "Hello World!"
letter_labels = list()
for index, value in enumerate(my_string):
letter_labels.append(tk.Label(root))
letter_labels[index].pack(side='left')
current_char_index = 0
root.after(250, update_label_letters)
root.mainloop()
<强>水平强>
letter_labels
另一种方法是定期 显示 try:
import tkinter as tk
except:
import Tkinter as tk
def show_label():
global current_char_index, my_string, letter_labels
if current_char_index < len(my_string):
letter_labels[current_char_index].pack()
letter_labels[current_char_index].after(250, show_label)
current_char_index += 1
if __name__ == '__main__':
root = tk.Tk()
my_string = "Hello World!"
letter_labels = list()
for _char in my_string:
letter_labels.append(tk.Label(root, text=_char))
current_char_index = 0
root.after(250, show_label)
root.mainloop()
。
<强>垂直强>
try:
import tkinter as tk
except:
import Tkinter as tk
def show_label():
global current_char_index, my_string, letter_labels
if current_char_index < len(my_string):
letter_labels[current_char_index].pack(side='left')
letter_labels[current_char_index].after(250, show_label)
current_char_index += 1
if __name__ == '__main__':
root = tk.Tk()
my_string = "Hello World!"
letter_labels = list()
for _char in my_string:
letter_labels.append(tk.Label(root, text=_char))
current_char_index = 0
root.after(250, show_label)
root.mainloop()
<强>水平强>
ReactDOM.render