我一直在玩 Quickly(Canonical 创建的用于快速开发 Python + GTK 应用程序的程序),我希望我开始开发的这个特定应用程序等到至少一个界面“启动”并且有一个由 DHCP 分配给它的默认路由。
到目前为止,这是我的代码:
import gettext
import subprocess
import time
from multiprocessing import Process, Queue, current_process
from gettext import gettext as _
gettext.textdomain('launcher')
import gtk
import logging
logger = logging.getLogger('launcher')
from launcher_lib import Window
from launcher.AboutLauncherDialog import AboutLauncherDialog
from launcher.PreferencesLauncherDialog import PreferencesLauncherDialog
# See launcher_lib.Window.py for more details about how this class works
class LauncherWindow(Window):
__gtype_name__ = "LauncherWindow"
def finish_initializing(self, builder): # pylint: disable=E1002
"""Set up the main window"""
super(LauncherWindow, self).finish_initializing(builder)
self.AboutDialog = AboutLauncherDialog
self.PreferencesDialog = PreferencesLauncherDialog
self.ui.status_label.set_text("Waiting for the interface to come up")
while True:
if subprocess.call(["/sbin/ifconfig | grep Bcast"], shell=True) == 0 and subprocess.call(["netstat -rn | grep UG | grep 0.0.0.0"], shell=True) == 0:
break
self.ui.status_label.set_text("Loaded")
# There is more code after this, but this should be all that I
# need to get the GUI to show a status line saying the interface
# is up... right???
现在,这一切都很好,但是它阻止了应用程序的启动,所以看起来它只是挂起,直到进程返回 true。
我想做的是把它推到一个单独的进程中,但是,我对 Python 有点 ^h^w^h 很多新手,虽然我找到了多处理库,但我似乎没有能够将 self.ui.status_label 推送到我可以从生成的进程中重复使用的任何内容中。
例如,我是否定义这样的变量:
# after line:
logger = logging.getLogger('launcher')
# add this
status_label = None
然后像这样引用它:
# after line:
self.PreferencesDialog = PreferencesLauncherDialog
# add this
status_label = self.ui.status_label
# Then set it like this:
status_label.set_text("Waiting for the interface to come up")
或者,我应该创建一个只处理更新状态窗口的新类(如果是这样,我应该怎么做),或者......我应该在我想设置它的时候抛出 self.ui.status_label 吗?我试过这样做:
def update_status(me):
me.ui.status_label.set_text("Update me!")
return True
# And then in the finish_initializing() code
update_status(self)
但这只是说
NameError: global name 'self' is not defined
我正在尽我最大的努力,但我现在很困惑,而且,正如我所提到的,Python 不是我完全熟悉的语言,但我正在试一试:)
PS,我认为这应该具有“快速”标签,但是,我还没有创建该标签的声誉。