3

所以我将 create_text 变量传递给“text1”

text1 = UseCanvas(self.window, "text", width = 100, height = 100, text = "Goodbye")

使用:

self.create_text(20,25 width=100,  anchor="center", text =self.text, tags= self.tag1)

将其传递给另一个班级后。

如何编辑该文本小部件?我希望它说“你好”而不是“再见”。我已经看遍了,我可以用它做任何我想做的事情,除了更改文本。

4

1 回答 1

13

您应该使用画布的itemconfig方法来修改文本属性。你必须给它一个或多个画布项目的 id。这是一个小的工作示例,可让您更改一个文本对象的文本:

# use 'tkinter' instead of 'Tkinter' if using python 3.x
import Tkinter as tk 

class Example(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)

        self.button = tk.Button(self, text="Change text", command=self.on_change_text)
        self.canvas = tk.Canvas(self, width=400, height=400)

        self.button.pack(side="top", anchor="w")
        self.canvas.pack(side="top", fill="both", expand=True)

        # every canvas object gets a unique id, which can be used later
        # to change the object. 
        self.text_id = self.canvas.create_text(10,10, anchor="nw", text="Hello, world")

    def on_change_text(self):
        self.canvas.itemconfig(self.text_id, text="Goodbye, world")

if __name__ == "__main__":
    root = tk.Tk()
    view = Example(root)
    view.pack(side="top", fill="both", expand=True)
    root.mainloop()
于 2013-03-04T02:06:09.003 回答