如何将变量从pyqt传递到.py文件并在按钮单击时执行?

时间:2016-05-07 06:15:17

标签: python pyqt pyqt4

我想将此pyqt4应用程序中的文件名传递给另一个.py文件,并在单击按钮时执行相应的.py文件。

GUI.py

from PyQt4 import QtCore, QtGui
import subprocess

class QDataViewer(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QWidget.__init__(self)
            self.uploadButton = QtGui.QPushButton('UPLOAD', self)
            self.uploadButton.setGeometry(300, 20, 80, 35)
            self.Button = QtGui.QPushButton('Key', self)
            self.Button.setGeometry(90, 150, 180, 35)
            self.connect(self.uploadButton, QtCore.SIGNAL('clicked()'), self.open)
            self.Button.clicked.connect(lambda:self.run('My_file.py'))
    def open (self):
        self.filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', "", "*.txt")
        return self.filename # I want THIS VARIABLE to be passed to another python file.

    def run(self, path):
        subprocess.call(['python',path])

My_File.py

textdoc = open(filename_from_GUI(self.filename), 'r').read()
By Using 'textdoc' relevant code(function) here 

我想知道如何首先 self.filename 变量从pyqt类传递到另一个文件,然后执行My_file.py时在GUI.py中单击 self.Button

1 个答案:

答案 0 :(得分:2)

您正在寻找command line arguments。将run功能更改为:

def run(self, path):
    subprocess.call(['python',path,self.filename])

将文件名作为命令行参数传递。在文件My_file.py中,您可以像这样访问它:

import sys
print(sys.argv[1])