我正在建立一个基本的文件管理员,并且在帖子标题中遇到了问题。 首先,它查找目录,生成项目列表,并将它们附加到树视图列表。
如果单击某个子目录,它将成为新的当前目录。我所需要的只是将它发送回def,它会读取并显示当前目录的内容(我认为)。
import gtk, os
class PyApp(gtk.Window):
def __init__(self):
super(PyApp, self).__init__()
self.set_size_request(250, 200)
self.set_position(gtk.WIN_POS_CENTER)
self.connect("destroy", gtk.main_quit)
self.set_title("SteveFM")
vbox = gtk.VBox(False, 8)
sw = gtk.ScrolledWindow()
sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
vbox.pack_start(sw, True, True, 0)
store = self.create_model()
treeView = gtk.TreeView(store)
treeView.connect("row-activated", self.on_activated)
treeView.set_rules_hint(True)
sw.add(treeView)
self.create_columns(treeView)
self.add(vbox)
self.show_all()
# Take the names of files and subdirectories in
# the current directory and sort them into a list for treeview
def create_model(self):
store = gtk.ListStore(str)
dirList=os.listdir(os.getcwd())
dircontents = []
for item in dirList:
if item[0] != '.':
if os.path.isdir(item):
dircontents.append(['/'+item])
else:
dircontents.append([item])
dircontents.sort()
for act in dircontents:
store.append([act[0]])
return store
# If a file is clicked, open with leafpad.
# If a directory is clicked, change the current working directory to it
# and (my hurdle) replace the current treeview list with the contents of the
# new directory, just like a file manager.
def on_activated(self, widget, row, col):
model = widget.get_model()
target = model[row][0]
if target[0] != "/":
cmd = "leafpad "+target
os.system(cmd)
else:
os.chdir(os.getcwd()+target)
print os.getcwd() # Just for debugging
def create_columns(self, treeView):
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn(None, rendererText, text=0)
column.set_sort_column_id(0)
treeView.append_column(column)
PyApp()
gtk.main()
答案 0 :(得分:1)
我所做的改变:
将treeView
替换为self.treeView
,以便我可以通过其他方法轻松访问。
在on_activated
中,使用create_model
创建新模型,并设置self.treeView
以使用gtk.TreeView.set_model
来使用该模型。
似乎有效。代码:
import gtk, os
class PyApp(gtk.Window):
def __init__(self):
super(PyApp, self).__init__()
self.set_size_request(250, 200)
self.set_position(gtk.WIN_POS_CENTER)
self.connect("destroy", gtk.main_quit)
self.set_title("SteveFM")
vbox = gtk.VBox(False, 8)
sw = gtk.ScrolledWindow()
sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
vbox.pack_start(sw, True, True, 0)
store = self.create_model()
self.treeView = gtk.TreeView(store)
self.treeView.connect("row-activated", self.on_activated)
self.treeView.set_rules_hint(True)
sw.add(self.treeView)
self.create_columns(self.treeView)
self.add(vbox)
self.show_all()
# Take the names of files and subdirectories in
# the current directory and sort them into a list for treeview
def create_model(self):
store = gtk.ListStore(str)
dirList=os.listdir(os.getcwd())
dircontents = []
for item in dirList:
if item[0] != '.':
if os.path.isdir(item):
dircontents.append(['/'+item])
else:
dircontents.append([item])
dircontents.sort()
for act in dircontents:
store.append([act[0]])
return store
# If a file is clicked, open with leafpad.
# If a directory is clicked, change the current working directory to it
# and (my hurdle) replace the current treeview list with the contents of the
# new directory, just like a file manager.
def on_activated(self, widget, row, col):
model = widget.get_model()
target = model[row][0]
if target[0] != "/":
cmd = "leafpad "+target
os.system(cmd)
else:
os.chdir(os.getcwd()+target)
print os.getcwd() # Just for debugging
self.treeView.set_model(self.create_model())
def create_columns(self, treeView):
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn(None, rendererText, text=0)
column.set_sort_column_id(0)
self.treeView.append_column(column)
PyApp()
gtk.main()