我正在尝试在窗口中创建一个按钮,该按钮最终将运行我用代码编写的函数。到目前为止,我的 GUI 代码如下:
import sys
from PyQt4 import QtGui, QtCore
class PAINSFILTER(QtGui.QMainWindow):
def __init__(self):
QtGui.QWidget.__init__(self)
# Layout Init.
self.setGeometry (650, 300, 600, 600)
self.setWindowTitle('Data Viewer')
self.setWindowIcon(QtGui.QIcon('pill.png'))
extractAction = QtGui.QAction("Quit", self)
extractAction.setShortcut("Ctrl+Q")
extractAction.setStatusTip('Leave App')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu("&File")
fileMenu.addAction(extractAction)
self.uploadButton = QtGui.QPushButton('UPLOAD SDF', self)
self.runfilterButton = QtGui.QPushButton('Run Filter', self)
self.uploadButton.move (200, 50)
self.runfilterButton.move (400,50)
hBoxLayout = QtGui.QHBoxLayout()
hBoxLayout.addWidget(self.uploadButton)
hBoxLayout.addWidget(self.runfilterButton)
self.setLayout(hBoxLayout)
# Signal Init.
self.connect(self.uploadButton, QtCore.SIGNAL('clicked()'), self.open)
def open (self):
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
print 'Path file:', filename
def close_application(self):
print ("Out")
sys.exit()
def run():
app = QtGui.QApplication(sys.argv)
mw = PAINSFILTER()
mw.show()
sys.exit(app.exec_())
run()
我试图通过按钮运行的函数是一个简单的嵌套循环,它将获取我通过“上传 sdf”按钮上传的文件并通过该函数运行它。如何使用 PyQt 将该功能链接到按钮。这是供参考的功能:
suppl = Chem.SDMolSupplier('data.sdf') #this .sdf file is the one uploaded through the upload sdf button
for m in suppl:
for x in range(len(pains_smarts)):
if m.HasSubstructMatch(Chem.MolFromSmarts(pains_smarts[x])):
match+=1
has_pains.append(m)
ss.append(c)
else:
no_match+=1
c+=1
for m in suppl:
for x in range(len(pains_smarts)):
if m.HasSubstructMatch(Chem.MolFromSmarts(pains_smarts[x])):
pname.append(pains_name[x])
for m in suppl:
for x in range(len(pains_smarts)):
if m.HasSubstructMatch(Chem.MolFromSmarts(pains_smarts[x])):
psmarts.append(pains_smarts[x])
uniquess = set(ss)
w = Chem.SDWriter('pains_structures.sdf')
notpains = Chem.SDWriter('no_pains_structures.sdf')
temp = 0
for m in suppl:
if temp in uniquess:
w.write(m)
else:
notpains.write(m)
temp += 1
w.flush()
notpains.flush()
import csv
csv_out = open("pains_compounds.csv", "w")
mywriter = csv.writer(csv_out)
for row in zip(ss, psmarts, pname):
mywriter.writerow(row)
csv_out.close()
new_suppl = Chem.SDMolSupplier("pains_structures.sdf")
ms = [x for x in new_suppl if x is not None]
for m in ms: tmp = AllChem.Compute2DCoords(m)
from rdkit.Chem import Draw
img = Draw.MolsToGridImage(ms, molsPerRow = 6, subImgSize = (200, 200), legends = [x.GetProp("ID") for x in ms])
img.save("pains_img.png")