请检查执行硬编码加、减、乘、删除功能的这段代码。
- 当您勾选复选框时,会触发相应的功能。
- 当您单击运行按钮时,将触发所有功能。
- 单击运行按钮后,将打印输出并关闭 tkinter 窗口。
from tkinter import *
master = Tk()
def run_all():
var1.set(1)
var2.set(1)
var3.set(1)
var4.set(1)
ad()
sub()
mul()
div()
master.destroy()
def ad():
if(var1.get()==1):
print(5+5)
def sub():
if(var2.get()==1):
print(5-5)
def mul():
if(var3.get()==1):
print(5*5)
def div():
if(var4.get()==1):
print(5/5)
Label(master, text="Operations:").grid(row=0, sticky=W)
var1 = IntVar()
Checkbutton(master, text="Add", variable=var1,command=ad).grid(row=1, sticky=W)
var2 = IntVar()
Checkbutton(master, text="Subtract", variable=var2,command=sub).grid(row=2, sticky=W)
var3 = IntVar()
Checkbutton(master, text="Multiply", variable=var3,command=mul).grid(row=3, sticky=W)
var4 = IntVar()
Checkbutton(master, text="Divide", variable=var4,command=div).grid(row=4, sticky=W)
Button(master, text='Run', command=run_all).grid(row=5, sticky=W, pady=4)
mainloop()
编辑:
根据评论,现在所有功能只有在您按下运行按钮时才会运行
Label(master, text="Operations:").grid(row=0, sticky=W)
var1 = IntVar()
Checkbutton(master, text="Add", variable=var1).grid(row=1, sticky=W)
var2 = IntVar()
Checkbutton(master, text="Subtract", variable=var2).grid(row=2, sticky=W)
var3 = IntVar()
Checkbutton(master, text="Multiply", variable=var3).grid(row=3, sticky=W)
var4 = IntVar()
Checkbutton(master, text="Divide", variable=var4).grid(row=4, sticky=W)
Button(master, text='Run', command=run_all).grid(row=5, sticky=W, pady=4)
mainloop()