Python GTK - 在小部件之间切换 - 在小部件中嵌入树或 window
Python GTK - switch between widgets - embed a tree in widget or window
我有一个带有菜单栏和 3 个菜单项的 window。前两项应该允许用户在主 "window"(对于 java 我会说面板)和日志 "window" 之间切换。我习惯在 swing 中使用卡片布局 - java。
但我想在 python gtk 中做同样的事情。
第二个问题是 - 我如何显示一棵树并在树旁边,比方说 4 个按钮。
树的代码是 - http://www.pygtk.org/pygtk2tutorial/sec-CellRenderers.html#cellrendererfig
希望您能给我一些提示,告诉我如何处理上述问题:/
我的 window 目前看起来像这样:
目前我的代码是这样的:
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()
我同时阅读了 public pygtk 教程的某些部分,并编写了以下解决方案。
该解决方案包含由 class gtk.table 继承的两个 classes。
如果按下其中一个按钮来切换 window,则会调用方法 "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()
我有一个带有菜单栏和 3 个菜单项的 window。前两项应该允许用户在主 "window"(对于 java 我会说面板)和日志 "window" 之间切换。我习惯在 swing 中使用卡片布局 - java。 但我想在 python gtk 中做同样的事情。
第二个问题是 - 我如何显示一棵树并在树旁边,比方说 4 个按钮。 树的代码是 - http://www.pygtk.org/pygtk2tutorial/sec-CellRenderers.html#cellrendererfig
希望您能给我一些提示,告诉我如何处理上述问题:/
我的 window 目前看起来像这样:
目前我的代码是这样的:
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()
我同时阅读了 public pygtk 教程的某些部分,并编写了以下解决方案。
该解决方案包含由 class gtk.table 继承的两个 classes。
如果按下其中一个按钮来切换 window,则会调用方法 "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()