为gtk中的简单文件浏览器编写剪切/复制/粘贴功能。每个剪切/复制按钮/粘贴点击都会发出越来越多的信号。以下是该程序的一部分,用于处理浏览器的剪切/复制/粘贴功能:
def __init__(self):
'''
some code
'''
iconView1.connect("selection-changed", self.on_selection_changed_1, copyButton1, copyButton2, cutButton1, cutButton2, pasteButton1, pasteButton2, deleteButton1, store1, store2)
iconView2.connect("selection-changed", self.on_selection_changed_2, copyButton1, copyButton2, cutButton1, cutButton2, pasteButton1, pasteButton2, deleteButton2, store1, store2)
def on_selection_changed_1(self, iconView1, copyButton1, copyButton2, cutButton1, cutButton2, pasteButton1, pasteButton2, deleteButton1, store1, store2) :
selectedIcon = iconView1.get_selected_items()
if selectedIcon :
model = iconView1.get_model()
path = model[selectedIcon[0]][COL_PATH]
self.action_directory_1 = self.current_directory_1+os.path.sep+path
copyButton1.set_sensitive(True)
cutButton1.set_sensitive(True)
deleteButton1.set_sensitive(True)
copyButton1.connect("clicked",self.copy_pressed, cutButton1, cutButton2, pasteButton1, pasteButton2, 1, store1, store2)
cutButton1.connect("clicked",self.cut_pressed, copyButton1, copyButton2, pasteButton1, pasteButton2, 1, store1, store2)
deleteButton1.connect("clicked",self.delete_pressed, 1, store1, store2)
else :
copyButton1.set_sensitive(False)
cutButton1.set_sensitive(False)
deleteButton1.set_sensitive(False)
def on_selection_changed_2(self, iconView2, copyButton1, copyButton2, cutButton1, cutButton2, pasteButton1, pasteButton2, deleteButton2, store1, store2) :
selectedIcon = iconView2.get_selected_items()
if selectedIcon :
model = iconView2.get_model()
path = model[selectedIcon[0]][COL_PATH]
self.action_directory_2 = self.current_directory_2+os.path.sep+path
copyButton2.set_sensitive(True)
cutButton2.set_sensitive(True)
pasteButton2.set_sensitive(True)
deleteButton2.set_sensitive(True)
copyButton2.connect("clicked",self.copy_pressed, cutButton1, cutButton2, pasteButton1, pasteButton2, 2, store1, store2)
cutButton2.connect("clicked",self.cut_pressed, copyButton1, copyButton2, pasteButton1, pasteButton2, 2, store1, store2)
deleteButton2.connect("clicked",self.delete_pressed, 2, store1, store2)
else :
copyButton2.set_sensitive(False)
cutButton2.set_sensitive(False)
pasteButton2.set_sensitive(False)
deleteButton2.set_sensitive(False)
###############ACTION METHODS--CUT#################
def cut_pressed(self, widget, copyButton1, copyButton2, pasteButton1, pasteButton2, index, store1, store2):
if index == 1:
pasteButton2.set_sensitive(True)
pasteButton2.connect("clicked", self.paste_pressed,"cut", 1, store1, store2)
print " CUT BUTTON 1"
print " ########################## "
copyButton1.set_sensitive(False)
widget.set_sensitive(False)
else :
pasteButton1.set_sensitive(True)
pasteButton1.connect("clicked", self.paste_pressed,"cut", 2, store1, store2)
print " CUT BUTTON 2"
copyButton2.set_sensitive(False)
widget.set_sensitive(False)
###############ACTION METHODS--COPY##################
def copy_pressed(self, widget, cutButton1, cutButton2, pasteButton1, pasteButton2, index, store1, store2):
if index == 1:
pasteButton2.set_sensitive(True)
pasteButton2.connect("clicked", self.paste_pressed,"copy", 1, store1, store2)
print " COPY BUTTON 1"
cutButton1.set_sensitive(False)
widget.set_sensitive(False)
else :
pasteButton1.set_sensitive(True)
pasteButton1.connect("clicked", self.paste_pressed,"copy", 2, store1, store2)
print " COPY BUTTON 2"
cutButton2.set_sensitive(False)
widget.set_sensitive(False)
##############ACTION METHODS--PASTE################
def paste_pressed(self, widget, action, index, store1, store2):
if index == 1:
if action == "cut" :
print " cut selected 1" + " ACTION DIRECTORY : " +self.action_directory_1
print " ########################## "
widget.set_sensitive(False)
else :
print " copy selected 1" + " ACTION DIRECTORY : " +self.action_directory_1
print " ########################## "
widget.set_sensitive(False)
else :
if action == "cut" :
print " cut selected 2" + " ACTION DIRECTORY : " +self.action_directory_1
print " ########################## "
widget.set_sensitive(False)
else :
print " copy selected 2" + " ACTION DIRECTORY : " +self.action_directory_1
print " ########################## "
widget.set_sensitive(False)
以下是终端收到的输出:
vineet @ vineet:〜/ Documents / Project $ python draft7.py
CUT BUTTON 1 // FIRST CLICK
##########################
cut selected 1 ACTION DIRECTORY : /media/vineet/MYLINUXLIVE/EFI
##########################
CUT BUTTON 1 // SECOND CLICK
##########################
CUT BUTTON 1
##########################
cut selected 1 ACTION DIRECTORY : /media/vineet/MYLINUXLIVE/Media_File
##########################
cut selected 1 ACTION DIRECTORY : /media/vineet/MYLINUXLIVE/Media_File
##########################
cut selected 1 ACTION DIRECTORY : /media/vineet/MYLINUXLIVE/Media_File
##########################
COPY BUTTON 1 // THIRD CLICK
COPY BUTTON 1
COPY BUTTON 1
cut selected 1 ACTION DIRECTORY : /media/vineet/MYLINUXLIVE/pics
##########################
cut selected 1 ACTION DIRECTORY : /media/vineet/MYLINUXLIVE/pics
##########################
cut selected 1 ACTION DIRECTORY : /media/vineet/MYLINUXLIVE/pics
##########################
copy selected 1 ACTION DIRECTORY : /media/vineet/MYLINUXLIVE/pics
##########################
copy selected 1 ACTION DIRECTORY : /media/vineet/MYLINUXLIVE/pics
##########################
copy selected 1 ACTION DIRECTORY : /media/vineet/MYLINUXLIVE/pics
##########################
正如您在终端输出中看到的,在第一次点击后每次点击都会发出增加的号码。如果每次点击我获得1个发射信号?(或者在这种情况下,每次点击一次CUT/COPY BUTTON 1/2' and an associated
切割/复制选择1 / 2`打印输出)
答案 0 :(得分:2)
您在每次按键时都会连接新的信号处理程序,并且永远不会删除旧的处理程序。当信号触发时,将连接您连接的每个处理程序。
您应该在某些初始化代码中仅连接所有信号一次(然后在处理程序中决定是否是实际处理信号的好时机,如果您有不希望它处理的情况),或者你应该断开处理程序本身的处理程序,如果你不再希望它被调用。