在 tkinter (pygubu) 中使用复选框

Working with checkboxes in tkinter (pygubu)

我用 GUI 制造商 pygubu 创建了一个 window。 window 包含复选框。但是我的脚本无法识别复选框是否被标记。 我如何检查复选框是否被标记? 我必须使用命令还是变量?什么是正确的语法?

下面你可以看到我在pygubu中做了什么。如何获取复选框的状态? http://i.stack.imgur.com/W9o5A.jpg

我试过了:

def checker13(self, variable=check13):
    self.variable13 = variable
    print self.variable13

这应该会在每次发生更改时向我显示复选框的状态。但我总是收到一个错误。我能做什么?

根据此处的示例http://www.python-course.eu/tkinter_checkboxes.php

from tkinter import *
master = Tk()
var1 = IntVar()
Checkbutton(master, text="male", variable=var1).grid(row=0, sticky=W)
var2 = IntVar()
Checkbutton(master, text="female", variable=var2).grid(row=1, sticky=W)
mainloop()

您需要设置一个变量来存储复选框的结果,然后引用它(在本例中是 var1 和 var2)。

编辑:更具体地回答您的问题;您可以检查 var1var2 的结果,因为它们等于 0(未选中)或 1(已选中)。希望这有帮助。

感谢您澄清您的问题。现在我知道您正在使用 Pygubu 来构建您的 GUI,我可以进一步研究一下。我发现以下文档页面可以帮助您解决问题: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Checkbutton.html

要注意的关键 Checkbutton 选项是 variable 选项。在您提供的图片中,您似乎尝试将 variable 选项设置为 var。上面的文档是关于 variable 选项的:

A control variable that tracks the current state of the checkbutton... Normally you will use an IntVar here, and the off and on values are 0 and 1, respectively.

您是否可以选择将 variable 选项更改为 IntVar,而不是图片中当前的 int?如果没有,那应该还是可以的。

因此,变量 var 将存储 Checkbutton 的状态。在您提供的图片中,您将 command 选项设置为 checker13。保持这样。现在,由于我从未使用过 Pygubu(甚至 Tkinter),我不能确定这段代码是否有效,但请尝试这样的事情:

def checker13(self):
    print(self.var)

如果这不起作用,试试这个:

def checker13(self):
    print(self.variable)

如果其中一个有效,请告诉我,以便我可以使用有效代码编辑我的答案。如果您仍然遇到困难,请告诉我。

最后我找到了一个不优雅的方法。不是很好 "pretty",但它有效:

self.xxx1 = 0 
def checker13(self):
    if self.xxx1 == 0:
        self.xxx1 = self.xxx1+1
    else:
        self.xxx1 = 0 

通常不勾选复选框,值为0。如果复选框被标记,则有一个事件并且值更改为 1。下一个事件将值更改回 0。通过这种方法,我可以通过检查 xxx1.

来检查复选框的状态

我知道还有其他方法(使用 IntVar()),但我专门用 pygubu 寻找解决方案。如果有更流畅的答案欢迎指正。

编辑: 从 pygubu 获取变量的值使用:

variable = self.builder.get_variable('variable')
variable = variable.get()

我刚遇到这个问题。下面是一个只处理复选框的简化解决方案。

checkbox_example.py内容:

import tkinter as tk
import pygubu

class App:
    def __init__(self, master):
        self.master = master
        self.builder = builder = pygubu.Builder() # Create builder
        builder.add_from_file('checkbox_example.ui') # Load ui file.

        # Create widget using master as parent
        self.mainwindow = builder.get_object('mainwindow', master)

        builder.connect_callbacks(self) # Connect callbacks

        # Create check button object
        self.check_button_object = builder.get_object('cb0', self.mainwindow)

    def on_checkbox_clicked(self):
        print("on_checkbox_clicked")
        checked = self.builder.get_variable('checked') # Get variable
        print(checked.get()) # get value of checked and print it

if __name__ == '__main__':
    root = tk,Tk()
    app = App(root)
    root.mainloop()

而checkbox_example.ui的内容是:

<?xml version='1.0' encoding='utf-8'?>
<interface>
  <object class="tk.Frame" id="mainwindow">
    <property name="height">200</property>
    <property name="width">200</property>
    <layout>
      <property name="column">0</property>
      <property name="propagate">True</property>
      <property name="row">0</property>
      <property name="sticky">nw</property>
    </layout>
    <child>
      <object class="tk.Checkbutton" id="cb0">
        <property name="command">on_checkbox_clicked</property>
        <property name="cursor">arrow</property>
        <property name="font">TkDefaultFont</property>
        <property name="offvalue">0</property>
        <property name="onvalue">1</property>
        <property name="pady">0</property>
        <property name="text" translatable="yes">checkbox_string</property>
        <property name="underline">0</property>
        <property name="variable">int:checked</property>
        <layout>
          <property name="column">0</property>
          <property name="propagate">True</property>
          <property name="row">1</property>
        </layout>
      </object>
    </child>
  </object>
</interface>

为了看到它被执行,我创建了一个简短的 youtube 剪辑: https://youtu.be/Y6lpi3oihOo