Python 访问canvas.create\u窗口内小部件的属性
我有以下问题。我正在制作一个tkinter GUI,我需要访问画布中的一个对象,在canvas.create\u窗口小部件中,包含一些其他对象。例如:Python 访问canvas.create\u窗口内小部件的属性,python,tkinter,tkinter-canvas,Python,Tkinter,Tkinter Canvas,我有以下问题。我正在制作一个tkinter GUI,我需要访问画布中的一个对象,在canvas.create\u窗口小部件中,包含一些其他对象。例如: import Tkinter as tk class Demo: def __init__(self, master): self.canvas = tk.Canvas() self.canvas.pack(fill="both", expand=True) f = tk.Frame(sel
import Tkinter as tk
class Demo:
def __init__(self, master):
self.canvas = tk.Canvas()
self.canvas.pack(fill="both", expand=True)
f = tk.Frame(self.canvas)
f.pack()
self.container = self.canvas.create_window(50,50, window = f)
l = tk.Label(f, text='abc')
e = tk.Entry(f, width = 5)
l.pack()
e.pack()
if __name__ == '__main__':
root = tk.Tk()
app = Demo(root)
root.mainloop()
class Demo:
def __init__(...):
...
self.l = tk.Label(f, text='abc')
...
def some_event_handler(event):
self.l.configure(text="xyz")
当触发其他事件时,我试图编辑l标签(当前为“abc”)的文本。我想我需要使用canvas.itemconfig,但是我找不到一种方法来向这个函数传递对标签的正确引用。有什么想法吗?
谢谢您不需要使用
itemconfigure
——这仅用于配置画布项目。您的标签不是画布项目,它只是一个普通的tkinter小部件,您可以像其他小部件一样访问它。保存引用,然后使用该引用调用方法
例如:
import Tkinter as tk
class Demo:
def __init__(self, master):
self.canvas = tk.Canvas()
self.canvas.pack(fill="both", expand=True)
f = tk.Frame(self.canvas)
f.pack()
self.container = self.canvas.create_window(50,50, window = f)
l = tk.Label(f, text='abc')
e = tk.Entry(f, width = 5)
l.pack()
e.pack()
if __name__ == '__main__':
root = tk.Tk()
app = Demo(root)
root.mainloop()
class Demo:
def __init__(...):
...
self.l = tk.Label(f, text='abc')
...
def some_event_handler(event):
self.l.configure(text="xyz")