模块范围的变量python

时间:2018-01-16 10:13:56

标签: python tkinter module sqlite

我正在尝试从名为 SignIn 的模块中导入一个值,并在主模块中使用它来应用sqlite查询。该变量称为用户名,并在用户在tkinter窗口中输入时分配。我在python文件Password_database中也有一个数据库 passwordDbase ,其中包含所有用户名。如何在没有循环依赖/循环导入的情况下将此值导入另一个模块('因为我一直无法调用SignIn')。提前致谢。 SignIn看起来像:

import tkinter
def login():
    global username
    username=entry_user.get()
win=tkinter.Tk()
entry_user=tkinter.Entry(win)
#with a bunch of other tkinter functions to make textboxes etc

我的主程序如下:

import sqlite3
import SignIn
import Password_database
SignIn()
Password_database()
conn = sqlite3.connect ('passwordDbase.db')
c = conn.cursor()
username=SignIn.username
username=username.strip()
c.execute('''SELECT * FROM passwordDbase WHERE employee_username=?''',(username,))
rows = c.fetchall()
row = c.fetchone()

rows=c.fetchall()
if len(rows) != 1:
   message=' '
else:
   message= 'correct'

conn.commit()
c.close()
conn.close()

2 个答案:

答案 0 :(得分:0)

也许它没有解决你的问题,但我会以不同的方式组织所有。

mydatabase.py内部,我会保留与数据库一起使用的函数

import sqlite3

def get_user(username):

    conn = sqlite3.connect('passwordDbase.db')
    c = conn.cursor()

    c.execute('SELECT * FROM passwordDbase WHERE employee_username=?', (username,))
    conn.commit()

    rows = c.fetchall()

    if len(rows) != 1:
        message = 'error'
        data = None
    else:
        message = 'correct'
        data = rows[0]

    c.close()
    conn.close()

    return message, data


def check_pasword(username, password):
    pass


def other_function_on_database():
    pass

我会将其导入到tkinter

的代码中
import tkinter as tk
import mydatabase

# --- functions ---

def login():
    username = entry_username.get()
    password = entry_password.get()

    username = username.strip()

    message, data = mydatabase.login(username)
    result_label['text'] = message

    #if data and data[x] == password:
    #    result_label['text'] = "correct"

# --- main ---

win = tk.Tk()

entry_username = tk.Entry(win)
entry_username.pack()

entry_password = tk.Entry(win)
entry_password.pack()

button_login = tk.Button(win, text="Login", command=login)
button_login.pack()

result_label = tk.Label(win, text="")
result_label.pack()

win.mainloop()

这种带有tkinter的文件是主文件,我为数据库函数构建了GUI包装器。

答案 1 :(得分:0)

首先,为防止导入时执行代码,请使用

#mymodule.py
class MyModule(object):
    def __init__(self, *args, **kwargs):
        pass

def MyFunc(myparam):
    print myparam

if __name__ == "__main__":
    # this code gets executed when the module is called like
    # python mymodule.py
    print "MyModule called, not imported"

如果您希望在导入时执行它,请将其放在模块范围内,而不是“if __name__ == "__main__":”范围内。

其次,如果要从模块导入“静态”值,请在模块内定义它们,如:

#mymodule.py
MyModuleText="Text"
MyModuleList=["My", "Module", "List"]

class MyModule(object):
    def __init__(self, *args, **kwargs):
        pass

def MyFunc(myparam):
    print myparam

if __name__ == "__main__":
    # this code gets executed when the module is called like
    # python mymodule.py
    print "MyModule called, not imported"

然后使用

#!/usr/bin/python

import mymodule
print mymodule.MyModuleText
print mymodule.MyModuleList

这将从模块中访问变量。

如果要从模块运行功能,请调用它们。 要使用类,请创建实例。

#mymodule.py
MyModuleText="Text"
MyModuleList=["My", "Module", "List"]

class MyModule(object):
    def __init__(self, *args, **kwargs):
        pass
    def some_method(self, text):
        print "MyModule.some_func %s"%text

def MyFunc(myparam):
    print myparam

if __name__ == "__main__":
    # this code gets executed when the module is called like
    # python mymodule.py
    print "MyModule called, not imported"
#!/usr/bin/python

import mymodule

myinstance = mymodule.MyClass()
myinstance.some_method("SomeMethod from outside")

mymodule.MyFunc("MyModuleFunction called")