Python-在函数之间的pyqt中传递变量文件名

时间:2018-11-12 09:24:40

标签: python function pyqt parameter-passing

我提出了阻止申请 使用一个按钮(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()

1 个答案:

答案 0 :(得分:1)

可能,您可以在本《准则》中想到这些解决方案。

全球声明

您在每个函数中写global filename。 这样,您就可以同时使用文件名。 请注意复制名称并覆盖。 请不要忘记在变量之前写此声明。

QObject的属性

def on_click_getfile(self):

self.setProperty("get_filename",get_filename)#您可以使用适当的名称进行命名。

def on_click_work(self):

get_filename = self.property("get_filename")#,您会得到同名的变量。

在Qt编程中,了解和思考父子关系以及与每个Widget的连接非常重要。

这些解决方案不是很好,因为它并不是专门用来增强您的技能的。 但是当您与小部件的Connection和传递变量混淆时,两者都是非常有用的。

自我创造。首先在构造函数中添加对象

您应该在Constructor(类main_window)中将文件对象设置为self.current_filename

-omitting-
self.current_filename = ""#here
def on_click_getfile(self):
        fname = QFileDialog.getOpenFileName(self,
                                            'Open file',
                                            'c:\\',
                                            'Alle LV-Check-Datein,(*.txt *.docx *.xml *.x81 *.pdf)'
                                            #'Alle Datein (*.*)'
                                            )
        self.filename = fname[0]#here this can be
        self.path_file.setText(self.filename)
        print (filename)

        return filename`# you may delete it.




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(self.filename#here,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

也就是说,不需要在两个函数之间传递变量。 您首先要创建self.filename对象。当您得到filename时,将其设置为self.filename。最后,您可以在“方法”中使用它。我认为这是解决此问题的最佳方法。

在这种情况下,它将比Signal&Slot容易。