Python 如何将变量从pyqt传递到.py文件,并在单击按钮时执行它?
我想将文件名从此pyqt4应用程序传递到另一个.py文件,并在单击按钮时执行相应的.py文件 GUI.pyPython 如何将变量从pyqt传递到.py文件,并在单击按钮时执行它?,python,pyqt,pyqt4,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.QPushBut
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类传递到另一个文件,然后在要查找的GUI.py中单击self.Button时执行My_file.py。将run
功能更改为:
def run(self, path):
subprocess.call(['python',path,self.filename])
将文件名作为命令行参数传递。在My_file.py文件中,您可以这样访问它:
import sys
print(sys.argv[1])
你在找我。将run
功能更改为:
def run(self, path):
subprocess.call(['python',path,self.filename])
将文件名作为命令行参数传递。在My_file.py文件中,您可以这样访问它:
import sys
print(sys.argv[1])
您可以在
My_File.py
中使用函数myFunc(文件名)
并将值传递给它。感谢回复,但如何传递变量您可以在My_File.py
中使用函数myFunc(文件名)
并将值传递给它。感谢回复,但如何传递变量