Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

python - Sending Given Inputs to Database by using MongoDB

I am designing an application by using MongoDB and Tkinter. I am trying to take inputs from user and send them to the database. Here is my code

def inputFunc():
    inputWindow = Tk()
    inputWindow.title("Mü?teri Ekleme Ekran?")
    inputWindow.geometry("500x500")
    
    submitButton = Button(
        inputWindow,
        text = "Mü?teriyi Ekle",
        width = 30,
        height = 5,
        fg = "DarkOrange1",
        bg = "green3",
        command = addClientFunc)
    submitButton.place(x=150,y=350)
    
    c_id = Label(inputWindow, text = "Mü?teri Numaras?").place(x = 30,y = 50)
    c_name = Label(inputWindow, text = "Mü?teri Ad?").place(x = 30,y = 70)  
    c_firmName = Label(inputWindow, text = "Firma Ad?").place(x = 30,y = 90)
    address = Label(inputWindow, text = "Adres").place(x = 30,y = 110)
    engine = Label(inputWindow, text = "Motor Markas?").place(x = 30,y = 130)
    power = Label(inputWindow, text = "Motor Gücü").place(x = 30,y = 150)
    phoneNumber = Label(inputWindow, text = "Telefon Numaras?").place(x = 30,y = 170)
    email = Label(inputWindow, text = "E-mail").place(x = 30, y = 190) 
    workHour = Label(inputWindow, text = "?al??ma Saati").place(x = 30,y = 210)
    
    idEntry = Entry(inputWindow).place(x = 180, y = 50)
    nameEntry = Entry(inputWindow).place(x = 180, y = 70)
    firmNameEntry = Entry(inputWindow).place(x = 180, y = 90)
    addressEntry = Entry(inputWindow).place(x = 180, y = 110)
    engineEntry = Entry(inputWindow).place(x = 180, y = 130)
    powerEntry = Entry(inputWindow).place(x = 180, y = 150)
    phoneNumberEntry = Entry(inputWindow).place(x = 180, y = 170)
    emailEntry = Entry(inputWindow).place(x = 180, y = 190)
    workHourEntry = Entry(inputWindow).place(x = 180, y = 210)
    
def addClientFunc():
    ins_table_Clients.insert_one({
        "c_id": idEntry.get(),
        "c_name": nameEntry.get(),
        "c_firmName": firmNameEntry.get(),
        "address": addressEntry.get(),
        "engine": engineEntry.get(),
        "power": powerEntry.get(),
        "phoneNumber": phoneNumberEntry.get(),
        "email": emailEntry.get(),
        "workHour": workHourEntry.get()
    })  

I am getting an error which says idEntry is not defined. First I thought that is about making idEntry global but that didn't work. What can I do to fix that? Appreciated for your helps.

question from:https://stackoverflow.com/questions/65921291/sending-given-inputs-to-database-by-using-mongodb

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are two issues in your code:

  1. all Entry widgets are local variables inside inputFunc() which cannot be accessed inside addClientFunc(). You can move addClientFunc() into inputFunc() as a nested function.

  2. all the entry variables are assigned the result of place(), so they are all None. You need to separate the line like idEntry = Entry(...).place(...) into two lines: idEntry = Entry(...) and idEntry.place(...).

Below is the modified code:

def inputFunc():
    def addClientFunc():
        ins_table_Clients.insert_one({
            "c_id": idEntry.get(),
            "c_name": nameEntry.get(),
            "c_firmName": firmNameEntry.get(),
            "address": addressEntry.get(),
            "engine": engineEntry.get(),
            "power": powerEntry.get(),
            "phoneNumber": phoneNumberEntry.get(),
            "email": emailEntry.get(),
            "workHour": workHourEntry.get()
        })  

    inputWindow = Tk()
    inputWindow.title("Mü?teri Ekleme Ekran?")
    inputWindow.geometry("500x500")
    
    submitButton = Button(
        inputWindow,
        text = "Mü?teriyi Ekle",
        width = 30,
        height = 5,
        fg = "DarkOrange1",
        bg = "green3",
        command = addClientFunc)
    submitButton.place(x=150,y=350)
    
    Label(inputWindow, text = "Mü?teri Numaras?").place(x = 30,y = 50)
    Label(inputWindow, text = "Mü?teri Ad?").place(x = 30,y = 70)  
    Label(inputWindow, text = "Firma Ad?").place(x = 30,y = 90)
    Label(inputWindow, text = "Adres").place(x = 30,y = 110)
    Label(inputWindow, text = "Motor Markas?").place(x = 30,y = 130)
    Label(inputWindow, text = "Motor Gücü").place(x = 30,y = 150)
    Label(inputWindow, text = "Telefon Numaras?").place(x = 30,y = 170)
    Label(inputWindow, text = "E-mail").place(x = 30, y = 190) 
    Label(inputWindow, text = "?al??ma Saati").place(x = 30,y = 210)
    
    idEntry = Entry(inputWindow)
    idEntry.place(x = 180, y = 50)
    nameEntry = Entry(inputWindow)
    nameEntry.place(x = 180, y = 70)
    firmNameEntry = Entry(inputWindow)
    firmNameEntry.place(x = 180, y = 90)
    addressEntry = Entry(inputWindow)
    addressEntry.place(x = 180, y = 110)
    engineEntry = Entry(inputWindow)
    engineEntry.place(x = 180, y = 130)
    powerEntry = Entry(inputWindow)
    powerEntry.place(x = 180, y = 150)
    phoneNumberEntry = Entry(inputWindow)
    phoneNumberEntry.place(x = 180, y = 170)
    emailEntry = Entry(inputWindow)
    emailEntry.place(x = 180, y = 190)
    workHourEntry = Entry(inputWindow)
    workHourEntry.place(x = 180, y = 210)

    inputWindow.mainloop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...