Python GTK - 在小部件之间切换 - 在小部件或窗口中嵌入树

时间:2015-08-07 13:26:56

标签: java python swing gtk pygtk

我有一个带菜单栏和3个菜单项的窗口。前两项应该允许用户在主“窗口”(对于java我会说面板)和日志“窗口”之间切换。我习惯在摇摆中使用卡片布局 - java。 但我想在python gtk中做同样的事情。

第二个问题是 - 我如何显示一棵树并站在树旁边,比方说4个按钮。 树的代码是 - http://www.pygtk.org/pygtk2tutorial/sec-CellRenderers.html#cellrendererfig

我希望你能给我一些提示如何处理上述问题:/

我的窗口目前看起来像这样: enter image description here

我的代码就在这一刻:

import pygtk
pygtk.require("2.0")
import gtk, gobject

class PyApp(gtk.Window):

    def __init__(self):
        super(PyApp, self).__init__()

        self.set_title("Utilities Testing Tool")
        self.set_size_request(700, 500)
        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(6400, 6400, 6440))
        self.set_position(gtk.WIN_POS_CENTER)

        mb = gtk.MenuBar()


        treem = gtk.MenuItem("Tree Window")
        treem.connect("button_press_event", self.open_tree_widget)
        logm = gtk.MenuItem("Log Window")
        logm.connect("button_press_event", self.open_log_widget)
        exit = gtk.MenuItem("Exit")
        exit.connect("button_press_event", gtk.main_quit)
        mb.append(treem)
        mb.append(logm)
        mb.append(exit)


        self.vbox = gtk.VBox(False, 2)
        self.vbox.pack_start(mb, False, False, 0)

        self.add(self.vbox)

        self.connect("destroy", gtk.main_quit)
        self.show_all()

    def open_tree_widget(self, arg0, arg1):
        print("tree")


    def open_log_widget(self,  arg0, arg1):
        print("log")

    def replace_widget(self, current, new):
        """
        Replace one widget with another.
        'current' has to be inside a container (e.g. gtk.VBox).
        """
        container = current.parent
        assert container # is "current" inside a container widget?

        # stolen from gazpacho code (widgets/base/base.py):
        props = {}
        for pspec in gtk.container_class_list_child_properties(container):
            props[pspec.name] = container.child_get_property(current, pspec.name)

        gtk.Container.remove(container, current)
        container.add(new)

        for name, value in props.items():
            container.child_set_property(new, name, value)    

PyApp()
gtk.main()

1 个答案:

答案 0 :(得分:0)

我在此期间阅读了公共pygtk教程的一些部分,并且我已经编写了以下解决方案。

该解决方案包含两个由gtk.table类继承的类。

如果按下其中一个按钮切换窗口,则会调用“replace_widget”方法。 “replace_widget”获取两个参数,即当前窗口小部件和希望窗口小部件。请参阅以下代码。我希望这个答案可以帮助一些有同样问题的人。谢谢你的回答。

import pygtk
pygtk.require("2.0")
import gtk, gobject
class clTableTree(gtk.Table):
    def __init__(self):
        super(clTableTree, self).__init__(15,15,True)

        self.set_row_spacings(5)
        self.btnStartTest = gtk.Button("Start testing Process")
        self.attach(self.btnStartTest,6,9,13,14)

        self.btnActivate = gtk.Button("Activate")
        self.attach(self.btnActivate,12,14,3,4)

        self.btnDeactivate = gtk.Button("Deactivate")
        self.attach(self.btnDeactivate,12,14,4,5)

        self.btnDuplicateNode = gtk.Button("Duplicate Node")
        self.attach(self.btnDuplicateNode,12,14,5,6)        

        self.btnDeleteNode = gtk.Button("Delete Node")
        self.attach(self.btnDeleteNode,12,14,6,7) 


class clTableLog(gtk.Table):
    def __init__(self):
        super(clTableLog, self).__init__(15,15,True)

        self.set_row_spacings(5)
        self.btnSaveLogAs = gtk.Button("Save log as...")
        self.attach(self.btnSaveLogAs,6,9,13,14)
        self.btnSaveLogAs.show()

        self.btnRefresh = gtk.Button("Refresh")
        self.attach(self.btnRefresh,12,14,5,6)        
        self.btnRefresh.show()

        self.btnClearALL = gtk.Button("Clear all")
        self.attach(self.btnClearALL,12,14,6,7)  
        self.btnClearALL.show()

class PyApp(gtk.Window):

    def __init__(self):
        super(PyApp, self).__init__()

        self.set_title("Utilities Testing Tool")
        self.set_size_request(700, 500)
        #self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(6400, 6400, 6440))
        self.set_position(gtk.WIN_POS_CENTER)

        mb = gtk.MenuBar()


        treem = gtk.MenuItem("Tree Window")
        treem.connect("button_press_event", self.open_tree_widget)
        logm = gtk.MenuItem("Log Window")
        logm.connect("button_press_event", self.open_log_widget)
        exit = gtk.MenuItem("Exit")
        exit.connect("button_press_event", gtk.main_quit)
        mb.append(treem)
        mb.append(logm)
        mb.append(exit)


        self.vbox = gtk.VBox(False, 2)
        self.vbox.pack_start(mb, False, False, 0)

        self.tableTreeWindow = self.create_table_tree()
        self.tableLogWindow = self.create_table_log()
        self.tableLogWindow.show()

        self.vbox.pack_start(self.tableTreeWindow)
        self.add(self.vbox)
        self.connect("destroy", gtk.main_quit)
        self.show_all()

    def create_table_tree(self):
        return clTableTree()

    def create_table_log(self):
        return clTableLog()

    def open_tree_widget(self, arg0, arg1):
        self.replace_widget(self.tableLogWindow, self.tableTreeWindow)


    def open_log_widget(self,  arg0, arg1):
        self.replace_widget(self.tableTreeWindow, self.tableLogWindow)

    def replace_widget(self, current, new):

        container = current.parent
        assert container # is "current" inside a container widget?

        props = {}
        for pspec in gtk.container_class_list_child_properties(container):
            props[pspec.name] = container.child_get_property(current, pspec.name)

        gtk.Container.remove(container, current)
        container.add(new)

        for name, value in props.items():
            container.child_set_property(new, name, value)    

PyApp()
gtk.main()