0

我正在尝试在窗口中创建一个按钮,该按钮最终将运行我用代码编写的函数。到目前为止,我的 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")
4

1 回答 1

0

看看PyQt 信号功能。对于 QPushButton 特别是它变成:

 buttonName.clicked.connect(functionName)

每当单击按钮时,它将发出clicked现在已functionName注册执行的信号。注意,您不要在之后加上括号,functionName因为您不希望函数执行,而是传递函数本身。此外,该函数需要满足clicked 信号的输出,该信号发出一个布尔值(因此 functionName 需要接受该参数)。在您的情况下,由于您想传递另一个对象,您可以创建一个方法:

def clickHandler(self,checked):
    #call function with object to pass as argument here

它调用您的方法并接收您要传递的对象

于 2015-07-27T20:59:29.823 回答