如何在tkinter的标签中包含可变文本?

时间:2019-06-20 11:00:06

标签: python tkinter label

我希望标签具有文本的一个字符,然后两个,然后三个,然后四个,依此类推。 我了解我需要在Label中使用StringVar和/或textvariable参数。 如您所见,我已经尝试过使用StringVar()。我还尝试了textvariable(使用和不使用.get()),仅使用.get()进行文本转换。在所有情况下,文本都不会出现或立即出现,而不是停滞不前。 这是我的代码:

class gui:
 def __init__(self, master):
     self.master = master
     master.title("VisualNovelPrototype")

     self.str = StringVar()
     self.str.set("Welcome to the game")
     self.label = Label(master, text=self.str.get())
     self.label.grid(row=0,column=1)

     self.button0 = Button(master, text="yes", command=self.start)
     self.button0.grid(row=1,column=1)

     self.button1 = Button(master, text="no", command=master.quit)
     self.button1.grid(row=2,column=1)

 def label_change(self, text):
     str = self.str
     store = ""
     for index in range(len(text)):
         store += text[index]
         str.set(store)
         time.sleep(0.08)
         self.label.config(text=self.str.get())

 def button_change(self, master, text0="", command0="", text1="", command1=""):
     self.button0.config(text=text0, command=command0)
     self.button1.config(text=text1, command=command1)

 def start(self):
     self.label_change("Its a cold night. A storm rages outside. You hear a knock at the door.")

1 个答案:

答案 0 :(得分:0)

我认为最好在迭代过程中使用label.config(text =“ TEXT”),就像我在代码中所做的那样。 我建议使用更多的self.variables将它们连接到每个函数。

代码如下:

from tkinter import *
import time
class gui:
 def __init__(self, master):
     self.master = master
     self.master.geometry("365x100")
     self.master.title("VisualNovelPrototype")

     self.str1 = StringVar()
     self.str1.set("Welcome to the game")
     self.label = Label(self.master, text=self.str1.get())
     self.label.grid(row=0,column=1)

     self.button0 = Button(self.master, text="yes", command=self.start)
     self.button0.place(x=180,y=40)

     self.button1 = Button(self.master, text="no", command=self.master.destroy)
     self.button1.place(x=180,y=70)

 def label_change(self, text):
     def process():
         self.store += text[self.index]
         self.index += 1
         self.label.config(text=self.store)
         self.master.update()
         self.label_change(self.txt)
     if self.store!=text:
         time.sleep(0.100)
         process()
     else:
         pass

 def button_change(self, master, text0="", command0="", text1="", command1=""):
     self.button0.config(text=text0, command=command0)
     self.button1.config(text=text1, command=command1)

 def start(self):
     self.store=""
     self.index=0
     self.txt="Its a cold night. A storm rages outside. You hear a knock at the door."
     self.label_change(self.txt)

window=Tk()
et=gui(window)

单击“是”时,此函数运行,该函数将初始化self.index和self.txt来为字符串建立索引,而另一个函数则用于存储字符串。 然后转到带有参数'self.txt'的label_change函数。

 def start(self):
     self.store=""
     self.index=0
     self.txt="Its a cold night. A storm rages outside. You hear a knock at the door."
     self.label_change(self.txt)

函数label_change将递归直到self.store与self.txt相同。

 if self.store!=text:
     time.sleep(0.100)
     process()
 else:
     pass

然后它将调用流程函数在'self.store'变量中添加一个字符,并将self.index递增1。最后,它将更新self.master(GUI窗口)。

def process():
     self.store += text[self.index]
     self.index += 1
     self.label.config(text=self.store)
     self.master.update()
     self.label_change(self.txt)

这是我的第一个答案,因此,如果有任何问题,我愿意提供帮助。 iao!