简体   繁体   English

使用Python和Glade的gtk3中的小部件转发器

[英]Widget repeaters in gtk3 using python and glade

I am trying to develop applications for gtk3 using python. 我正在尝试使用python开发gtk3的应用程序。 I have designed the UI using glade. 我使用林间空地设计了UI。 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. 在我的应用程序中,我有10个标签小部件,它们基于数据数组显示不同的数据。 Now I have to call the gbuilder.get_object() method every time I need to get the desired object. 现在,每次需要获取所需对象时,我都必须调用gbuilder.get_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): 例如,要获得第7项,只需对列表建立索引(请注意,索引以0开头):

widget_list[6]

The purpose of {0} is generate the names of the widgets: {0}的目的是生成小部件的名称:

>> ['<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 有关如何使用format更多信息,请查看格式规范迷你语言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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