0

I am trying to create A new folder and new data base file to be the startup page of an application, what i am looking for is to make the folder name and DB file name to be seen (globally)in all scripts. file 1

import os
import pandas as pd
from PyQt5.QtCore import *
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "Create a New Project"
        self.setGeometry(200, 200 , 700, 400)
        self.InitWindow()
    def InitWindow(self):
        self.setWindowTitle(self.title)
        self.labelA = QLabel('  New Project:', self)
        self.labelA.setGeometry(50, 50, 120, 30)
        self.labelA.setStyleSheet('QLabel { color: blue; background-color: lightgreen; font-family: Times New Roman ; font-weight: bold; font-size: 10pt}')
        self.lineA = QLineEdit(self)
        self.lineA.setGeometry(200, 50, 300, 30)
        self.lineA.setStyleSheet('QLineEdit { color: blue; background-color: lightgreen; font-family: Times New Roman ; font-weight: bold; font-size: 10pt}')
        #self.lineA.textChanged.connect(self.project)
        self.btn1 = QPushButton("Create", self)
        self.btn1.setStyleSheet('QPushButton { color: blue; background-color: lightgreen; font-family: Times New Roman ; font-weight: bold; font-size: 10pt}')
        self.btn1.setGeometry(600, 50, 70, 30)
        self.btn1.clicked.connect(self.project)
        self.labelc = QLabel('  DB File:', self)
        self.labelc.setGeometry(50, 250, 120, 30)
        self.labelc.setStyleSheet('QLabel { color: blue; background-color: lightgreen; font-family: Times New Roman ; font-weight: bold; font-size: 10pt}')
        self.linec = QLineEdit(self)
        self.linec.setGeometry(200, 250, 300, 30)
        self.linec.setObjectName("linec")
        self.linec.setStyleSheet('QLineEdit { color: blue; background-color: lightgreen; font-family: Times New Roman ; font-weight: bold; font-size: 10pt}')
        #self.linec.textChanged.connect(self.project)
        self.btn3 = QPushButton("Browse", self)
        self.btn3.setStyleSheet('QPushButton { color: blue; background-color: lightgreen; font-family: Times New Roman ; font-weight: bold; font-size: 10pt}')
        self.btn3.setGeometry(600, 250, 70, 30)
        self.btn3.clicked.connect(self.database_file)

    def project(self):
        Dirname = str(QFileDialog.getExistingDirectory(self, 'Select Directory'))
        if Dirname:
            self.lineA.setText(Dirname)
        self.database_file(Dirname)
        print(Dirname)
        return Dirname
    def database_file(self, dir_name):
        global path
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        Name, _ = QFileDialog.getSaveFileName(self, "Data Base File()", "",
                                                  "Data base Files (*.db)")
        if Name:
            file = open(Name,'w')
            self.linec.setText(Name)
        path = os.path.join(dir_name, Name )
        print(path)
        return path

if __name__ == '__main__':
    import sys
    App = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(App.exec_())

and then need to work with the database file and the project folder in all scripts considering that the folder and file names could be changed for every new project. and one of the other scripts maybe like this

import sys
import sqlite3
import pandas as pd
conn = sqlite3.connect('path')
data = pd.read_sql_query("select * from Curves_Calc_table ", conn)

data.to_csv('Dirname/data.csv',sep='\t' )

any advise will be appreciated

Sam Fathy
  • 3
  • 1
  • 3
  • How and when would the second file be accessed? – musicamante Apr 19 '20 at 17:29
  • if i added some data to database file using another script , the idea here is to pass project folder name and DB file name to any other python files within the app. – Sam Fathy Apr 19 '20 at 17:46
  • Please clarify what you mean by "any other python files within the app", as it doesn't seem to answer what I asked you. Files like that in your second example are probably going to be imported when the application is started, right? If so, there would be a conceptual problem: imports are normally done when the main script is loaded (and, usually, at the very beginning), which means that at that point `path` wouldn't exist yet or wouldn't be set yet. But I assume that you are going to write to the database only when required. – musicamante Apr 19 '20 at 17:53
  • imports are normally done when the main script is loaded (and, usually, at the very beginning), which means that at that point path wouldn't exist yet or wouldn't be set ((you are right)), i will try to turn around it – Sam Fathy Apr 19 '20 at 18:34
  • You'll probably need to rethink the project structure (at least partially). The most simple and logical solution is to do all database actions in functions, and call them using the path as one of their arguments. With this approach, you can still keep things in separate files and importing functions along with everything else won't be a problem. Another alternative could be to create a class that acts as an interface with the db, then create an instance which would then store its path. Also, consider using QSql models, like [QSqlTableModel](https://doc.qt.io/qt-5/qsqltablemodel.html). – musicamante Apr 19 '20 at 18:41
  • thanks, appreciated – Sam Fathy Apr 19 '20 at 19:59

0 Answers0