I made a destop application With one button (button_on_click_getfile) you can select a file. With the other button(button_on_click_work) you are supposed to work the file. 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.
How can I pass the filename. My code works, but returns the below when i press the 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.
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
In def on_click_getfile(self):
self.setProperty("get_filename",get_filename)
#you can Name with an appropriate Name.
In def on_click_work(self):
get_filename = self.property("get_filename")
# you get the valiable by the same Name.
In Qt programming, it is very important to know & think the parent& child relationship,and Connection with each 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.
making self. object in constructor at first
you should make a file object self.current_filename in the Constructor(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
That is to say, to pass valiable between two functions is not needed.
you make self.filename
object at first. 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.
In this case, it will be easier than Signal&Slot.
User contributions licensed under CC BY-SA 3.0