如何在 quick/gedit 中启动一个新的 python 程序?

如何在 quick/gedit 中启动一个新的 python 程序?

我正在尝试快速制作一个应用程序,我已经制作了窗口,一个称为测试,另一个称为菜单,测试是起始页,我想对其进行编程,以便当您单击按钮(按钮1)时,它将加载menu.ui。以下是TestWindow中的所有代码:

# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# This file is in the public domain
### END LICENSE
import gettext
from gettext import gettext as _
gettext.textdomain('test')

import gtk
import logging
logger = logging.getLogger('test')
from test_lib import Window
from test.AboutTestDialog import AboutTestDialog
from test.PreferencesTestDialog import PreferencesTestDialog
import os
# See test_lib.Window.py for more details about how this class works
class TestWindow(Window):
    __gtype_name__ = "TestWindow"

    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the main window"""
        super(TestWindow, self).finish_initializing(builder)

        self.AboutDialog = AboutTestDialog
        self.PreferencesDialog = PreferencesTestDialog

        # Code for other initialization actions should be added here.
    def on_button1_clicked(self, widget, data=None):
        print "Loading Menu!"
        os.chdir(r"/home/user/test/data/ui/")
        os.startfile("menu.ui")

最后两行是我尝试让它启动 menu.ui。有什么方法可以做到这一点吗?任何帮助都值得感激!谢谢 -Drex

答案1

使用 Python 快速进行应用程序开发的一个好起点是:http://developer.ubuntu.com/resources/tutorials/all/diy-media-player-with-pygtk/

总的来说,你可以看到.ui你的代码中处理其他文件的速度有多快

from test.AboutTestDialog import AboutTestDialog
from test.PreferencesTestDialog import PreferencesTestDialog
[...]
self.AboutDialog = AboutTestDialog
self.PreferencesDialog = PreferencesTestDialog

您可以按照相同的方式添加新窗口:

在您的目录下为您的菜单窗口创建一个新文件test,使得您的代码看起来像这样:

from test.AboutTestDialog import AboutTestDialog
from test.PreferencesTestDialog import PreferencesTestDialog
from test.MenuTestWindow import MenuTestWindow
[...]
self.AboutDialog = AboutTestDialog
self.PreferencesDialog = PreferencesTestDialog
self.MenuWindow = MenuTestWindow

on_button1_clicked然后您可以像这样在您的方法中显示窗口:

def on_button1_clicked(self, widget, data=None):
    print "Loading Menu!"
    self.MenuWindow.show()

现在剩下的唯一问题是,你的 MenuTestWindow 类是什么样的?我会简单地查看快速创建的类并编写如下内容:

import gettext
from gettext import gettext as _
gettext.textdomain('test')

import logging
logger = logging.getLogger('test')

from test_lib.MenuWindow import MenuWindow

# See test_lib.MenuWindow for more details about how this class works.
class MenuTestWindow():
    __gtype_name__ = "MenuTestWindow"

    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the about dialog"""
        super(MenuTestWindow, self).finish_initializing(builder)
        # Code for other initialization actions should be added here.

这给我们留下了test_lib.MenuWindow文件和类(也是从快速默认值中窃取的):

import gtk
import logging
logger = logging.getLogger('test_lib')

from . helpers import get_builder, show_uri, get_help_uri
from . preferences import preferences

# This class is meant to be subclassed by MenuWindow.  It provides
# common functions and some boilerplate.
class Window(gtk.Window):
    __gtype_name__ = "Window"

    # To construct a new instance of this method, the following notable 
    # methods are called in this order:
    # __new__(cls)
    # __init__(self)
    # finish_initializing(self, builder)
    # __init__(self)
    #
    # For this reason, it's recommended you leave __init__ empty and put
    # your initialization code in finish_initializing

    def __new__(cls):
        """Special static method that's automatically called by Python when 
        constructing a new instance of this class.

        Returns a fully instantiated MenuTestWindow object.
        """
        builder = get_builder('MenuWindow')
        new_object = builder.get_object("menu_window")
        new_object.finish_initializing(builder)
        return new_object

    def finish_initializing(self, builder):
        """Called while initializing this instance in __new__

        finish_initializing should be called after parsing the UI definition
        and creating a TestWindow object with it in order to finish
        initializing the start of the new TestWindow instance.
        """
        # Get a reference to the builder and set up the signals.
        self.builder = builder
        self.ui = builder.get_ui(self, True)

(我希望我没有错过任何东西...:-)

这就是全部了。

然而:如果我要编写此应用程序,我可能不会使用其他.ui文件,而是将每个附加 gui/窗口放在主窗口ui文件中(我们将新窗口称为 MenuWindow)并通过类似以下方式访问它:

def on_button1_clicked(self, widget, data=None):
    mw = self.builder.get_object("MenuWindow")
    mw.show()

这样你就不必创建所有其他类和文件。但这是你的应用程序,你必须自己知道是否.ui需要单独的文件。

相关内容