用Python实现几个复选框勾选,并输出对应结果到界面上,不要输出到控制台
这里提供一个基于Tkinter模块的Python代码实现,包含三个复选框,勾选不同的复选框会输出不同的结果到界面上:
import tkinter as tk
# 定义复选框勾选函数
def check():
result = ""
if var1.get() == 1:
result += "复选框1已勾选\n"
if var2.get() == 1:
result += "复选框2已勾选\n"
if var3.get() == 1:
result += "复选框3已勾选\n"
label.config(text=result)
# 创建主窗口
root = tk.Tk()
root.geometry("300x200")
# 创建复选框
var1 = tk.IntVar()
checkbutton1 = tk.Checkbutton(root, text="复选框1", variable=var1, command=check)
checkbutton1.pack()
var2 = tk.IntVar()
checkbutton2 = tk.Checkbutton(root, text="复选框2", variable=var2, command=check)
checkbutton2.pack()
var3 = tk.IntVar()
checkbutton3 = tk.Checkbutton(root, text="复选框3", variable=var3, command=check)
checkbutton3.pack()
# 创建文本标签
label = tk.Label(root, text="")
label.pack()
# 进入消息循环
root.mainloop()
运行代码后,会出现一个带有三个复选框和一个文本标签的窗口。勾选不同的复选框会输出不同的结果到文本标签上。
原文地址: https://www.cveoy.top/t/topic/sJY 著作权归作者所有。请勿转载和采集!