Python Tkinter 网格复选框

Python Tkinter Grid Checkbox(Python Tkinter 网格复选框)
本文介绍了Python Tkinter 网格复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种简单的方法可以使用 Tkinter 创建复选框网格.我正在尝试制作一个 10 行和列的网格(所以 100 个复选框),以便每行只能选择两个复选框.

I was wondering if there is an easy way to create a grid of checkboxes using Tkinter. I am trying to make a grid of 10 rows and columns (so 100 checkboxes) so that only two checkboxes can be selected per row.

我正在使用带有 spyder 的 python 2.7

I'm using python 2.7 with spyder

到目前为止我所拥有的:

What I have so far:

from Tkinter import*

master = Tk()
master.title("Select Groups")

rows=10
columns=10


for x in range(rows):
    for y in range(columns):
        Label(master, text= "Group %s"%(y+1)).grid(row=0,column=y+1)
        Label(master, text= "Test %s"%(x+1)).grid(row=x+1,column=0)
        Checkbutton(master).grid(row=x+1, column=y+1)

mainloop()

一旦选中了两个复选框,我正在尝试使用 state='Disabled' 来灰显一行.

I'm trying to use state='Disabled' to grey out a row once two checkboxes have been selected.

推荐答案

这是一个使用您提供的 10x10 网格的示例.它应该为您提供有关如何实现此功能的基本概念.

Here's an example using your provided 10x10 grid. It should give you the basic idea of how to implement this.

只需确保您保留对每个 Checkbutton(示例中为 boxes)以及每个 IntVar(boxVars 在示例中).

Just make sure you keep a reference to every Checkbutton (boxes in the example) as well as every IntVar (boxVars in the example).

原因如下:

-Checkbuttons 需要调用 config(state = DISABLED/NORMAL).

-IntVars 来确定每个 Checkbutton 的值.

-IntVars are needed to determine the value of each Checkbutton.

除了这些关键元素之外,它基本上只是一些二维数组处理.

Aside from those crucial elements its basically just some 2D array processing.

这是我的示例代码(现在基于您提供的代码).

Here's my example code (now based off of your provided code).

from Tkinter import *

master = Tk()
master.title("Select Groups")

rows=10
columns=10

boxes = []
boxVars = []

# Create all IntVars, set to 0

for i in range(rows):
    boxVars.append([])
    for j in range(columns):
        boxVars[i].append(IntVar())
        boxVars[i][j].set(0)

def checkRow(i):
    global boxVars, boxes
    row = boxVars[i]
    deselected = []

    # Loop through row that was changed, check which items were not selected 
    # (so that we know which indeces to disable in the event that 2 have been selected)

    for j in range(len(row)):
        if row[j].get() == 0:
            deselected.append(j)

    # Check if enough buttons have been selected. If so, disable the deselected indeces,
    # Otherwise set all of them to active (in case we have previously disabled them).

    if len(deselected) == (len(row) - 2):
        for j in deselected:
            boxes[i][j].config(state = DISABLED)
    else:
        for item in boxes[i]:
            item.config(state = NORMAL)

def getSelected():
    selected = {}
    for i in range(len(boxVars)):
        temp = []
        for j in range(len(boxVars[i])):
            if boxVars[i][j].get() == 1:
                temp.append(j + 1)
        if len(temp) > 1:
            selected[i + 1] = temp
    print selected


for x in range(rows):
    boxes.append([])
    for y in range(columns):
        Label(master, text= "Group %s"%(y+1)).grid(row=0,column=y+1)
        Label(master, text= "Test %s"%(x+1)).grid(row=x+1,column=0)
        boxes[x].append(Checkbutton(master, variable = boxVars[x][y], command = lambda x = x: checkRow(x)))
        boxes[x][y].grid(row=x+1, column=y+1)

b = Button(master, text = "Get", command = getSelected, width = 10)
b.grid(row = 12, column = 11)
mainloop()

这篇关于Python Tkinter 网格复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)