Isotropix Forums

Python newbie - need help on script button / choice

Clarisse Scripting related topics

Python newbie - need help on script button / choice

Unread postby netisfred » Sat Jul 31, 2021 6:50 pm

Hello,
I try to make a script starting with several choices, and I look for this choices to appear in a window with Buttons for each choices.
I saw on the forum the solution to do this as a list, but this presentation doesn't suit what I look for unfortunately.

Saw I tried, et tried again, but I really do not succeed to make those buttons work, especially to return the result as a variable, variable I would use to run the full script.

So to resume:
Choice A, choice B, C D etc. presented as buttons.
When choice "" is selected, store the result "choice"" and run the rest of the script depending of this result / variable.
And the best would be the window to direclty close when the button is clicked.

If anyone would have some solution for this, it would be really really great : )

Thank you !
netisfred
 
Posts: 4
Joined: Fri Sep 14, 2018 1:13 am

Re: Python newbie - need help on script button / choice

Unread postby anemoff » Tue Aug 03, 2021 1:14 pm

Hi,

There's no built-in GUI to do that, so you'll have to code it yourself.
Here is an example to get you started (tested on on 5.0 SP2):

python code

class ChoiceWindow(ix.api.GuiWindow):
def __init__(self):
evt_window = ix.application.get_event_window()
x = 0
y = 0
w = 200
h = 60
super(ChoiceWindow, self).__init__(evt_window, x, y, w, h, "Test")

self.choice = None

self.panel = ix.api.GuiPanel(self, 0, 0, w, h)
self.panel.set_constraints(ix.api.GuiWidget.CONSTRAINT_LEFT,
ix.api.GuiWidget.CONSTRAINT_TOP,
ix.api.GuiWidget.CONSTRAINT_RIGHT,
ix.api.GuiWidget.CONSTRAINT_BOTTOM)

padding = 4
btn_x = padding
btn_y = padding
btn_h = 20
btn_w = w - padding * 2
self.buttons = []
choices = ['A', 'B', 'C']
for choice in choices:
btn = ix.api.GuiPushButton(self.panel, btn_x, btn_y, btn_w, btn_h, choice)
self.buttons.append(btn)
self.connect(btn, 'EVT_ID_PUSH_BUTTON_CLICK', self.on_button_clicked)
btn_y += btn_h + padding

# update widget height to fit all buttons and padding
h = btn_y
# center window
x = evt_window.get_x() + evt_window.get_width() // 2 - w // 2
y = evt_window.get_y() + evt_window.get_height() // 2 - h // 2

self.resize(x, y, self.get_width(), h)

def on_button_clicked(self, sender, event_id):
# update choice from button label
self.choice = sender.get_label()
# close the window
self.hide()

def get_choice(self):
return self.choice


# Run it
window = ChoiceWindow()
window.show()
while window.is_shown():
ix.application.check_for_events()
print('Choice: {}'.format(window.get_choice()))


Cheers,
Anthony Nemoff
Isotropix
R&D Engineer
User avatar
anemoff
 
Posts: 413
Joined: Wed Jan 13, 2016 10:10 am

Re: Python newbie - need help on script button / choice

Unread postby netisfred » Wed Aug 04, 2021 6:40 pm

Hello Anthony,
Thank you so much for this ! I just tested it and it's perfect :) I didn't have a chance to find this. But with this I can continue the entire script :)

Thanks again !
netisfred
 
Posts: 4
Joined: Fri Sep 14, 2018 1:13 am


Return to Scripting
cron