0

我必须制作几个按钮,然后需要访问它们,所以我尝试如下但它不起作用。我怎样才能制作像数组/列表这样的按钮?

""" Make 100 gtk.Button as array / list """
r = self.db_Query("http://pbx/a/endpoints")
if r:
  for row in r:
    lab1 = row['username']
    self.button[lab1]= gtk.Button()
    self.button[lab1].set_tooltip_text(lab1)

    self.button[lab1].connect("clicked" , self.button1_action, None)
    window.add(self.button[lab1] )

然后将它们搜索为

self.button['STACKOVERFLOW'].set_text("how!")
4

1 回答 1

0

有用。

import sys,os
import pygtk, gtk, gobject

class GTK_Main:
  def __init__(self):
    """ Window """
    button_rc = """
    style "deButton" {
      # Default - Yellow
      bg[NORMAL] = "yellow"
      fg[NORMAL] = "#000000"

      # Mouse-Over - Blue
      fg[PRELIGHT] = "blue"
      bg[PRELIGHT] = "#000000"

      # Click - RED
      bg[ACTIVE] = "red"
      fg[ACTIVE] = "#000000"

    }
    widget "*.deButton" style "deButton"
    """

    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.set_title("Test")
    window.set_default_size(100, 100)
    window.connect("destroy", gtk.main_quit, "WM destroy")
    gtk.rc_parse_string(button_rc)

    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("deButton")
      buttonlist.append(b)
      hbox_eq.pack_start(b, True, True, 0)


    for button in buttonlist:
      print button.get_label()


    window.add(hbox_eq)
    window.show_all()

  def exit(self, widget, data=None):
    gtk.main_quit()


GTK_Main()
gtk.gdk.threads_init()
gtk.main()
于 2013-10-23T21:23:07.570 回答