从另一个 class 调用自变量(在另一个模块中)
Call self variable from another class (in another module)
我正在尝试调用 OpenFile() 中的变量“self.filename”,它通过另一个模块中的 QFileDialog(GUI)获取文件路径 db.py,AddData()(作为变量“url = ..”)。如何在另一个模块中调用名为 self.filename 的自变量?
#main.py
class App(QMainWindow):
def __init__(self):
super().__init__()
self.win = uic.loadUi(r"GUI.ui",self)
self.initUI()
self.win.show()
def initUI(self):
self.win.btOpen.clicked.connect(self.OpenFile)
def OpenFile(self):
self.filename=QFileDialog.getOpenFileName(self,"Open File",r"C:","csv File (*.csv)")
db.Database()
#db.py
class Database():
def __init__(self):
super().__init__()
self.db = sql.connect("data.db")
self.cur=self.db.cursor()
self.cur.execute("CREATE TABLE IF NOT EXISTS Table (Col1 real, Col2 real);")
self.AddData()
def AddData(self):
"""Add raw data to database"""
url=r"table.csv"
with open(url) as file:
dr = csv.DictReader(file)
to_db = [(i['Col1'], i['Col2']) for i in dr]
self.cur.executemany("INSERT INTO Table (Col1, Col2) VALUES (?, ?);", to_db)
self.db.commit()
为什么不直接将 filename
作为参数传递给 AddData
方法?
结果应与此类似:
#main.py
class App(QMainWindow):
def __init__(self):
super().__init__()
# initialize the database
self.database = db.Database()
#self.database.connect() # optionally: connect to your database once in a method separate from the constructor
self.win = uic.loadUi(r"GUI.ui",self)
self.initUI()
self.win.show()
def initUI(self):
self.win.btOpen.clicked.connect(self.OpenFile)
def OpenFile(self):
filename = QFileDialog.getOpenFileName(self,"Open File",r"C:","csv File (*.csv)")
self.database.addData(filename)
#db.py
class Database():
def __init__(self):
super().__init__()
self.db = sql.connect("data.db")
self.cur = self.db.cursor()
self.cur.execute("CREATE TABLE IF NOT EXISTS Table (Col1 real, Col2 real);")
#self.addData() # do not call addData here because there is no default file to load data from (at least it is not outlined in your question)
def addData(self, filename):
"""Add raw data parsed from a csv file to this database"""
url = filename
with open(url) as file:
dr = csv.DictReader(file)
to_db = [(i['Col1'], i['Col2']) for i in dr]
self.cur.executemany("INSERT INTO Table (Col1, Col2) VALUES (?, ?);", to_db)
self.db.commit()
还要注意始终如一地命名您的函数 (camelCase)。我将你的 AddData
重命名为 addData
。
我正在尝试调用 OpenFile() 中的变量“self.filename”,它通过另一个模块中的 QFileDialog(GUI)获取文件路径 db.py,AddData()(作为变量“url = ..”)。如何在另一个模块中调用名为 self.filename 的自变量?
#main.py
class App(QMainWindow):
def __init__(self):
super().__init__()
self.win = uic.loadUi(r"GUI.ui",self)
self.initUI()
self.win.show()
def initUI(self):
self.win.btOpen.clicked.connect(self.OpenFile)
def OpenFile(self):
self.filename=QFileDialog.getOpenFileName(self,"Open File",r"C:","csv File (*.csv)")
db.Database()
#db.py
class Database():
def __init__(self):
super().__init__()
self.db = sql.connect("data.db")
self.cur=self.db.cursor()
self.cur.execute("CREATE TABLE IF NOT EXISTS Table (Col1 real, Col2 real);")
self.AddData()
def AddData(self):
"""Add raw data to database"""
url=r"table.csv"
with open(url) as file:
dr = csv.DictReader(file)
to_db = [(i['Col1'], i['Col2']) for i in dr]
self.cur.executemany("INSERT INTO Table (Col1, Col2) VALUES (?, ?);", to_db)
self.db.commit()
为什么不直接将 filename
作为参数传递给 AddData
方法?
结果应与此类似:
#main.py
class App(QMainWindow):
def __init__(self):
super().__init__()
# initialize the database
self.database = db.Database()
#self.database.connect() # optionally: connect to your database once in a method separate from the constructor
self.win = uic.loadUi(r"GUI.ui",self)
self.initUI()
self.win.show()
def initUI(self):
self.win.btOpen.clicked.connect(self.OpenFile)
def OpenFile(self):
filename = QFileDialog.getOpenFileName(self,"Open File",r"C:","csv File (*.csv)")
self.database.addData(filename)
#db.py
class Database():
def __init__(self):
super().__init__()
self.db = sql.connect("data.db")
self.cur = self.db.cursor()
self.cur.execute("CREATE TABLE IF NOT EXISTS Table (Col1 real, Col2 real);")
#self.addData() # do not call addData here because there is no default file to load data from (at least it is not outlined in your question)
def addData(self, filename):
"""Add raw data parsed from a csv file to this database"""
url = filename
with open(url) as file:
dr = csv.DictReader(file)
to_db = [(i['Col1'], i['Col2']) for i in dr]
self.cur.executemany("INSERT INTO Table (Col1, Col2) VALUES (?, ?);", to_db)
self.db.commit()
还要注意始终如一地命名您的函数 (camelCase)。我将你的 AddData
重命名为 addData
。