在此代码中,“更改输入选项”按钮在“处理”和“浏览”按钮之间切换。如果我们按其中任何一个,它下面的按钮都会被激活。当再次单击“更改输入选项”按钮时,我希望它禁用“处理”和“浏览”下面的按钮,并像最初一样再次开始在“处理”和“浏览”按钮之间切换。我该怎么办?
import tkinter as tk
import tkinter.ttk as ttk
window = tk.Tk()
def browse_file1():
if browse_button["state"] == "normal":
show_fig["state"] = "disabled"
else:
show_fig["state"] = "normal"
def open_window():
if submit_button["state"] == "disabled":
save_button["state"] = "disabled"
show_button["state"] = "disabled"
else:
save_button["state"] = "normal"
show_button["state"] = "normal"
def switch1():
submit_button.state(('!disabled' if 'disabled' in submit_button.state() else 'disabled',))
browse_button.state(('!disabled' if 'disabled' in browse_button.state() else 'disabled',))
window.configure(background='white')
ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
w = 700 # width for the Tk root
h = 410 # height for the Tk root
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)
window.geometry('%dx%d+%d+%d' % (w, h, x, y))
canvas = tk.Canvas(window,bg="white",width=700, height=410, highlightthickness=0)
canvas.pack()
submit_button = ttk.Button(canvas, text="Process", command=lambda: open_window())
canvas.create_window(560, 215, window=submit_button, anchor=tk.NW)
show_button = ttk.Button(canvas, text='Show Figure')
canvas.create_window(523, 248, window=show_button, anchor=tk.NW)
save_button = ttk.Button(canvas, text="Save Entry")
canvas.create_window(605, 248, window=save_button, anchor=tk.NW)
browse_button = ttk.Button(canvas, text='Browse', command= lambda: browse_file1())
canvas.create_window(150, 335, window=browse_button, anchor=tk.NW)
show_fig = ttk.Button(canvas, text="Show Figure")
canvas.create_window(150, 370, window=show_fig, anchor=tk.NW)
show_fig["state"] = "disabled"
browse_button["state"] = "disabled"
save_button["state"] = "disabled"
show_button["state"] = "disabled"
one_button = ttk.Button(canvas, text='Change Input Option', command=switch1)
canvas.create_window(17, 13, window=one_button, anchor=tk.NW)
window.resizable(False, False)
window.mainloop()
答案 0 :(得分:1)
这可以做到:
if str(submit_button["state"]) == "disabled":
submit_button["state"] = "normal"
browse_button["state"] = "disabled"
show_fig["state"] = "disabled"
elif str(browse_button["state"]) == "disabled":
submit_button["state"] = "disabled"
browse_button["state"] = "normal"
save_button["state"] = "disabled"
show_button["state"] = "disabled"
只需将您的switch1()
函数替换为此。