简体   繁体   English

Pygtk gtk.Entry()和多个get_text()答案

[英]Pygtk gtk.Entry() and multiple get_text() answers

I'm using pygtk to make a questionnaire type application and in particular I'm struggling with one aspect. 我正在使用pygtk进行调查表类型的应用程序,尤其是我在一个方面苦苦挣扎。

I've tried to design it so that you can ask one or two people at a time. 我已经尝试过设计它,以便您一次可以问一个或两个人。 So I have radio buttons that the user can select between either person one or person two. 因此,我有一个单选按钮,用户可以在第一个人或第二个人之间进行选择。 The reason I've done it this way is so that I only need to use one set of gtk.Entry() widgets, gtk.ComboBox() widgets,...etc. 我这样做的原因是,我只需要使用一组gtk.Entry()小部件, gtk.ComboBox()小部件等。

So what I currently have set up is an "assign data" method where, depending on which person you've asked, it saves the get_text() information to a specific variable. 因此,我目前设置的是“分配数据”方法,根据您所询问的人,该方法将get_text()信息保存到特定变量中。

I was wondering if there was some cleverer way of having the specific person's information for the fields they've already entered appear in the widgets when you select them with the radio buttons other than just a long list of widget.set_text() = specific_variable ? 我想知道是否有某种巧妙的方法,使您使用单选按钮(而不是一长串的widget.set_text() = specific_variable选择单选按钮时,将已经输入的特定人员的信息显示在小部件中。

Thanks 谢谢

Well, if you named your widgets (eg, in a GtkBuilder file or manually), you can do the following: 好吧,如果您为窗口小部件命名(例如,在GtkBuilder文件中或手动命名),则可以执行以下操作:

widgets = [w for w in container_widget.get_children() if hasattr(w, "get_text")]
value_dict = {}

# save values to dictionary
for widget in widgets:
    value_dict[widget.get_name()] = widget.get_text()

# load values from dict
for widget in widgets:
    widget.set_text(value_dict[widget.get_name()])

If the widgets don't have names, you can just use enumerate , but that is a little more fragile. 如果小部件没有名称,则可以使用enumerate ,但这会更脆弱。

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

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