alejandroautalan / pygubu

A simple GUI builder for the python tkinter module
MIT License
2.04k stars 214 forks source link

Bindings callbacks with underscore are not called #280

Closed Sgambe33 closed 1 year ago

Sgambe33 commented 1 year ago

Describe the bug If a declare a binding in the bindings section and give a custom name to the callback that contains an underscore, the callback is not fired when the binding specifies.

To Reproduce Steps to reproduce the behavior:

  1. Go to 'Bindings'
  2. Click on '+'
  3. Add a new binding
  4. Give a custom name with an '_' somewhere to the callback
  5. Try to execute it from the GUI
  6. Nothing happens

Expected behavior I expect the callback to be fired anyway.

Your Environment (please complete the following information):

alejandroautalan commented 1 year ago

Hello @Sgambe33, thanks for your report. However, I can not reproduce it.

Can you explain the fifth step?

5 - Try to execute it from the GUI

I made the following example:

issue280.ui:

<?xml version='1.0' encoding='utf-8'?>
<interface version="1.3">
  <object class="tk.Toplevel" id="toplevel1">
    <property name="geometry">100x100</property>
    <property name="height">200</property>
    <property name="width">200</property>
    <child>
      <object class="ttk.Frame" id="frame1">
        <property name="height">200</property>
        <property name="padding">5</property>
        <property name="width">200</property>
        <layout manager="pack">
          <property name="expand">true</property>
          <property name="side">top</property>
        </layout>
        <child>
          <object class="ttk.Label" id="label1">
            <property name="text" translatable="yes">label1</property>
            <bind sequence="&lt;ButtonPress&gt;" handler="_on_label_clicked" add="" />
            <layout manager="pack">
              <property name="pady">0 20</property>
              <property name="side">top</property>
            </layout>
          </object>
        </child>
        <child>
          <object class="ttk.Entry" id="entry2">
            <bind sequence="&lt;KeyRelease&gt;" handler="_on_key_release" add="True" />
            <layout manager="pack">
              <property name="side">top</property>
            </layout>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

issue280.py:

#!/usr/bin/python3
import pathlib
import tkinter as tk
import pygubu
PROJECT_PATH = pathlib.Path(__file__).parent
PROJECT_UI = PROJECT_PATH / "issue280.ui"

class Issue280App:
    def __init__(self, master=None):
        self.builder = builder = pygubu.Builder()
        builder.add_resource_path(PROJECT_PATH)
        builder.add_from_file(PROJECT_UI)
        # Main widget
        self.mainwindow = builder.get_object("toplevel1", master)
        builder.connect_callbacks(self)

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

    def _on_label_clicked(self, event=None):
        print("Label clicked")

    def _on_key_release(self, event:tk.Event=None):
        print("Key pressed:", event.keysym)

if __name__ == "__main__":
    app = Issue280App()
    app.run()

I run the example and the bindings are working. If you have a little example let me know.

Regards Alejandro A.

Sgambe33 commented 1 year ago

That's strange. Perhaps it was a temporary thing?