当前位置:首页 / 文章测试 / python

python

开始打字练习

def set_op(op):

calLog.set(op) # 修改运算符号显示

# --- 新增:计算函数 ---

def calc():

try:

num1 = eval(v1.get())

num2 = eval(v2.get())

op = calLog.get() # 获取当前选择的运算符号

# 根据运算符号计算结果

if op == "+":

res = num1 + num2

elif op == "-":

res = num1 - num2

elif op == "*":

res = num1 * num2

elif op == "/":

if num2 == 0: # 处理除数为0的情况

res = "错误(除数为0)"

else:

res = num1 / num2

else:

res = "请选择运算符号"

v3.set(str(res)) # 显示结果

except ValueError:

v3.set("请输入有效数字")

# --- 新增:运算符号选择按钮(单选框) ---

op_var = StringVar(value="+") # 默认选择“加法”

Radiobutton(text="加法", variable=op_var, value="+", command=lambda: set_op("+")).grid(row=1, column=0)

Radiobutton(text="减法", variable=op_var, value="-", command=lambda: set_op("-")).grid(row=1, column=1)

Radiobutton(text="乘法", variable=op_var, value="*", command=lambda: set_op("*")).grid(row=1, column=2)

Radiobutton(text="除法", variable=op_var, value="/", command=lambda: set_op("/")).grid(row=1, column=3)

声明:以上文章均为用户自行发布,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。