我用GUI制造商pygubu创建了一个窗口。 该窗口包含复选框。 但我的脚本无法识别是否标记了复选框。 如何检查复选框是否已标记? 我是否要使用命令或变量? 什么是正确的语法?
下面你可以看到我在pygubu中做了什么。 如何获取复选框的状态? http://i.stack.imgur.com/W9o5A.jpg
我试过了:
def checker13(self, variable=check13): self.variable13 = variable print self.variable13这应该在每次更改时打印出复选框的状态。 但我总是收到一个错误。 我能做什么?
I created a window with the GUI maker pygubu. This window contains checkboxes. But my script is not able to recognize if the checkboxes are marked or not. How do I have to check if the checkboxes are marked or not? Do Ihave to use the command or the variable? What is the right syntax?
Below you can see what I have done in pygubu. How do I get the state of my checkbox? http://i.stack.imgur.com/W9o5A.jpg
I tried:
def checker13(self, variable=check13): self.variable13 = variable print self.variable13This should print me the state of the checkbox everytime something changed. But I always receive an error. What can I do?
最满意答案
根据这里的例子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)。
编辑:更具体地回答您的问题; 你可以检查var1和var2的结果,因为它们将等于0表示未检查, 1表示检查。 希望这可以帮助。
Finally I found an inelegant way to do it. It's not very "pretty", but it works:
self.xxx1 = 0 def checker13(self): if self.xxx1 == 0: self.xxx1 = self.xxx1+1 else: self.xxx1 = 0Normally the checkbox is not marked and the value is 0. If the checkbox gets marked there is an event and the value changes to 1. The next event changes the value back to 0. By this method I can check the state of the checkbox by checking xxx1.
I knew there are other ways (with IntVar()) but I was specifically searching for a solution with pygubu. If there is a smoother answer feel free to correct me.
EDIT: to get the value of an variable from pygubu use:
variable = self.builder.get_variable('variable') variable = variable.get()更多推荐
发布评论