简体   繁体   中英

Widget repeaters in gtk3 using python and glade

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM