Replies: 2 comments
-
Hello @AJ6CU, The designer does not allow you to enter python code in command properties, only methods or function names. Some times you need only one callback to manage a group of widgets. In those cases you use the "Send widget id" option of the command. An example: #!/usr/bin/python3
import pathlib
import pygubu
PROJECT_PATH = pathlib.Path(__file__).parent
PROJECT_UI = PROJECT_PATH / "discussion164.ui"
class Discussion164App:
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_button_clicked(self, widget_id):
if widget_id == "action_a":
print("Action A")
elif widget_id == "action_b":
print("Action B")
else:
print("Action C")
if __name__ == "__main__":
app = Discussion164App()
app.run() discussion164.ui <?xml version='1.0' encoding='utf-8'?>
<interface version="1.3">
<object class="tk.Toplevel" id="toplevel1">
<property name="height">200</property>
<property name="title" translatable="yes">Actions Test</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="side">top</property>
</layout>
<child>
<object class="ttk.Button" id="action_a" named="True">
<property name="command" type="command" cbtype="with_wid">on_button_clicked</property>
<property name="text" translatable="yes">Action A</property>
<layout manager="pack">
<property name="side">top</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Button" id="action_b" named="True">
<property name="command" type="command" cbtype="with_wid">on_button_clicked</property>
<property name="text" translatable="yes">Action B</property>
<layout manager="pack">
<property name="side">top</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Button" id="action_c" named="True">
<property name="command" type="command" cbtype="with_wid">on_button_clicked</property>
<property name="text" translatable="yes">Action C</property>
<layout manager="pack">
<property name="side">top</property>
</layout>
</object>
</child>
</object>
</child>
</object>
</interface> |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hi,
When I select the custom widget object in code generation with the file you provided I get the error:
C:\Users\markj\PycharmProjects\tkinkerscratch\venv\Scripts\python.exe C:/Users/markj/PycharmProjects/tkinkerscratch/validate.py
Traceback (most recent call last):
File "C:\Users\markj\PycharmProjects\tkinkerscratch\validate.py", line 87, in <module>
widget.pack(expand=True, fill="both")
AttributeError: 'TestWidget' object has no attribute 'pack'
If I select Code Script it works.
If I select application I get the error message:
import pygubu
ModuleNotFoundError: No module named 'pygubu'
This latter one is probably because I messed up setting up the search path, so no worth tracking down. But what about the error message when selecting “custom widget”?
Thanks
Mark
From: Alejandro Autalán ***@***.***>
Sent: Saturday, November 19, 2022 10:43 AM
To: alejandroautalan/pygubu-designer ***@***.***>
Cc: AJ6CU ***@***.***>; Mention ***@***.***>
Subject: Re: [alejandroautalan/pygubu-designer] Is it possible to use a lambda function as a command parameter? (Discussion #164)
Hello @AJ6CU <https://github.com/AJ6CU> ,
The designer does not allow you to enter python code in command properties, only methods or function names.
Some times you need only one callback to manage a group of widgets. In those cases you use the "Send widget id" option of the command.
An example:
<https://user-images.githubusercontent.com/3482471/202866342-b6500552-6d91-4b14-8c3d-c7f00dadabd0.png>
#!/usr/bin/python3
import pathlib
import pygubu
PROJECT_PATH = pathlib.Path(__file__).parent
PROJECT_UI = PROJECT_PATH / "discussion164.ui"
class Discussion164App:
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_button_clicked(self, widget_id):
if widget_id == "action_a":
print("Action A")
elif widget_id == "action_b":
print("Action B")
else:
print("Action C")
if __name__ == "__main__":
app = Discussion164App()
app.run()
discussion164.ui
<?xml version='1.0' encoding='utf-8'?>
<interface version="1.3">
<object class="tk.Toplevel" id="toplevel1">
<property name="height">200</property>
<property name="title" translatable="yes">Actions Test</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="side">top</property>
</layout>
<child>
<object class="ttk.Button" id="action_a" named="True">
<property name="command" type="command" cbtype="with_wid">on_button_clicked</property>
<property name="text" translatable="yes">Action A</property>
<layout manager="pack">
<property name="side">top</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Button" id="action_b" named="True">
<property name="command" type="command" cbtype="with_wid">on_button_clicked</property>
<property name="text" translatable="yes">Action B</property>
<layout manager="pack">
<property name="side">top</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Button" id="action_c" named="True">
<property name="command" type="command" cbtype="with_wid">on_button_clicked</property>
<property name="text" translatable="yes">Action C</property>
<layout manager="pack">
<property name="side">top</property>
</layout>
</object>
</child>
</object>
</child>
</object>
</interface>
—
Reply to this email directly, view it on GitHub <#164 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/AQXOXTFG4XV5XIHI3SVYV7DWJENTZANCNFSM6AAAAAASFCGLUY> .
You are receiving this because you were mentioned. <https://github.com/notifications/beacon/AQXOXTBJTU7KUDPCKMRISEDWJENTZA5CNFSM6AAAAAASFCGLU2WGG33NNVSW45C7OR4XAZNRIRUXGY3VONZWS33OINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAH7OQG.gif> Message ID: ***@***.*** ***@***.***> >
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Would like the command for a button to be something like "lambda: self.button_clicked(1)" Tried entering that directly and just get the read error box. When I selected 'send widget id', the actual command definition has no parameter and while the callback definition does, and python is not happy.
Thanks!
Mark
Beta Was this translation helpful? Give feedback.
All reactions