使用tkinter中的复选框(pygubu)(Working with checkboxes in tkinter (pygubu))

编程入门 行业动态 更新时间:2024-10-10 19:16:51
使用tkinter中的复选框(pygubu)(Working with checkboxes in tkinter (pygubu))

我用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.variable13

This 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 = 0

Normally 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()

更多推荐

本文发布于:2023-07-30 14:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   tkinter   pygubu   checkboxes   Working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!