【问题标题】:Python - Passing variable filename in pyqt between functionsPython - 在函数之间传递pyqt中的变量文件名
【发布时间】: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


【解决方案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是非常重要的。

这些解决方案不是很好,因为它不是专门用于提高您的技能。 但是当您对小部件的连接和传递变量感到困惑时,两者都非常有用。

自我。首先是构造函数中的对象

你应该在构造函数中创建一个文件对象 self.current_filename(class 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

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

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

【讨论】:

    猜你喜欢
    • 2013-04-09
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2019-06-04
    相关资源
    最近更新 更多