【问题标题】:Multiple buttons to call same function in python Gtk在python Gtk中调用相同函数的多个按钮
【发布时间】:2013-05-10 05:16:21
【问题描述】:

我正在使用 Gtk-Python 开发一个小型应用程序,其中我有大约 10 到 20 个按钮,当单击其中任何一个时,我希望将按钮的值附加到我已经完成的文本框中到目前为止

button1.connect("clicked",button_function)
button2.connect("clicked",button_function)
...

适用于所有功能。这是唯一的方法吗?或者有什么优雅的方法吗?并且按钮的值由

指定
button1._value=#something
...

谁能帮帮我?

【问题讨论】:

  • 这样,传递一个列表中的按钮对象,这样调用,for button in buttonslist: button.connect("clicked",button_function)
  • 但这不会一次调用所有按钮的函数吗?
  • 您只是将函数绑定到点击事件,您可以随时在任何按钮上调用。
  • 你成功了..谢谢
  • @Srini 您能否发表您的评论作为回复,以便 Aswin Murugesh 接受?

标签: python function button gtk


【解决方案1】:

这样,把按钮对象放在一个列表中,这样调用,

for button in buttonslist: 
    button.connect("clicked",button_function)

【讨论】:

    【解决方案2】:
    import sys,os
    import pygtk, gtk, gobject
    
    class GTK_Main:
      def __init__(self):
    
    
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Test")
        window.set_default_size(500, 400)
        window.connect("destroy", gtk.main_quit, "WM destroy")
    
        hbox_eq = gtk.HBox()
    
        buttonlist = []
    
        for i in [1,2,3,4,5,6,7,8,9]:
          b =  gtk.Button( str(i) )
          b.set_name("YOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!")
    
          buttonlist.append(b)
          hbox_eq.pack_start(b, True, True, 0)
    
        for button in buttonlist:
          print button
          #print button.get_label()
          #print button.get_name()
    
        window.add(hbox_eq)
        window.show_all()
    
      def exit(self, widget, data=None):
        gtk.main_quit()
    
    
    GTK_Main()
    gtk.gdk.threads_init()
    gtk.main()
    

    输出:

    <gtk.Button object at 0x25d8370 (GtkButton at 0x24348e0)>
    <gtk.Button object at 0x25d83c0 (GtkButton at 0x24349a0)>
    <gtk.Button object at 0x25d8410 (GtkButton at 0x2434a60)>
    <gtk.Button object at 0x25d8460 (GtkButton at 0x2434b20)>
    <gtk.Button object at 0x25d84b0 (GtkButton at 0x2434be0)>
    <gtk.Button object at 0x25d8500 (GtkButton at 0x2434ca0)>
    <gtk.Button object at 0x25d8550 (GtkButton at 0x2434d60)>
    <gtk.Button object at 0x25d85a0 (GtkButton at 0x2434e20)>
    <gtk.Button object at 0x25d85f0 (GtkButton at 0x2434ee0)>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多