1
try:
#Python 2
import Tkinter as tk
except ImportError:
#Python 3
import tkinter as tk
def flip_switch(canv_obj, btn_text):
if btn_text == 'on':
canv_obj.config(bg="#F1F584")
else:
canv_obj.config(bg="#000000")
main_window = tk.Tk()
light = tk.Canvas(main_window, bg="#000000", width=100, height=50)
light.pack()
on_btn = tk.Button(main_window, text="ON", command=flip_switch(light, 'on'))
on_btn.pack()
off_btn = tk.Button(main_window, text="OFF", command=flip_switch(light, 'off'))
off_btn.pack()
main_window.mainloop()
這個小代碼用作光開關應用,而是按下ON按鈕時,沒有任何反應 - 不是連的錯誤消息。請糾正我出錯的地方。按下按鈕時,什麼都沒有發生。甚至沒有錯誤消息