我是Python新手。我正在想象一个场景,最终我将有许多子类在主 Window 类上监听同一事件。在我在主类中创建所有子类实例并从那里分配所有事件之前。但在创建了几个子类之后,该方法变得很难监督和管理。所以现在我正在考虑使用下面所示的代码。但由于我对类(class)的了解还很初级,我想知道/希望是否有更好的方法。
class Window:
def __init__(self):
self.on_key_press_events = []
def extend_on_key_press(self, event_fn):
self.on_key_press_events.append(event_fn)
def on_key_press(self, key):
print('a key was pressed')
for event_fn in self.on_key_press_events:
event_fn(key)
class Sub():
def __init__(self, STR_sub):
self.STR_sub = STR_sub
def on_key_press(self, key):
print(f'pressed {key} from Sub: {self.STR_sub}')
def bind_events(self, win):
win.extend_on_key_press(self.on_key_press)
class Sub2():
def __init__(self, STR_sub):
self.STR_sub = STR_sub
def on_key_press(self, key):
print(f'pressed {key} from Sub2: {self.STR_sub}')
def bind_events(self, win):
win.extend_on_key_press(self.on_key_press)
win = Window()
sub = Sub("sub instance of Sub")
sub.bind_events(win)
sub2 = Sub2("sub2 instance of Sub2")
sub2.bind_events(win)
win.on_key_press('A')
最佳答案
您可以将这些类传递给 Window
类吗?:
class Sub():
def __init__(self, STR_sub='cba'):
self.STR_sub = STR_sub
def on_key_press(self, key):
print(f'pressed {key} from Sub: {self.STR_sub}')
class Sub2():
def __init__(self, STR_sub='abc'):
self.STR_sub = STR_sub
def on_key_press(self, key):
print(f'pressed {key} from Sub2: {self.STR_sub}')
class Sub3():
def __init__(self, STR_sub='?'):
self.STR_sub = STR_sub
class Window:
def __init__(self, send_events_to=[]):
self.send_events_to = send_events_to
def on_key_press(self, key):
print('a key was pressed')
for cls in self.send_events_to:
send = getattr(cls, 'on_key_press', None)
if send:
send(key)
else:
print('no on_key_press method')
使用示例:
s = Sub()
s2 = Sub2()
s3 = Sub3()
w = Window(send_events_to=[s, s2, s3])
w.on_key_press('hello')
输出:
a key was pressed
pressed hello from Sub: cba
pressed hello from Sub2: abc
no on_key_press method
有一些子类化:
class BaseSub(object):
def __init__(self, STR_sub='?'):
self.STR_sub = STR_sub
def on_key_press(self, key):
print(f'pressed {key} from {self.__class__.__name__}: {self.STR_sub}')
class Sub(BaseSub, object):
def __init__(self,*args, **kwargs):
super().__init__(*args, **kwargs)
class Sub2(BaseSub, object):
def __init__(self, *args, **kwargs):
super().__init__( *args, **kwargs)
def on_key_press(self, key):
print(f'pressed {key} from {self.__class__.__name__} with a different method: {self.STR_sub}')
class Sub3():
pass
class Window:
def __init__(self, send_events_to=[]):
self.send_events_to = send_events_to
def on_key_press(self, key):
print('a key was pressed')
for cls in self.send_events_to:
send = getattr(cls, 'on_key_press', None)
if send:
send(key)
else:
print('no on_key_press method')
s = Sub(STR_sub='abc')
s2 = Sub2(STR_sub='cba')
s3 = Sub3()
w = Window(send_events_to=[s, s2, s3])
w.on_key_press('hello')
输出:
a key was pressed
pressed hello from Sub: abc
pressed hello from Sub2 with a different method: cba
no on_key_press method
关于python - 将一个类的多个函数绑定(bind)到另一个类的类事件: is there a better/shorter way?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58963925/