Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add multiply to ttkspacer #317

Conversation

SZRabinowitz
Copy link
Contributor

  • Override the multiplication dunder for ttkspacer to support creating multiple spacers at a time.

for example:

row_2.addWidgets([ttk.TTkButton(text="Submit"), *ttk.TTkSpacer() * 3])

I want to add a test but I cannot figure out the naming scheme of the t.ui tests.

So Here is the test:

import TermTk as ttk

root = ttk.TTk(layout=ttk.TTkVBoxLayout())
frame_1 = ttk.TTkFrame(title="Without Spacer", border=True, layout=ttk.TTkHBoxLayout(), parent=root)
frame_1.layout().addWidgets([ttk.TTkButton(text="Button 1")])

frame_2 = ttk.TTkFrame(title="With 1 Spacer", border=True, layout=ttk.TTkHBoxLayout(), parent=root)
frame_2.layout().addWidgets([ttk.TTkButton(text="Button 2"), ttk.TTkSpacer()])

frame_3 = ttk.TTkFrame(title="With multiple Spacer", border=True, layout=ttk.TTkHBoxLayout(), parent=root)
frame_3.layout().addWidgets([ttk.TTkButton(text="Button 3"), *ttk.TTkSpacer() * 100])

root.mainloop()

- Override the multiplication dunder for ttkspacer to support creating multiple spacers at a time.

for example:
```
row_2.addWidgets([ttk.TTkButton(text="Submit"), *ttk.TTkSpacer() * 3])
```
@SZRabinowitz
Copy link
Contributor Author

I can see multiple use cases for this.

In the test I provided, I used it to make sure the button stays it's minimum size. Is there a better way to accomplish that?

@ceccopierangiolieugenio
Copy link
Owner

I can see multiple use cases for this.

In the test I provided, I used it to make sure the button stays it's minimum size. Is there a better way to accomplish that?

yes, any widget has a minimum and maximum size, just define the maximum size the value that you need.

@SZRabinowitz
Copy link
Contributor Author

yes, any widget has a minimum and maximum size, just define the maximum size the value that you need.

Thanks for the answer. In the above example, it would shrink the entire frame.

I guess the right approach would be to set the minimum width and add only a single spacer.

@ceccopierangiolieugenio
Copy link
Owner

unfortunately i cannot accept this PR, it will add a use case that was not intended for the layout mechanism.
I'd rather add (and I will eventually) a form layout to address this issue.
instead of filling the layout with hundreds of spacers (which memory footprint could be significant), I would write something like:

l = ttk.TTkGridLayout()
l.addWidget(ttk.TTkButton(<init something>), 0,0)
l.addItem(ttk.TTkLayout(),0,1,1,99)

an empty layout works as well as a spacer and it is lighter.
i assign it 99 colspan in order to force the layout to use 1/100 width for the button and 99/100 for the spacer, still I would not choose this solution either but rather plan the sizes of the elements and define their max/min dimension.

@SZRabinowitz SZRabinowitz deleted the enhancement/spacer-multiplication branch January 24, 2025 09:52
@SZRabinowitz
Copy link
Contributor Author

Great explanation! Thank you for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants