这让我快疯了:
- 为什么该方法
the_method(self, button)
引用两个单独的实例,self.button
具体取决于调用它的对象? - 如何显式引用对象实例?
谢谢您的帮助
import os, stat, time
import gtk
class Lister(object):
OPEN_IMAGE = gtk.image_new_from_stock(gtk.STOCK_DND_MULTIPLE, gtk.ICON_SIZE_BUTTON)
CLOSED_IMAGE = gtk.image_new_from_stock(gtk.STOCK_DND, gtk.ICON_SIZE_BUTTON)
def __init__(self, dname = None):
filename = "foo"
self.hbox = gtk.HBox()
self.button = gtk.Button()
self.button.set_image(self.OPEN_IMAGE)
self.button.connect('clicked', self.open_file)
self.hbox.pack_start(self.button, False)
def open_file(self, button):
Buttons().the_method("foo")
# return
class Buttons(object):
OPEN_IMAGE = gtk.image_new_from_stock(gtk.STOCK_DND_MULTIPLE, gtk.ICON_SIZE_BUTTON)
CLOSED_IMAGE = gtk.image_new_from_stock(gtk.STOCK_DND, gtk.ICON_SIZE_BUTTON)
def __init__(self):
self.button = gtk.Button() # THIS is the button to modify
self.hbox = gtk.HBox()
self.hbox.pack_start(self.button, False)
self.button.set_image(self.OPEN_IMAGE)
self.button.connect('clicked', self.the_method)
def the_method(self, button):
print vars(self)
self.button.set_image(self.CLOSED_IMAGE)
class GUI(object):
def delete_event(self, widget, event, data=None):
gtk.main_quit()
return False
def __init__(self):
self.window = gtk.Window()
self.window.set_size_request(300, 600)
self.window.connect("delete_event", self.delete_event)
vbox = gtk.VBox()
vbox.pack_start(Buttons().hbox, False, False, 1)
vbox.pack_start(Lister().hbox)
self.window.add(vbox)
self.window.show_all()
return
def main():
gtk.main()
if __name__ == "__main__":
GUI()
main()