0

I am trying to develop applications for gtk3 using python. I have designed the UI using glade. I want to know if there is any way of using widget arrays, in which each widget of similar type would have the same name, but with a different index. It would help in reducing code to a great extent.

In my application, I have 10 label widgets which display different data, based on an array of data. Now I have to call the gbuilder.get_object() method every time I need to get the desired object. If I were able to use widget arrays, it would really help in reducing the code redundancy.

1 Answer 1

1

If you have named the widgets in glade like this:

  • <widget_name>_1
  • <widget_name>_2
  • ...
  • <widget_name>_n

It would be easy to create such a list of widgets in your application like this:

widget_list = [builder.get_object('<widget_name>_{0}'.format(i))
               for i in range(1, n+1)]

To get, for example, item 7, all you need is index the list (note that indexes start with 0):

widget_list[6]

The purpose of {0} is generate the names of the widgets:

>> ['<widget_name>_{0}'.format(i)) for i in range(1, 4)
['<widget_name>_1', '<widget_name>_2', '<widget_name>_3']

For more information about how to use format, please have a look at the format specification mini-language

Sign up to request clarification or add additional context in comments.

4 Comments

and how do i use the data from <widget_name>_7 from the list. also, what is the purpose of the {0} ?
@GauravSood I've edited my answer to add the information you asked in the comment.
i got the method. just one question. i have 10 button, which correspond to the same method upon click. i have used a list. there is usage of dictionary for the various signals emitted. how do i add the list of buttons to the dict and should i use the same signal for all the buttons? also, in the method, how am i going to check which button was clicked? the dict is of this format: <code>dic={'on_<widget name>_clicked':method, 'on_<widget name>_clicked':method1} builder.connect_signals(dic) <code>
Please for the benefit of other people that might have the same problem in the future, don't use comments to ask new questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.