I've been trying to set Tkinter-checkbox
state via code for some time now and I'm stuck.
Tried .toggle()
, .set()
, .select()
but Im getting "object has no attribute"
Is there another way to access Tkinter
object methods while using pygubu
?
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. I did it in the __init__
of the app class to set defaults.
Assuming the variable connected to the checkbox is called boxChecked
and has its type set to boolean
checked = builder.get_variable("boxChecked")
checked.set(True)
You can then get the state of the checkbox from the variable with checked.get()
I assume there must be a 'nicer' way to do this, but this worked for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.