为什么这些 Python tkinter checkbuttons 是链接的? [英] Why are these Python tkinter checkbuttons linked?

查看:23
本文介绍了为什么这些 Python tkinter checkbuttons 是链接的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建一个 GUI 来比较两个不同文件夹之间的文件,并拥有一个我现在正在尝试构建的基本框架.

I'm currently trying to create a GUI to compare files between two different folders and have a rudimentary framework that I'm trying to build off of right now.

我在窗口的左侧、右侧和底部有三个框架,每个框架有两个复选按钮.我希望能够相互独立地选择每个复选按钮,但每次单击任何一个框架的第一个复选按钮时,其他框架的第一个复选按钮也会同时选择/取消选择.

I have three frames on the left, right, and bottom of the window with two checkbuttons each. I want to be able to select each checkbutton independently of each other but every time I click on the first checkbutton of any one of the frames, the first checkbutton for the other frames also selects/deselects at the same time.

为什么会这样,我如何让它们彼此独立工作?这是我的代码供参考:

Why is this the case, and how do I make them work independently from each other? Here's my code for reference:

from tkinter import *

root = Tk()

leftFrame = Frame(root, bg = "#4d94ff")
leftFrame.pack(side = LEFT, fill = BOTH)

rightFrame = Frame(root, bg = "#ff4d4d")
rightFrame.pack(side = RIGHT, fill = BOTH)

bottomFrame = Frame(root, bg = "#5cd65c")
bottomFrame.pack(side = BOTTOM)

check_L1 = Checkbutton(leftFrame, text = "C1", bg = "#4d94ff")
check_L2 = Checkbutton(leftFrame, text = "C2", bg = "#4d94ff")

check_R1 = Checkbutton(rightFrame, text = "C1", bg = "#ff4d4d")
check_R2 = Checkbutton(rightFrame, text = "C2", bg = "#ff4d4d")

checktype1 = Checkbutton(bottomFrame, text = "Check Type 1", bg = "#5cd65c")
checktype2 = Checkbutton(bottomFrame, text = "Check Type 2", bg = "#5cd65c")

check_L1.grid(row = 0)
check_L2.grid(row = 0, column = 1)
check_R1.grid(row = 0)
check_R2.grid(row = 0, column = 1)
checktype1.grid(row = 0)
checktype2.grid(row = 1)


root.mainloop()

推荐答案

TL;DR

您需要为每个复选按钮指定一个唯一的变量.

var_L1 = tk.IntVar()
var_R1 = tk.IntVar()
...
check_L1 = Checkbutton(..., variable=var_L1)
check_R1 = Checkbutton(..., variable=var_R1)
...

发生了什么

复选按钮需要一个变量与之关联.您没有明确设置变量,因此 tkinter 正在创建默认值.Tkinter 通过创建以复选按钮名称命名的内部变量来实现这一点.

What is happening

Checkbuttons require a variable to be associated with them. You are't explicitly setting a variable so tkinter is creating defaults. Tkinter does this by creating internal variables named after the checkbutton name.

当您创建小部件但不给它们命名时,tkinter 将选择默认名称.全名包括整个小部件层次结构的名称.因此,例如,check_L1 的内部名称是 .!frame.!checkbutton'check_R1 的名称是 .!frame2.!checkbuttonchecktype1 的内部名称是 '.!frame3.!checkbutton'.

When you create widgets and don't give them names, tkinter will pick default names. The full name includes the names of entire widget hierarchy. So, for example, the internal name for check_L1 is .!frame.!checkbutton', the name for check_R1 is .!frame2.!checkbutton, and the internal name for checktype1 is '.!frame3.!checkbutton'.

当 tkinter 为复选按钮创建默认变量时,它仅使用变量全名的最后一部分.因此,对于 check_L1,它会创建一个名为 !checkbutton 的内部变量.check_R1 的内部变量也是 !checkbuttonchecktype2 的内部变量也是.

When tkinter creates the default variables for the checkbuttons it uses only the last component of the full name for the variables. Thus, for check_L1 it creates an internal variable named !checkbutton. The internal variable for check_R1 is also !checkbutton, as is the internal variable for checktype2.

因为所有这些复选按钮共享同一个内部变量,所以它们是相互关联的.

Because all of these checkbuttons share the same internal variable, they are linked.

注意:如果你在一个循环中创建了很多复选框,你可以将每个复选框的引用保存在一个数组中.例如:

Note: if you are creating a lot of checkboxes in a loop, you can save the reference to each checkbutton in an array. For example:

vars = []
for i in range(x):
    vars.append[tk.IntVar())
...
check_L1 = Checkbutton(..., variable=vars[0])
...
print(f"the value of L1 is {vars[0].get()}")

这篇关于为什么这些 Python tkinter checkbuttons 是链接的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆