<legend id='LwTIL'><style id='LwTIL'><dir id='LwTIL'><q id='LwTIL'></q></dir></style></legend>
  1. <tfoot id='LwTIL'></tfoot>

  2. <small id='LwTIL'></small><noframes id='LwTIL'>

    1. <i id='LwTIL'><tr id='LwTIL'><dt id='LwTIL'><q id='LwTIL'><span id='LwTIL'><b id='LwTIL'><form id='LwTIL'><ins id='LwTIL'></ins><ul id='LwTIL'></ul><sub id='LwTIL'></sub></form><legend id='LwTIL'></legend><bdo id='LwTIL'><pre id='LwTIL'><center id='LwTIL'></center></pre></bdo></b><th id='LwTIL'></th></span></q></dt></tr></i><div id='LwTIL'><tfoot id='LwTIL'></tfoot><dl id='LwTIL'><fieldset id='LwTIL'></fieldset></dl></div>

        <bdo id='LwTIL'></bdo><ul id='LwTIL'></ul>

      Python Tkinter 网格复选框

      Python Tkinter Grid Checkbox(Python Tkinter 网格复选框)
      1. <legend id='Q8NBj'><style id='Q8NBj'><dir id='Q8NBj'><q id='Q8NBj'></q></dir></style></legend>
        <i id='Q8NBj'><tr id='Q8NBj'><dt id='Q8NBj'><q id='Q8NBj'><span id='Q8NBj'><b id='Q8NBj'><form id='Q8NBj'><ins id='Q8NBj'></ins><ul id='Q8NBj'></ul><sub id='Q8NBj'></sub></form><legend id='Q8NBj'></legend><bdo id='Q8NBj'><pre id='Q8NBj'><center id='Q8NBj'></center></pre></bdo></b><th id='Q8NBj'></th></span></q></dt></tr></i><div id='Q8NBj'><tfoot id='Q8NBj'></tfoot><dl id='Q8NBj'><fieldset id='Q8NBj'></fieldset></dl></div>
            <tbody id='Q8NBj'></tbody>
          <tfoot id='Q8NBj'></tfoot>

              <bdo id='Q8NBj'></bdo><ul id='Q8NBj'></ul>

                <small id='Q8NBj'></small><noframes id='Q8NBj'>

                本文介绍了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 网格复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                <small id='smB1Y'></small><noframes id='smB1Y'>

                    <tbody id='smB1Y'></tbody>

                  <legend id='smB1Y'><style id='smB1Y'><dir id='smB1Y'><q id='smB1Y'></q></dir></style></legend>
                    • <bdo id='smB1Y'></bdo><ul id='smB1Y'></ul>
                        <i id='smB1Y'><tr id='smB1Y'><dt id='smB1Y'><q id='smB1Y'><span id='smB1Y'><b id='smB1Y'><form id='smB1Y'><ins id='smB1Y'></ins><ul id='smB1Y'></ul><sub id='smB1Y'></sub></form><legend id='smB1Y'></legend><bdo id='smB1Y'><pre id='smB1Y'><center id='smB1Y'></center></pre></bdo></b><th id='smB1Y'></th></span></q></dt></tr></i><div id='smB1Y'><tfoot id='smB1Y'></tfoot><dl id='smB1Y'><fieldset id='smB1Y'></fieldset></dl></div>
                      • <tfoot id='smB1Y'></tfoot>