[英]How to set Checkbox state in Pygubu
I've been trying to set Tkinter-checkbox
state via code for some time now and I'm stuck.我一直在尝试通过代码设置
Tkinter-checkbox
状态有一段时间了,但我被卡住了。
Tried .toggle()
, .set()
, .select()
but Im getting "object has no attribute"试过
.toggle()
, .set()
, .select()
但我得到“对象没有属性”
Is there another way to access Tkinter
object methods while using pygubu
?使用
pygubu
是否有另一种方法可以访问Tkinter
对象方法?
Any ideas on how to do it?关于如何做到这一点的任何想法?
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()
I managed to do this by setting a variable associated with the checkbox in pygubu (bottom right of the designer pane) then getting that variable in the code and changing it.我设法通过在 pygubu(设计器窗格的右下角)中设置一个与复选框关联的变量然后在代码中获取该变量并更改它来做到这一点。 I did it in the
__init__
of the app class to set defaults.我在 app 类的
__init__
中设置了默认值。
Assuming the variable connected to the checkbox is called boxChecked
and has its type set to boolean假设连接到复选框的变量称为
boxChecked
并且其类型设置为 boolean
checked = builder.get_variable("boxChecked")
checked.set(True)
You can then get the state of the checkbox from the variable with checked.get()
然后,您可以使用
checked.get()
从变量中获取复选框的状态
I assume there must be a 'nicer' way to do this, but this worked for me.我认为必须有一种“更好”的方法来做到这一点,但这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.