Python 如何管理代码,使代码的所有数据库部分存储在模块中,前端部分存储在主代码中

Python 如何管理代码,使代码的所有数据库部分存储在模块中,前端部分存储在主代码中,python,python-3.x,Python,Python 3.x,我的整个代码太长了,所以我决定创建一个模块来执行代码中的所有数据库部分 那么,在执行我的主代码时是否有可能 模块被调用 模块识别主代码中的值 以下是主要代码: from tkinter import * from quesm import extfile def register(): w1 = Tk() w1.geometry('400x530') l3 = Label(w1, text='User Name').place(x=25, y=40) e3

我的整个代码太长了,所以我决定创建一个模块来执行代码中的所有数据库部分

  • 那么,在执行我的主代码时是否有可能
  • 模块被调用
  • 模块识别主代码中的值
  • 以下是主要代码:

    from tkinter import *
    from quesm import extfile
    
    
    def register():
        w1 = Tk()
        w1.geometry('400x530')
    
        l3 = Label(w1, text='User Name').place(x=25, y=40)
        e3 = Entry(w1)
        e3.place(x=30, y=80 - 10)
    
        b2 = Button(w1, text='Register', command=extfile.detail_fetch)
                                   # While Clicking Button "extfile.detail_fetch" gets Called
    
    
        b2.place(x=30, y=450)
    
        w1.mainloop()
    
    register()
    
    b2 = Button(w1, text='Register', command = lambda: extfile.detail_fetch(e3))
    
    下面是模块(extfile):


    问题是您已经在主代码中初始化了“e3”。因此,您的模块无法找到它。您可以做的是使e3成为函数的参数,因此当您调用它时,您将e3传递给它。比如:

    class extfile:
        def detail_fetch(e3):
            uid = e3.get()
    
    然后,对于主代码:

    from tkinter import *
    from quesm import extfile
    
    
    def register():
        w1 = Tk()
        w1.geometry('400x530')
    
        l3 = Label(w1, text='User Name').place(x=25, y=40)
        e3 = Entry(w1)
        e3.place(x=30, y=80 - 10)
    
        b2 = Button(w1, text='Register', command=extfile.detail_fetch)
                                   # While Clicking Button "extfile.detail_fetch" gets Called
    
    
        b2.place(x=30, y=450)
    
        w1.mainloop()
    
    register()
    
    b2 = Button(w1, text='Register', command = lambda: extfile.detail_fetch(e3))
    

    在哪里向函数传递参数?e3不是可以在任何地方访问的全局变量;它具有本地作用域。您是否尝试过修改OP的主代码以通过e3(命令参数赋值)并运行它?如果不使用lambda函数就无法将参数传递给Tkinter中的函数,则会出现一个错误。是的,我会将其添加到我的答案中。忘了那部分。