关于“Python GTK 添加信号到组合框”的问题/ 于 2010 年 4 月 24 日 18:21 回答
我对 Python 和 pygtk 很陌生,我想在主窗口中使用组合的更改值
,但我不能!
我需要一些帮助
提前致谢
最诚挚的问候
I'm very new to Python and pygtk , I want to use the changed value of the combo
in the main window , but I can't !
I need some help
Thanks in advance
Best regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有代码,肯定很难提供帮助,但我会尝试一下...
从您的问题来看,我猜测您希望在用户更改组合框后检索组合框的值。
您将需要创建一个新的事件处理程序来检索用户更改时的值。为了确保整个模块可以使用这些数据,我们首先在模块的顶部创建一个全局变量。 这必须位于所有类和定义之上和之外!
在这里,我们创建了一个名为“combo_value”的变量,并将其值设置为 -1。这很重要,因为首先,它将变量定义为“integer”类型,其次,如果组合框中未选择任何内容,则“-1”是下面代码返回的值。
现在,在包含 pygtk 代码的类中,放置此定义。我更喜欢将所有事件处理程序放在“__ init __”定义中,因为这使它们更易于访问。
现在我们需要使用“已更改”信号将组合框连接到此事件。
现在你就得到了它!然后,您可以通过检查combo_value 变量的值来连接所有其他进程。请记住 - 此代码将该变量设置为组合框中所选项目的索引,而不是文本值!记住这一点非常重要,因为如果您尝试检查这个变量,它不会给你带来任何好处。
如果未选择任何内容,该值将为“-1”。请记住,所有项目的索引都从零开始计数。记下组合框的值及其索引可能会很有用,以供参考。它可能看起来像这样:
然后,在使用组合框值的代码中,您可能会有一些类似这样的内容:
等等,等等。
所以你可以在上下文中看到所有内容,这是完整的代码,减去上面我的老生常谈的小例子......
我希望这有帮助!同样,如果没有代码,就很难向您提供具体信息。我正在帮助您,因为您是新来的,但请务必针对未来的所有问题发布您项目中的具体示例和代码。
干杯!
Without code, it is rather difficult to help for sure, but I'll take a stab at this...
From your question, I am guessing that you're wanting to retrieve the value of the combo box once the user changes it.
You're going to need to create a new event handler to retrieve the value upon the user changing. In order to ensure that the entire module can use this data, we'll start by creating a global variable at the TOP of your module. This must be above and outside of all classes and definitions!
Here, we've created a variable called "combo_value", and set it's value to -1. This is important because, first of all, it defines the variable as type 'integer', and second, '-1' is the value returned by the code below if nothing is selected in the combo box.
Now, in the class where you have your pygtk code, put this definition. I prefer putting all my event handlers inside the "__ init __" definition, as it makes them easier to access.
Now we need to connect our combo box to this event using the "changed" signal.
And there you have it! You can then hook up all of your other processes by checking the value of the combo_value variable. Just remember - this code sets that variable to the INDEX of the selected item in the combo box, and not the text value! This is extremely important to remember, because if you try and check for a string in this variable, it'll get you nowhere.
The value will be "-1" if nothing is selected. Remember to count from zero for the index of all your items. It may be useful to write down the values of your combo box and their indexes, for reference. It might look something like this:
Then, in the code for working with the combo box value, you may have a little something like this:
And so on, and so forth.
So you can see everything in context, here is the entire code, minus my corny little example above...
I hope this helps! Again, without code, it is very hard to give you specifics. I'm helping you out, since you're new here, but please be sure to post specific examples and code from your project on all future questions.
Cheers!