Tkinter checkbuttons默认在Mac上显示减号

时间:2017-06-06 12:37:22

标签: python python-2.7 tkinter

我在Mac上默认显示减号的复选框有问题:

screenshot

以下代码用于在labelframe中生成这些代码:

for x in ["Why", "Do", "These", "Have", "Minus", "Signs", "On", "Them?", "Clicked once", "Clicked twice"]:
    check_button = ttk.Checkbutton(
        check_button_labelframe,
        text=x,
    )
    check_button.grid(column=0, row=current_button_row, sticky="W")
    current_button_row += 1

减号是什么意思?我希望它们被检查或空白(分别在屏幕截图中的“Clicked once”和“Clicked two”)。我试过提供variable=somevariable

somevariable=tk.IntVar()
somevariable.set(1)

但这并没有改变任何事情。我在这里使用 ttk .checkbuttons,当使用tk.checkbuttons时,所有的框都只是空白。

编辑:更完整的示例:

        cases = {
            "case1": {
                "description": "This is case 1",
                "enabled_or_not": 0,
                "enabled_var": tk.IntVar()
            },
            "case2": {
                "description": "This is case 2",
                "enabled_or_not": 1,
                "enabled_var": tk.IntVar()
            }
        }

        for case in cases.keys():
            case_info = cases[case]
            case_info["enabled_var"].set(case_info["enabled_or_not"])
            check_button = ttk.Checkbutton(
                check_button_labelframe,
                text=case_info["description"],
                variable=case_info["enabled_var"],
                onvalue=1,
                offvalue=0
            )
            check_button.grid(column=0, row=current_button_row, sticky="W")
            current_button_row += 1

产生这个:

Another screenshot

编辑2: 这似乎与Macs动画复选框的方式有关,请查看:

Screen recording gif

编辑1000(MCVE):你是对的,伙计们,如果除去复选框之外的所有东西都会被删除,它会起作用:

app = tk.Tk()
app.wm_title("MCVE")
app.geometry("320x240")

# Tk checkbuttons
first_var = tk.IntVar()
first_var.set(1)
first_checkbox = tk.Checkbutton(app, text="This should be enabled", variable=first_var, onvalue=1, offvalue=0)
first_checkbox.grid(column=0, row=0, sticky="W")

second_var = tk.IntVar()
second_var.set(0)
second_checkbox = tk.Checkbutton(app, text="This should be disabled", variable=second_var, onvalue=1, offvalue=0)
second_checkbox.grid(column=0, row=1)

# Ttk checkbuttons
third_var = tk.IntVar()
third_var.set(1)
third_checkbox = ttk.Checkbutton(app, text="This should be enabled", variable=third_var, onvalue=1, offvalue=0)
third_checkbox.grid(column=0, row=2)

fourth_var = tk.IntVar()
fourth_var.set(0)
fourth_checkbox = ttk.Checkbutton(app, text="This should be disabled", variable=fourth_var, onvalue=1, offvalue=0)
fourth_checkbox.grid(column=0, row=3)

app.mainloop()

It's aliiiive

那么它与存储在字典中的变量有什么关系吗?

1 个答案:

答案 0 :(得分:0)

减号是默认状态,在Windows上是带灰色背景的检查标志。

我更喜欢在创建复选框后使用.deselect()方法 - 看起来更好。