如何从for循环tkinter python获取checkbuttons的值

时间:2018-03-11 14:34:45

标签: python loops for-loop tkinter

我是tkinter编程的新手,我非常坚持使用checkbuttons。我一次创建了多个检查按钮,每个按钮都有不同的文本和不同的网格位置。但是我不知道如何获取每个按钮的值或如何设置它。我希望能够获得每个按钮的状态/值,如果选中它,则调用另一个函数。如何设置和调用每个按钮的值/状态?这可以在for循环中完成,还是我必须单独创建它们?

    def CheckIfValid(self, window):
        Class = self.ClassChosen.get()
        Unit = self.UnitChosen.get()
        Topic = self.TopicChosen.get()
        if Class == '' or Unit == '' or Topic == '':
            tm.showinfo("Error", "Please fill in all boxes")

        else:
            QuestionData = OpenFile()
            QuestionsList = []
            for x in range (len(QuestionData)):
                #if QuestionData[x][2] == Topic:
                    QuestionsList.append(QuestionData[x][0])


            for y in range(len(QuestionsList)):
                self.ButtonVal[y] = IntVar()
                Checkbutton(window, text = QuestionsList[y], padx = 20, variable = self.ButtonVal[y]).grid(row = 12 + y, column = 2)
            ConfirmSelection = Button(window, text = "Set Homework", command = lambda: SetHomeworkClass.ConfirmHomework(self)).grid()
            print(variable.get()) #here I would like to be able to get the value of all checkbuttons but don't know how

1 个答案:

答案 0 :(得分:1)

使用从Checkbutton中的command =或Button中调用的IntVar列表。不知道你为什么要调用另一个类的对象,SetHomeworkClass.objectConfirmHomework(self)。它看起来不像你编程的那样,因为那是另一个名称空间,IntVars列表在这个名称空间中,但这是另一个主题的另一个主题。

 (function() {
   var dropzone = document.getElementById("dropzone");

  dropzone.ondrop = function(event) {
    event.preventDefault();
    this.className = "dropzone";

    console.log(event.dataTransfer.files[0]);

    window.onload = function() {
      var fileInput = document.getElementById('dropzone');
      var fileDisplayArea = document.getElementById('displayarea');

      fileInput.addEventListener('dropzone.ondrop', function(read) {
        var file = fileInput.files[0];
        var textType = /text.*/;

        if (file.type.match(textType)) {
          var reader = new FileReader();

          reader.onload = function(read) {
            fileDisplayArea.innerText = reader.result;
          }

          reader.readAsText(file);
          }

          else {
            fileDisplayArea.innerText = "File not supported!";
          }
      });
  }
  }

  dropzone.ondragover = function() {
    this.className = "dropzone dragover";
    return false;
  };

  dropzone.ondragleave = function() {
    this.className = "dropzone";
    return false;
  };

  }())