python_tkinter-格子变色
from tkinter import * def change_color(): current_color = box.cget("background") if current_color == "red": next_color = "green" else: next_color = "red" box.config(background=next_color) root.after(1000, change_color) root = Tk() box = Text(root, background="green") box.pack() change_color() root.mainloop()
另外一种写法
from tkinter import * def change_color(idx=0): print(idx) color = ["red","green"] box.config(background=color[idx]) root.after(1000, change_color,(idx+1) % 2) root = Tk() box = Text(root, background="green") box.pack() change_color() root.mainloop()

浙公网安备 33010602011771号