Python Forum

Full Version: what is wrong with my code?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
from PyQt5.QtWidgets import QMainWindow, QApplication,QFileDialog,QLabel
import os

def get_file_name(self):

        file_filter= "data file (*.SQLite3)"
        fname = QFileDialog.getOpenFileName(
            parent=self,
            caption="select a file",
            directory=os.getcwd(),
            filter=file_filter
        )

        print(fname)
get_file_name()
Error:
TypeError: get_file_name() missing 1 required positional argument: 'self'
Should be pretty self-explanatory: on line 6, you declare that the function has a single parameter (called self), but on line 17, you aren't passing a value for it.
Why is your parameter called self? That's usually what you name the first parameter to a method in a class, but you have no class here.
It looks like "get_file_name()" is a method of a QWidget or QMainWindow subclass, not a standalone function. I think it was meant to be used like this:
class MyWindow(Qwidget):
    ...
    def get_file_name(self):
        ...

window = MyWindow()
window.get_file_name()
Python converts "window.get_file_name()" into "MyWindow.get_file_name(window)".
and you should construct a QApplication

from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QLabel
import os

class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.setGeometry(100, 100, 400, 300)
        
        
    def get_file_name(self):
        file_filter= "data file (*.sqlite *.sqlite3 *.db)"
        fname,_ = QFileDialog.getOpenFileName(
            parent=None,
            caption="select a file",
            directory=os.getcwd(),
            filter=file_filter
        )
        if fname:
            print(fname)
        else:
            print("cancelled")

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    window.get_file_name()
    sys.exit(app.exec_())
(Apr-26-2022, 06:12 PM)Axel_Erfurt Wrote: [ -> ]and you should construct a QApplication

your app do work on its own, but i changed my approach, i made a ui , and converted it into code and add two lines , it worked, but i want to add something into "the bracket" , when i add them it will crash

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication,QFileDialog

class Ui_MainWindow(object):

####################################################
    def get_file_name(self):

        fname = QFileDialog.getOpenFileName() ####when i want to add these (self,"","","all file (*)") contents into the bracket, the app crashes
       
        print(fname[0])
 ##########################################################        
         


    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(532, 563)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 532, 21))
        self.menuBar.setObjectName("menuBar")
        self.menuopen = QtWidgets.QMenu(self.menuBar)
        self.menuopen.setObjectName("menuopen")
        MainWindow.setMenuBar(self.menuBar)
        self.actionopen_a_sqlite_file = QtWidgets.QAction(MainWindow)
        self.actionopen_a_sqlite_file.setObjectName("actionopen_a_sqlite_file")

        self.actionopen_a_sqlite_file.triggered.connect(self.get_file_name)#################### 

        self.menuopen.addAction(self.actionopen_a_sqlite_file)
        self.menuBar.addAction(self.menuopen.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.menuopen.setTitle(_translate("MainWindow", "open"))
        self.actionopen_a_sqlite_file.setText(_translate("MainWindow", "open a sqlite file"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
(Apr-26-2022, 11:28 AM)ndc85430 Wrote: [ -> ]Why is your parameter called self? That's usually what you name the first parameter to a method in a class, but you have no class here.

see my new reply , see if you got some thouoghts on it Wink
(Apr-26-2022, 05:12 PM)deanhystad Wrote: [ -> ]It looks like "get_file_name()" is a method of a QWidget or QMainWindow subclass, not a standalone function. I think it was meant to be used like this:
class MyWindow(Qwidget):
    ...
    def get_file_name(self):
        ...

window = MyWindow()
window.get_file_name()
Python converts "window.get_file_name()" into "MyWindow.get_file_name(window)".


see my new reply , see if you got some thouoghts on it Wink
Ui_MainWindow is a window in name only. It is not a subclass of QWidget or QMainWindow.

MainWindow is can be the parent for your file dialog. You could do this:
    def get_file_name(self):
         fname = QFileDialog.getOpenFileName(MainWindow, ...) 
         print(fname[0])
...
(Apr-27-2022, 03:14 AM)deanhystad Wrote: [ -> ]Ui_MainWindow is a window in name only. It is not a subclass of QWidget or QMainWindow.

MainWindow is can be the parent for your file dialog. You could do this:
    def get_file_name(self):
         fname = QFileDialog.getOpenFileName(MainWindow, ...) 
         print(fname[0])
...

what you say is right, problem solved thanks
Pages: 1 2