在 tkinter 中编辑文本 [英] Editing Text in tkinter

查看:37
本文介绍了在 tkinter 中编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经将 create_text 变量传递给text1"

So I've passed a create_text variable to "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)

在将它传递给另一个类之后.

after passing it to another class.

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

How do I edit that text widget? I want it to say "Hello" instead of "Goodbye". I've looked all over and I can do anything I want with it EXCEPT change the text.

推荐答案

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

You should use the itemconfig method of the canvas to modify the text attribute. You have to give to it an id of one or more canvas items. Here is a small working example that lets you change the text of one text object:

# 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()

这篇关于在 tkinter 中编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆