如何在Pygubu中设置复选框状态

时间:2018-07-12 11:07:51

标签: python tkinter

一段时间以来,我一直在尝试通过代码设置tkinter复选框状态,但我遇到了麻烦。

尝试了.toggle().set().select(),但是我收到“对象没有属性”

在使用pygubu时是否还有另一种访问tkinter对象方法的方法?

有什么想法吗?

import os
import tkinter as tk  # for python 3
import pygubu

CURDIR = os.path.dirname(__file__)
UI_FILE = os.path.join(CURDIR, 'gui2.ui')

class Application:
    def __init__(self):
        #1: Create a builder
        self.builder = builder = pygubu.Builder()
        #2: Load an ui file
        builder.add_from_file(UI_FILE)
        #3: Create the widget using a master as parent
        self.mainwindow = builder.get_object('toplevel1')
         #4: connect callbacks
        self.builder.connect_callbacks(self)

    def on_print(self):
        checked = self.builder.get_object('Checkbutton_1')
        checked.toggle()

    def run(self):
        self.mainwindow.mainloop()

if __name__ == '__main__':
    app = Application()
    app.run()

1 个答案:

答案 0 :(得分:0)

我设法通过设置与pygubu中的复选框相关联的变量(设计器窗格的右下方),然后在代码中获取该变量并进行更改来做到这一点。我是在应用类的__init__中设置默认值的。

假定连接到复选框的变量称为boxChecked,并且其类型设置为boolean

checked = builder.get_variable("boxChecked")
checked.set(True)

然后您可以使用checked.get()

从变量中获取复选框的状态。

我认为必须有一种“更聪明”的方式来做到这一点,但这对我有用。

相关问题