我正在尝试管理包含“其他文件”的文件(许多小文件存储在几个大文件中)。“引擎”工作得很好,但我对 GUI 有一些问题。由于处理时间的原因,某些函数是线程化的,例如删除或添加文件。以下是部分代码:
import gtk, os, data, time, threading, subprocess
import Image, re
gtk.gdk.threads_init()
#some unimportant code - creating window with some buttons and IconView item (to show elements in currently opened directory)
def _del_thread(self): #deletes items selected in gtk.IconView
dl, fl = self.items #self.items stores selected dirs and files
for d in dl:
self.changestatus('Deleting: '+data.dirs[d][0]+'...') #prints the text at StatusBar (data.dirs[d][0] is a filename)
data.RemoveDir(d) #recursively removes all content in directory d
for f in fl:
self.changestatus('Usuwanie pliku: '+data.files[f][0]+'...')
data.PopFile(f) #removes single file
self.changestatus('') #clears the StatusBar
self.refresh() #some elements are deleted - refresh IconView
def _add_thread(self, fl): #adds files listed in fl
for f in fl:
self.changestatus('Adding: '+f[-50:]+'...')
#some unimportant code (recognizing file's extension and generating a thumbnail
data.PutFile(f, thumb, self.dir) #adds the file with generated thumbnail to currently opened directory (self.dir)
self.changestatus('') #clears the StatusBar
self.refresh() #some elements are added - refresh IconView
def Delete(self, widget): #triggered by clicking a context-menu item
md = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, \
gtk.MESSAGE_QUESTION, gtk.BUTTONS_OK_CANCEL, \
"Do you want to remove selected items?")
res = md.run()
md.destroy()
if res == gtk.RESPONSE_OK:
t = threading.Thread(target=self._del_thread) #start new thread
t.start()
def Add(self, widget): #triggered by clicking a context-menu item
chooser = gtk.FileChooserDialog(title='Select files to add...',action=gtk.FILE_CHOOSER_ACTION_OPEN,
buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
chooser.set_select_multiple(True)
response = chooser.run()
if response == gtk.RESPONSE_OK:
#star new thread with selected files' list as an argument
t = threading.Thread(target=self._add_thread, args=(chooser.get_filenames(),))
t.start()
chooser.destroy()
还有第三个函数在线程中运行,但这并不重要。它很快(约 1 秒),始终执行良好并且从不冻结窗口。另外两个(如上所示)有时执行得很好,有时则不然。例如,我在 FileChooserDialog 中选择了一些文件,添加线程启动,StatusBar 连续显示正在添加的文件,最后窗口冻结。之后窗口看起来像这样(对不起波兰描述): 为什么线程有时处理没有问题,有时没有?
最好的问候, mopsiok