Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

tkinter - How do I get a text (string) value from an entrybox in a child window into a variable

I am trying to use a menu command to open a child window containing an entrybox and a Button. The entrybox will receive text input (a name). When the button is clicked the name must be passed to to a function to save it to a table in the database. I have tried many options but none works properly. Any assistance will be appreciated.Here is my code:

#Extract from creation of menu

menubar = tk.Menu(root)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Add New Princple", command=OpenNewPrinciple)

root.config(menu=menubar)

#The part that opens the New window. This works fine

def OpenNewPrinciple():
        
        AddNewWindow=tk.Toplevel(root)
        AddNewWindow.title("Add a principle")
        AddNewWindow.geometry("500x100")
        AddNewWindow.grid_columnconfigure(0,weight=1)
        frame3=ttk.Frame(AddNewWindow)
        frame3.grid(column=0, padx=10,sticky="news")
        l_Principle_name=ttk.Label(AddNewWindow,text="Principle's name", width =30)
        l_Principle_name.grid(row = 10, column=0,sticky="w",padx=5,pady=5)
        e_Principle_name=ttk.Entry(AddNewWindow, width = 30)
        e_Principle_name.grid(row = 10, column=1,sticky="w",padx=5, pady=5)
        b_SavePrinciple=ttk.Button(AddNewWindow, text="Save Name", width =25,command=Save_Principle)
        b_SavePrinciple.grid(row=12, column=1, padx=5, pady =20,sticky="w")
        e_Principle_name.focus()

#The part that has to receive the value of the entrybox in the New window. This does not work.

def Save_Principle():

        print("The principle's name is:" + str(Var) 
        Id=LastId
        Id=Id+1
         
        conn= sqlite3.connect(r'/home/bushbug/Databases/TrackMyInv')
        cur=conn.cursor()

        my_data=(6,Var)
        
        Sql="INSERT INTO Principles (Id,PName) VALUES (?,?)"
   
        cur.execute(Sql,my_data)
        conn.commit()
        msgbox.showinfo(title="Data saved",message="Your record has been successfuly saved")
        b_Save.config(state=tk.DISABLED)
       
        cur.close()
        conn.close()
question from:https://stackoverflow.com/questions/65651746/how-do-i-get-a-text-string-value-from-an-entrybox-in-a-child-window-into-a-var

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...