浏览文件并在不同的文件中使用它的路径
Browse file and use its path in a different file
我在使用 PyQt5 时遇到了一些麻烦。我正在尝试创建一个浏览文件的程序,当找到一个文件时,它将路径保存在内存中,当我按下一个按钮时,它 运行 是另一个使用浏览路径的 .py 文件。这是我到目前为止能够做的。
File1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_DE(object):
def setupUi(self, DE):
DE.setObjectName("DE")
DE.resize(1149, 517)
font = QtGui.QFont()
font.setPointSize(10)
DE.setFont(font)
# The "browse" button
self.GermanyBrowse = QtWidgets.QPushButton(DE)
self.GermanyBrowse.setGeometry(QtCore.QRect(240, 60, 91, 21))
self.GermanyBrowse.setObjectName("GermanyBrowse")
self.GermanyBrowse.clicked.connect(self.getfile)
# The run button
self.Run = QtWidgets.QPushButton(DE)
self.Run.setGeometry(QtCore.QRect(500, 460, 156, 23))
self.Run.setObjectName("Run")
self.Run.clicked.connect(self.runFile())
self.retranslateUi(DE)
def retranslateUi(self, DE):
_translate = QtCore.QCoreApplication.translate
DE.setWindowTitle(_translate("DE", "Title"))
self.Run.setText(_translate("DE", "Run"))
self.GermanyBrowse.setText(_translate("DE", "Browse"))
def getfile(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
None,
"Select file",
"",
"*.xlsx", )
return fileName
def runFile(self):
import File2.py
file2.name_of_file()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
DE = QtWidgets.QDialog()
ui = Ui_DE()
ui.setupUi(DE)
DE.show()
sys.exit(app.exec_())
File2.py
import File1
x = File1.Ui_DE.getfile(GermanyBrowse)
def name_of_file(str):
alert = QtWidgets.QMessageBox()
alert.setText(f"You have opnened {str}")
alert.exec_()
"NameError: name 'GermanyBrowse' is not defined" - 当我 运行 File2.py
时我得到的是什么
我的问题是,如何调用从“self.GermanyBrowse.clicked.connect(self.getfile) 从 File1.py 到 File2.py 的路径作为字符串。
谢谢!
试一试:
file1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_DE(object):
def setupUi(self, DE):
DE.setObjectName("DE")
DE.resize(1149, 517)
font = QtGui.QFont()
font.setPointSize(10)
DE.setFont(font)
# The "browse" button
self.GermanyBrowse = QtWidgets.QPushButton(DE)
self.GermanyBrowse.setGeometry(QtCore.QRect(240, 60, 91, 21))
self.GermanyBrowse.setObjectName("GermanyBrowse")
self.GermanyBrowse.clicked.connect(self.getfile)
# The run button
self.Run = QtWidgets.QPushButton(DE)
self.Run.setGeometry(QtCore.QRect(500, 460, 156, 23))
self.Run.setObjectName("Run")
self.Run.clicked.connect(self.runFile) # - ()
self.retranslateUi(DE)
def retranslateUi(self, DE):
_translate = QtCore.QCoreApplication.translate
DE.setWindowTitle(_translate("DE", "Title"))
self.Run.setText(_translate("DE", "Run"))
self.GermanyBrowse.setText(_translate("DE", "Browse"))
def getfile(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
None,
"Select file",
"",
"*.xlsx", )
return fileName
def runFile(self):
import file2 # --- File2.py
file2.name_of_file(file2.x) # +++ (file2.x)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
DE = QtWidgets.QDialog()
ui = Ui_DE()
ui.setupUi(DE)
DE.show()
sys.exit(app.exec_())
file2.py
from PyQt5 import QtWidgets # +++
import file1 # - DailyMCI, + file1
#x = DailyMCI.Ui_DE.getfile(GermanyBrowse) # ---
x = file1.Ui_DE.getfile(file1.Ui_DE) # +++
def name_of_file(str):
alert = QtWidgets.QMessageBox()
alert.setText("You have opnened {str}".format(str=str)) # I have python 3.5 (f"You have opnened {str}")
alert.exec_()
我在使用 PyQt5 时遇到了一些麻烦。我正在尝试创建一个浏览文件的程序,当找到一个文件时,它将路径保存在内存中,当我按下一个按钮时,它 运行 是另一个使用浏览路径的 .py 文件。这是我到目前为止能够做的。
File1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_DE(object):
def setupUi(self, DE):
DE.setObjectName("DE")
DE.resize(1149, 517)
font = QtGui.QFont()
font.setPointSize(10)
DE.setFont(font)
# The "browse" button
self.GermanyBrowse = QtWidgets.QPushButton(DE)
self.GermanyBrowse.setGeometry(QtCore.QRect(240, 60, 91, 21))
self.GermanyBrowse.setObjectName("GermanyBrowse")
self.GermanyBrowse.clicked.connect(self.getfile)
# The run button
self.Run = QtWidgets.QPushButton(DE)
self.Run.setGeometry(QtCore.QRect(500, 460, 156, 23))
self.Run.setObjectName("Run")
self.Run.clicked.connect(self.runFile())
self.retranslateUi(DE)
def retranslateUi(self, DE):
_translate = QtCore.QCoreApplication.translate
DE.setWindowTitle(_translate("DE", "Title"))
self.Run.setText(_translate("DE", "Run"))
self.GermanyBrowse.setText(_translate("DE", "Browse"))
def getfile(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
None,
"Select file",
"",
"*.xlsx", )
return fileName
def runFile(self):
import File2.py
file2.name_of_file()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
DE = QtWidgets.QDialog()
ui = Ui_DE()
ui.setupUi(DE)
DE.show()
sys.exit(app.exec_())
File2.py
import File1
x = File1.Ui_DE.getfile(GermanyBrowse)
def name_of_file(str):
alert = QtWidgets.QMessageBox()
alert.setText(f"You have opnened {str}")
alert.exec_()
"NameError: name 'GermanyBrowse' is not defined" - 当我 运行 File2.py
时我得到的是什么我的问题是,如何调用从“self.GermanyBrowse.clicked.connect(self.getfile) 从 File1.py 到 File2.py 的路径作为字符串。
谢谢!
试一试:
file1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_DE(object):
def setupUi(self, DE):
DE.setObjectName("DE")
DE.resize(1149, 517)
font = QtGui.QFont()
font.setPointSize(10)
DE.setFont(font)
# The "browse" button
self.GermanyBrowse = QtWidgets.QPushButton(DE)
self.GermanyBrowse.setGeometry(QtCore.QRect(240, 60, 91, 21))
self.GermanyBrowse.setObjectName("GermanyBrowse")
self.GermanyBrowse.clicked.connect(self.getfile)
# The run button
self.Run = QtWidgets.QPushButton(DE)
self.Run.setGeometry(QtCore.QRect(500, 460, 156, 23))
self.Run.setObjectName("Run")
self.Run.clicked.connect(self.runFile) # - ()
self.retranslateUi(DE)
def retranslateUi(self, DE):
_translate = QtCore.QCoreApplication.translate
DE.setWindowTitle(_translate("DE", "Title"))
self.Run.setText(_translate("DE", "Run"))
self.GermanyBrowse.setText(_translate("DE", "Browse"))
def getfile(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
None,
"Select file",
"",
"*.xlsx", )
return fileName
def runFile(self):
import file2 # --- File2.py
file2.name_of_file(file2.x) # +++ (file2.x)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
DE = QtWidgets.QDialog()
ui = Ui_DE()
ui.setupUi(DE)
DE.show()
sys.exit(app.exec_())
file2.py
from PyQt5 import QtWidgets # +++
import file1 # - DailyMCI, + file1
#x = DailyMCI.Ui_DE.getfile(GermanyBrowse) # ---
x = file1.Ui_DE.getfile(file1.Ui_DE) # +++
def name_of_file(str):
alert = QtWidgets.QMessageBox()
alert.setText("You have opnened {str}".format(str=str)) # I have python 3.5 (f"You have opnened {str}")
alert.exec_()