关于“Python GTK 添加信号到组合框”的问题/ 于 2010 年 4 月 24 日 18:21 回答

发布于 2024-10-30 19:08:16 字数 103 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人如玉 2024-11-06 19:08:16

如果没有代码,肯定很难提供帮助,但我会尝试一下...

从您的问题来看,我猜测您希望在用户更改组合框后检索组合框的值。

您将需要创建一个新的事件处理程序来检索用户更改时的值。为了确保整个模块可以使用这些数据,我们首先在模块的顶部创建一个全局变量。 这必须位于所有类和定义之上和之外!

combo_value = -1

在这里,我们创建了一个名为“combo_value”的变量,并将其值设置为 -1。这很重要,因为首先,它将变量定义为“integer”类型,其次,如果组合框中未选择任何内容,则“-1”是下面代码返回的值。

现在,在包含 pygtk 代码的类中,放置此定义。我更喜欢将所有事件处理程序放在“__ init __”定义中,因为这使它们更易于访问。

def combo_changed(event, data=None):
    #This imports the combo_value variable declared above. Otherwise, the module 
    #would be creating a local variable instead, which would be of no use to the
    #rest of the program.
    global combo_value

    #This retrieves the combo box's selected index and sets the combo_value
    #variable to that index.
    combo_value = combobox.get_active()

现在我们需要使用“已更改”信号将组合框连接到此事件。

combobox.connect("changed", combo_changed)

现在你就得到了它!然后,您可以通过检查combo_value 变量的值来连接所有其他进程。请记住 - 此代码将该变量设置为组合框中所选项目的索引,而不是文本值!记住这一点非常重要,因为如果您尝试检查这个变量,它不会给你带来任何好处。

如果未选择任何内容,该值将为“-1”。请记住,所有项目的索引都从零开始计数。记下组合框的值及其索引可能会很有用,以供参考。它可能看起来像这样:

组合框(选择颜色)
“黑色”- 0
“白色”- 1
“红色”- 2
“绿色”- 3
“蓝色”- 4

然后,在使用组合框值的代码中,您可能会有一些类似这样的内容:

if combo_value == -1:
   pass
   #In other words, do nothing at all.
elif combo_value == 0
   #Set color to black
elif combo_value == 1
   #Set color to white

等等,等等。

所以你可以在上下文中看到所有内容,这是完整的代码,减去上面我的老生常谈的小例子......

combo_value = -1

class MyApplication:
   def __init__(self):
       #Your code here.
       def combo_changed(event, data=None):
           #This imports the combo_value variable declared above. Otherwise, the module 
           #would be creating a local variable instead, which would be of no use to the
           #rest of the program.
           global combo_value

           #This retrieves the combo box's selected index and sets the combo_value
           #variable to that index.
           combo_value = combobox.get_active()

       #Your GUI code is here.

       #This is where your combobox is created from a model (we're assuming it is
       #already declared before this point, and called "MyModel".
       combobox = gtk.ComboBox(MyModel)

       #Now we connect the "changed" signal of the combobox to the event we created.
       combobox.connect("changed", combo_changed)

我希望这有帮助!同样,如果没有代码,就很难向您提供具体信息。我正在帮助您,因为您是新来的,但请务必针对未来的所有问题发布您项目中的具体示例和代码。

干杯!

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!

combo_value = -1

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.

def combo_changed(event, data=None):
    #This imports the combo_value variable declared above. Otherwise, the module 
    #would be creating a local variable instead, which would be of no use to the
    #rest of the program.
    global combo_value

    #This retrieves the combo box's selected index and sets the combo_value
    #variable to that index.
    combo_value = combobox.get_active()

Now we need to connect our combo box to this event using the "changed" signal.

combobox.connect("changed", combo_changed)

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:

Combobox (choose color)
"Black" - 0
"White" - 1
"Red" - 2
"Green" - 3
"Blue" - 4

Then, in the code for working with the combo box value, you may have a little something like this:

if combo_value == -1:
   pass
   #In other words, do nothing at all.
elif combo_value == 0
   #Set color to black
elif combo_value == 1
   #Set color to white

And so on, and so forth.

So you can see everything in context, here is the entire code, minus my corny little example above...

combo_value = -1

class MyApplication:
   def __init__(self):
       #Your code here.
       def combo_changed(event, data=None):
           #This imports the combo_value variable declared above. Otherwise, the module 
           #would be creating a local variable instead, which would be of no use to the
           #rest of the program.
           global combo_value

           #This retrieves the combo box's selected index and sets the combo_value
           #variable to that index.
           combo_value = combobox.get_active()

       #Your GUI code is here.

       #This is where your combobox is created from a model (we're assuming it is
       #already declared before this point, and called "MyModel".
       combobox = gtk.ComboBox(MyModel)

       #Now we connect the "changed" signal of the combobox to the event we created.
       combobox.connect("changed", combo_changed)

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文