【发布时间】:2018-11-12 09:24:39
【问题描述】:
我做了一个桌面应用程序 使用一个按钮 (button_on_click_getfile),您可以选择一个文件。 使用另一个按钮(button_on_click_work),您应该使用该文件。 在处理文件之前,我需要用户的一些输入,因此我需要将文件名从 button_on_click_getfile 传递给 button_on_click_work。
如何传递文件名。我的代码有效,但是当我按下 button_on_click_work 时返回以下内容:
Process finished with exit code -1073740791 (0xC0000409)
这是我的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QGridLayout, QWidget, QDesktopWidget
from tkinter import messagebox
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, QAction, QMessageBox
from PyQt5.QtWidgets import QCalendarWidget, QFontDialog, QColorDialog, QTextEdit, QFileDialog
from PyQt5.QtWidgets import QCheckBox, QProgressBar, QComboBox, QLabel, QStyleFactory, QLineEdit, QInputDialog
from PyQt5.QtWidgets import QTabWidget
from file_to_text2 import convert_file_to_txt2
from excel_function import work_file_with_excel
OUTPUT_FILENAME = 'test.txt'
class main_window(QTabWidget):
def __init__(self, parent=None):
super(main_window, self).__init__(parent)
self.setGeometry(50,50, 1078, 541)
self.setWindowTitle("Lea\'s Program")
qtRectangle = self.frameGeometry()
centerPoint = QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
self.centralWidget = QtWidgets.QWidget()
self.tabWidget = QtWidgets.QTabWidget(self.centralWidget)
self.tabWidget.setGeometry(QtCore.QRect(10, 10, 1200, 1000))
self.tabWidget.setLayoutDirection(QtCore.Qt.LeftToRight)
self.tabWidget.setTabPosition(QtWidgets.QTabWidget.West)
self.tab_v1 = QtWidgets.QWidget()
self.addTab(self.tab_v1, "Read")
self.openFile = QPushButton("Get file", self.tab_v1)
self.openFile.setGeometry(QtCore.QRect(700, 15, 200, 30))
self.openFile.clicked.connect(self.on_click_getfile)
self.path_file = QLabel("",self.tab_v1)
self.path_file.setGeometry(QtCore.QRect(200, 15, 350, 30))
self.groupbox = QGroupBox('Informationen', self.tab_v1)
self.groupbox.setGeometry(QtCore.QRect(15, 100, 1000, 600))
self.w_pz = QLineEdit(self.groupbox)
self.w_pz.setGeometry(QtCore.QRect(212, 150, 250, 22))
self.w_pn = QLineEdit(self.groupbox)
self.w_pn.setGeometry(QtCore.QRect(212, 200, 250, 22))
self.work_file = QtWidgets.QPushButton("Search", self.tab_v1)
self.work_file.setGeometry(QtCore.QRect(700, 250, 150, 30))
self.work_file.clicked.connect(self.on_click_work)
def on_click_getfile(self):
fname = QFileDialog.getOpenFileName(self,
'Open file',
'c:\\',
'Alle LV-Check-Datein,(*.txt *.docx *.xml *.x81 *.pdf)'
#'Alle Datein (*.*)'
)
filename = fname[0]
self.path_file.setText(filename)
print (filename)
return filename # with this i want to pass the filename to on_click_work
def on_click_work(self):
if len (self.w_pz.text()) < 1:
messagebox.showinfo("Information", "Please put something in")
elif len (self.w_pn.text()) < 1:
messagebox.showinfo("Information", "Please put something in")
else:
input_lv =([self.w_pz.text(),self.w_pn.text()])
print (input_lv)
filename = on_click_getfile(filename) ##this should get the filename from the on_click_getfile function
print (filename)
convert_file_to_txt2(filename,input_lv, output_filename=OUTPUT_FILENAME) # this should start running convertig different filetypes depending on the filename, which was put in at teh on_click_getfile function
work_file_with_excel(OUTPUT_FILENAME) # this should get the outputfile from convert_file_to_txt2 and run a search
def main():
app = QApplication(sys.argv)
ex = main_window()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
【问题讨论】:
-
请将您的代码减少到 [minimal.完整且可验证](www.stackoverflow.com/help/mcve) 示例,不要只发布其中的一部分,因为它没有任何意义。谢谢!
-
我编辑了这篇文章。你现在能帮我吗?
-
我认为您应该使用
self.on_click_getfile()调用您的函数,而不是将filename作为参数传递(因为您的函数on_click_getfile没有参数)。
标签: python function pyqt parameter-passing