循环创建的Tkinter复选框 [英] Tkinter checkboxes created in loop

查看:92
本文介绍了循环创建的Tkinter复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做我的第一个Tkinter项目,并使用了几个stackoverflow的答案和解释(以及它们导致的其他链接)作为试图了解如何构建应用程序的基础。

I'm working on my first Tkinter project, and have used several stackoverflow answers and explanations (and other links they lead to) as the basis for trying to understand how to build my application.

阅读此问题(以及大多数已接受答案的链接)后,我构建了应用程序:

I structured my application after reading this question (and most of the links from the accepted answer): Switch between two frames in tkinter

在我的一个框架中,我需要使用for循环创建复选框。我发现此页面有帮助:如何从python tkinter的for循环中的列表中创建多个复选框

In one of my frames, I need to create checkboxes using a for loop. I found this page helpful: How do I create multiple checkboxes from a list in a for loop in python tkinter

我很难获取所有

我的代码的相关部分如下(python2.7):

The relevant part of my code is as follows (python2.7):

import Tkinter as tk

class Main(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        ...
        ## gets set on a different frame in the application
        self.files_list = []

class A(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        ...
        ## self.f_list contains the values (a list of dictionaries) that I am expecting on this frame
        self.f_list = controller.files_list

        for f in self.f_list:
            self.file_name = tk.StringVar()
            self.file_name.set(f['file'])
            self.run_file = tk.IntVar()
            self.run_file.set(1)
            cb = tk.Checkbutton(self, text=self.file_name.get(), variable=self.run_file)
            cb.pack()

这将产生我期望的文件名列表,每个都有一个复选框。但是,运行时仅检查从循环产生的最后一个复选框。

This produces the list as I would expect of "file names", each with a checkbox. However, only the last checkbox produced from the loop is checked when run.

在调用pack方法之前,我先放置了一条print语句来打印self.run_file.get()并在每次循环中打印出1值。

Before calling the pack method, I put a print statement to print self.run_file.get() and each time through the loop it prints a value of 1.

我尝试了几种不同的方式更改循环:

I tried changing my loop several different ways :

## same behavior
self.run_file = tk.Variable()
self.run_file.set(1) 

## same behavior
self.run_file = tk.IntVar(value=1)

## no checkboxes set
cb = tk.Checkbutton(self, text=self.file_name.get(), variable=self.run_file.get())

我觉得自从价值每次循环时.file_name都不相同,那里没有问题。由于默认情况下选中了最后一个复选框,因此我觉得以前的复选框中的值正在丢失,但是我不知道如何构造复选框或self.run_file变量,因此默认情况下选中了每个复选框。阅读以下问题后,我在for循环中的变量上使用了self: Python,Tkinter:如果默认情况下是否有方法可以选中复选框?

I feel like since the value of self.file_name is different each time through the loop there is no issue there. Since the last checkbox is checked by default, it makes me feel that the value is getting lost on the previous checkboxes but I don't know how to structure my checkboxes or the self.run_file variable so that each box is checked by default. I'm using self on the variables in the for loop after reading this question: Python, Tkinter : if there a way to check checkboxes by default?

我研究了围绕该主题的许多不同问题,但是仍然无法给出正确的答案。有人可以指出我正确的方向吗?

I have looked at numerous different questions around this topic, but, still can't come up with the correct answer. Can someone point me in the right direction?

推荐答案

您的问题是 self.run_file for 循环中的每次迭代中,c $ c>都会被覆盖。为确保每个复选框的 IntVar 不被覆盖,请分别单独存储,例如在列表中:

Your problem is that self.run_file is being overwritten at each iteration in the for loop. To make sure that the IntVar of each checkbox are not overwritten, store them separately, for example in a list:

self.run_file_IntVars = []

for i, f in enumerate(self.f_list):
    self.run_file_IntVars.append(tk.IntVar(value=1))
    cb = tk.Checkbutton(self, text=f['file'], 
                        variable=self.run_file_IntVars[i])
    cb.pack()

这篇关于循环创建的Tkinter复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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