python - 选中另一个复选框时取消选中
问题描述
所以问题出在if
功能上,它不起作用。它完全忽略了所有值的变化,根本不改变任何值。这段代码有什么问题?
import tkinter as tk
root = tk.Tk()
CheckVar4 = tk.IntVar()
CheckVar5 = tk.IntVar()
C4 = tk.Checkbutton(root, text = "Medium terms", variable = CheckVar4, \
onvalue = 1, offvalue = 0, height=1, \
width = 12)
C5 = tk.Checkbutton(root, text = "Hard terms", variable = CheckVar5, \
onvalue = 1, offvalue = 0, height=1, \
width = 8)
if CheckVar4.get() == 1:
CheckVar5.set(0)
if CheckVar5.get() == 1:
CheckVar4.set(0)
root.mainloop()
解决方案
也许想改用单选按钮?示例如下:
import tkinter as tk
root = tk.Tk()
v = tk.IntVar()
tk.Label(root,
text="""Choose a
programming language:""",
justify = tk.LEFT,
padx = 20).pack()
tk.Radiobutton(root,
text="Python",
padx = 20,
variable=v,
value=1).pack(anchor=tk.W)
tk.Radiobutton(root,
text="Perl",
padx = 20,
variable=v,
value=2).pack(anchor=tk.W)
root.mainloop()
推荐阅读
- python - 无法在单独的输出文件中使用 for 循环将数据保存在不同的行中
- assembly - 奇怪的 jmp 与调用行为
- javascript - 为什么这个 dropzone.js 不能上传到实时服务器?
- c++ - 如何使用 gnuplot 为箭头设置动画?
- python - 如何创建一个数字增加 3 的数组?
- html - 如何在网页上生成序号?
- asp.net-core - Microsoft azure 服务总线主题 Complete() 方法在 asp.net 核心的 Message 类中丢失
- python - 根据条件向列表元素添加值(Python)
- java - Selenium browsermobproxy 使所有网络不安全
- python - 当文件名具有不同长度的数字时,如何使用 pathlib.glob() 遍历文件