简体   繁体   English

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

[英]Python - Passing variable filename in pyqt between functions

I made a destop application With one button (button_on_click_getfile) you can select a file. 我制作了一个destop应用程序,只需单击一个按钮(button_on_click_getfile),即可选择一个文件。 With the other button(button_on_click_work) you are supposed to work the file. 使用另一个按钮(button_on_click_work),您应该处理文件。 I need some input from the user before working the file, therefore i need to pass the filename from button_on_click_getfile to button_on_click_work. 在处理文件之前,我需要用户输入一些信息,因此我需要将文件名从button_on_click_getfile传递给button_on_click_work。

How can I pass the filename. 如何传递文件名。 My code works, but returns the below when i press the button_on_click_work: 我的代码有效,但是当我按下button_on_click_work时返回以下内容:

Process finished with exit code -1073740791 (0xC0000409)

Here is my code: 这是我的代码:

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()

Probably ,you can think of These solutions in this Code. 也许您可以在本《准则》中想到这些解决方案。

Global Statement 全球声明

You write global filename in each functions. 您在每个函数中写入global filename With this, You can use the both of filename. 这样,您就可以同时使用文件名。 Please pay Attention to duplicate the Name & overwrite. 请注意复制名称并覆盖。 Please don't Forget to write this statement in just front of valiables. 请不要忘记在变量之前写此声明。

QObject's property QObject的属性

In def on_click_getfile(self): def on_click_getfile(self):

self.setProperty("get_filename",get_filename) #you can Name with an appropriate Name. self.setProperty("get_filename",get_filename) #您可以使用适当的名称进行命名。

In def on_click_work(self): def on_click_work(self):

get_filename = self.property("get_filename") # you get the valiable by the same Name. get_filename = self.property("get_filename") #您将获得具有相同名称的变量。

In Qt programming, it is very important to know & think the parent& child relationship,and Connection with each Widget. 在Qt编程中,了解和思考父子关系以及与每个Widget的连接非常重要。

These solutions are not very good because it is not dedicated to enhancing your skill. 这些解决方案不是很好,因为它不是专门用于增强您的技能的。 But both are very useful when you are confused with widget's Connection & passing valiables. 但是,当您与小部件的Connection和传递变量混淆时,两者都非常有用。

making self. 自我。 object in constructor at first 首先在构造函数中的对象

you should make a file object self.current_filename in the Constructor(class main_window) 您应该在构造函数中创建一个文件对象self.current_filename(类main_window)

-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

That is to say, to pass valiable between two functions is not needed. 也就是说,不需要在两个函数之间传递变量。 you make self.filename object at first. 首先要创建self.filename对象。 and when you get filename ,set it to self.filename .And last, you use it in the Methods.I think this is the best solution for this Problem. 当你得到filename ,将其设置为self.filename 。最后,在方法中使用它。我认为这是解决此问题的最佳方法。

In this case, it will be easier than Signal&Slot. 在这种情况下,它将比Signal&Slot容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM