[Tutor] Pygubu designer question

Alan Gauld alan.gauld at yahoo.co.uk
Tue Feb 1 04:32:02 EST 2022


On 01/02/2022 06:53, Phil wrote:
> I've spent most of the afternoon experimenting with Pygubu builder.

Caveat: I've never heard of Pygubu before let alone used it...

> uses a callback mechanism rather than the bind method that I'm more 
> familiar with. 

bind is a callback mechanism.
A callback is simply a function that gets called when an event occurs.
bind associates a callback function with an event.
pygubu must do the same somehow.

>          self.entry1.configure(validatecommand=self.callback)

The idea behind validatecommand is that you have a function which
checks that the contents of the box is valid data - for example
its numeric or looks like an email address or whatever.
But of course you could use it to process the data too.

> My programme includes the following:
> 
> # define the function callbacks
> def on_button1_click():
>      messagebox.showinfo('Message', 'You pressed me') # no problems here

This is a standalone function, outside of any class(no self)

> def callback(self):
>      #print(entry1.get()) # entry1 is unknown and self.entry1 didn't 

Depending where you put this iyt cxould be a method of a class (self) or
it could be a standalone function that just happens to take a parameter
called self. Its not clear which. If it in inside the class then
self.entry1 should work. If its outside the class it won't.

>          # Configure callbacks
>          callbacks = {
>              'on_button1_clicked': on_button1_click,
>              'callback' : callback # This doesn't look to be correct
>          }
> 
>          builder.connect_callbacks(callbacks)

I have no idea what this does, it is obviously a pygubu construct.
But it doesn't seem to tally with the validatecommand code above.

> The word 'here' is printed when the programme is first run. I press the 

That suggests that your callback is indeed a standalone function
rather than a method and that it is somehow being called - search
for "callback(" to see if it is being called rather than referenced.

> The error message is Exception in Tkinter callback
> Traceback (most recent call last):
>    File "/usr/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
>      return self.func(*args)
> TypeError: 'NoneType' object is not callable

I'm guesssing that somehere theres a place where you should have passed
callback but you've pased callback() Calling the function which returns
None and that has been stored as the actuall callback function. Then
when tkinter tries to call the function is hits None and you get this error.

> I know that I need to read up on Tkinter but someone who may be familiar 
> with callbacks and Pygubu may be able to help.

As I said above there is nothing special about callbacks. It's how
all Tkinter (and indeed all GUIs) function. They are just
functions that get passed into the GUI for future use. bind()
is one way of setting a callback, the widget options are another.
How pygubu does it I don;t know but somewhere under the hood
it will boil down to a bind() or widget option.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list