选中其他复选框时取消选中

2024-10-02 04:37:34 发布

您现在位置: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()

Tags: textgetifrootwidthvariabletkheight
1条回答
网友
1楼 · 发布于 2024-10-02 04:37:34

也许你想用单选按钮代替?来自here的示例如下:

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()

相关问题 更多 >

    热门问题