第27529题 单选题
关于Python Tkinter中Button组件的带参事件绑定,下列写法正确的是?

现有如下Python代码片段,需要实现点击Tkinter按钮时调用带参数的show_message函数并打印指定内容:

import tkinter as tk
root = tk.Tk()
def show_message(msg):
    print(f"按钮点击提示:{msg}")

# 待完成Button组件的事件绑定

下列四个选项中,哪一个可以正确完成该绑定需求?

A

btn = tk.Button(root, text="测试按钮", command=show_message("Hello Tkinter"))

B

btn = tk.Button(root, text="测试按钮", command=lambda: show_message("Hello Tkinter"))

C

btn = tk.Button(root, text="测试按钮", command=show_message)

D

btn = tk.Button(root, text="测试按钮", bind="<Button-1>", show_message("Hello Tkinter"))

程序运行统计
暂无判题统计
提交0次 正确率0.00%
答案解析