我是新手&我在使用check Button()时遇到了麻烦.我想要它,这样当我选中"Button 3"时,"Button 1"和"Button 2"也会被选中.到目前为止,这是我的代码. 我看了这个问题:Can i make one checkbutton in tkinter check all the other checkbuttons?,这导致我使用.select()方法,但当我运行图形用户界面和判断按钮3、按钮1和2时,没有被选中.我应该如何编写函数,以便当我选中"Button 3"时,"Button 1"和"Button 2"也会被选中?

from tkinter import *

root = Tk()

button1_bool = BooleanVar()
button1 = checkButton(root, text = "Button 1", variable = button1_bool, onvalue = True, offvalue = False)
button1.pack()

button2_bool = BooleanVar()
button2 = checkButton(root, text = "Button 2", variable = button2_bool, onvalue = True, offvalue = False)
button2.pack()

def button3ischecked():
    if button3.get() == True:
        button1.select()
        button2.select()

button3_bool = BooleanVar()
button3 = checkButton(root, text = "Button 3", variable = button3_bool, onvalue = True, offvalue = False, command = button3ischecked)

root.mainloop()

推荐答案

代码中的主要问题

  1. 使用正确的函数创建复选框Checkbutton()
  2. get()函数需要在你的button3_bool上运行,这是保持状态的变量,否则你甚至不需要像其他人说的那样使用get()
from tkinter import *

root = Tk()

button1_bool = BooleanVar()
button1 = Checkbutton(root, text = "Button 1", variable = button1_bool, onvalue = True, offvalue = False)
button1.pack()

button2_bool = BooleanVar()
button2 = Checkbutton(root, text = "Button 2", variable = button2_bool, onvalue = True, offvalue = False)
button2.pack()

def button3ischecked():
    if button3_bool.get() == True:
        button1.select()
        button2.select()

button3_bool = BooleanVar()
button3 = Checkbutton(root, text="Button 3", variable=button3_bool, onvalue=True, offvalue=False,
                      command=button3ischecked)
button3.pack()

if __name__ == '__main__':
    root.mainloop()

我会同意授课,这样你就可以访问所有东西了

import tkinter as tk


class App(tk.Tk):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.button1_bool = tk.BooleanVar()
        self.button1 = tk.Checkbutton(self, text="Button 1", variable=self.button1_bool, onvalue=True, offvalue=False)
        self.button1.pack()

        self.button2_bool = tk.BooleanVar()
        self.button2 = tk.Checkbutton(self, text="Button 2", variable=self.button2_bool, onvalue=True, offvalue=False)
        self.button2.pack()

        self.button3_bool = tk.BooleanVar()
        self.button3 = tk.Checkbutton(self, text="Button 3", variable=self.button3_bool, onvalue=True, offvalue=False,
                                      command=self.button3ischecked)
        self.button3.pack()

    def button3ischecked(self):
        if self.button3_bool.get() == True:
            self.button1.select()
            self.button2.select()


if __name__ == '__main__':
    app = App()
    app.mainloop()

Python-3.x相关问答推荐

类型的可变性对变量的作用域有影响吗?

在numpy. linalg的qr之后使用scipy. integrate中的solve_ivp时出现了一个奇怪的错误

丢弃重复的索引,并在多索引数据帧中保留一个

按小时和日期对Pandas 数据帧进行分组

使用数据库将文件从Sharepoint下载到文件系统

tkinter treeview 如何在获取所选项目时将设置的对象作为对象返回

DynamoDB - boto3 - batch_write_item:提供的关键元素与架构不匹配

如何在Pandas 中按条件计算分组?

在字符串中查找正则表达式的所有模式

Python defaultdict 在获取时返回 None,尽管使用默认值初始化

Python3 AttributeError:列表对象没有属性清除

UnicodeDecodeError:utf-8编解码器无法解码位置 1 的字节 0x8b:无效的起始字节,同时读取Pandas中的 csv 文件

如何禁用 pylint 禁止自用警告?

基本 Flask 应用程序未运行(TypeError:模块中缺少必填字段type_ignores)

获取嵌套字典的所有键

无法在 Windows Python 3.5 上安装 Levenshtein 距离包

Python 3x 的最佳机器学习包?

Python3 mysqlclient-1.3.6(又名 PyMySQL)的用法?

AttributeError:系列对象没有属性iterrows

在python中打印下标