나는 단지 초보자 해요 그러나 이것은 내가, 어쩌면 ;-)
가 나는 '제어'에서 주요 구성 요소를 인스턴스화 영감을 인스턴스화 된 개체를 만들 수 있도록 빌더 개체를 전달할 수 않는 것입니다 빌더 객체 (예 : mainwindow)를 사용하거나 빌더 (aboutDialog 예)에 추가하십시오. 또한 각 구성 요소에 "신호"를 추가하는 사전 (dic)을 전달합니다.
그러면 'connect_signals (dic)'이 실행됩니다.
물론 callback 메서드에 사용자 인수를 전달해야 할 때 몇 가지 수동 신호 연결이 필요하지만 그 수가 적습니다.
#modules.control.py
class Control:
def __init__(self):
# Load the builder obj
guibuilder = gtk.Builder()
guibuilder.add_from_file("gui/mainwindow.ui")
# Create a dictionnary to store signal from loaded components
dic = {}
# Instanciate the components...
aboutdialog = modules.aboutdialog.AboutDialog(guibuilder, dic)
mainwin = modules.mainwindow.MainWindow(guibuilder, dic, self)
...
guibuilder.connect_signals(dic)
del dic
#modules/aboutdialog.py
class AboutDialog:
def __init__(self, builder, dic):
dic["on_OpenAboutWindow_activate"] = self.on_OpenAboutWindow_activate
self.builder = builder
def on_OpenAboutWindow_activate(self, menu_item):
self.builder.add_from_file("gui/aboutdialog.ui")
self.aboutdialog = self.builder.get_object("aboutdialog")
self.aboutdialog.run()
self.aboutdialog.destroy()
#modules/mainwindow.py
class MainWindow:
def __init__(self, builder, dic, controller):
self.control = controller
# get gui xml and/or signals
dic["on_file_new_activate"] = self.control.newFile
dic["on_file_open_activate"] = self.control.openFile
dic["on_file_save_activate"] = self.control.saveFile
dic["on_file_close_activate"] = self.control.closeFile
...
# get needed gui objects
self.mainWindow = builder.get_object("mainWindow")
...
편집 : 자동차에 대한 대안은 콜백에 신호를 첨부 :
테스트되지 않은 코드
def start_element(name, attrs):
if name == "signal":
if attrs["handler"]:
handler = attrs["handler"]
#Insert code to verify if handler is part of the collection
#we want.
self.handlerList.append(handler)
def extractSignals(uiFile)
import xml.parsers.expat
p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = self.start_element
p.ParseFile(uiFile)
self.handlerList = []
extractSignals(uiFile)
for handler in handlerList:
dic[handler] = eval(''. join(["self.", handler, "_cb"]))
고마워, 간단하고 효과적! :) – mac