Page 1 of 1

Python newbie - need help on script button / choice

Unread postPosted: Sat Jul 31, 2021 6:50 pm
by netisfred
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 !

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

Unread postPosted: Tue Aug 03, 2021 1:14 pm
by anemoff

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)

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.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

def get_choice(self):
return self.choice

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


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

Unread postPosted: Wed Aug 04, 2021 6:40 pm
by netisfred
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 !