|
发表于 2017-5-12 12:00:00
|
显示全部楼层
- #Use a trace on the StringVar:
- from tkinter import *
- root = Tk()
- s = StringVar(root, '23.1')
- # your image here
- image1 = PhotoImage(width=200, height=200)
- image1.put('blue', to=(0,0,199,199))
- canvas = Canvas(root, width=300, height=300)
- canvas.pack()
- canvas.create_image(150,150, anchor='c', image=image1)
- txt = canvas.create_text(150,150, font='Helvetica 24 bold', text=s.get())
- def on_change(varname, index, mode):
- canvas.itemconfigure(txt, text=root.getvar(varname))
- s.trace_variable('w', on_change)
- def trigger_change():
- s.set('26.0')
- root.after(2000, trigger_change)
- root.mainloop()
复制代码
- ##Alternatively, you could just use a Label widget and take advantage of the compound option.
- from tkinter import *
- root = Tk()
- s = StringVar(root, 'fine')
- image1 = PhotoImage(width=200, height=200)
- image1.put('blue', to=(0,0,199,199))
- image2 = PhotoImage(width=200, height=200)
- image2.put('gray70', to=(0,0,199,199))
- lbl = Label(root, compound='center', textvariable=s, image=image1)
- lbl.pack()
- def trigger_change():
- lbl.config(image=image2)
- s.set('cloudy')
- root.after(2000, trigger_change)
- root.mainloop()
复制代码 |
|