I am new to tkinter and trying to make an invoice application using this library. The app will read an Excel file using the Browse button and then save the Excel data into MySQL database which is working fine. However, when I press the Report button, it's not displaying the record on new window and getting halt all the time. Even a new window won't open, it should display the record on the current window. Here is my code:
# Import the required Libraries
from tkinter import *
from tkinter import ttk, filedialog
from tkinter.filedialog import askopenfilename
import mysql.connector
import xlrd
import tkinter as tk
from tkinter import filedialog
def browse_button():
# Allow user to select a directory and store it in global var
# called folder_path
global folder_path
filename = filedialog.askopenfilename()
folder_path.set(filename)
print(filename)
def show_new_window():
print("Show new window called")
# Create the new window
new_window = tk.Toplevel(app)
new_window.title("Report")
# Create a label in the new window
label = tk.Label(new_window, text="Delivery Report")
label.pack()
# Connect to the database
conn = mysql.connector.connect(user='root',
password='',
host='localhost',
database='magnetico')
mycursor = conn.cursor()
mycursor.execute("SELECT id_user, Vendor, KM FROM perorder")
# Get the results
result = mycursor.fetchall()
# Close the cursor and connection
mycursor.close()
conn.close()
# Create a button for each row in the result
for row in result:
button = tk.Button(new_window, text=str(row))
button.pack()
# Set the geometry of tkinter frame
app = tk.Tk()
app.geometry("900x800")
folder_path = tk.StringVar()
# Add a heading
heading = tk.Label(app, text="Click the Button to browse the Files", font=("Helvetica", 16))
heading.grid(row=0, column=1, columnspan=2)
lbl1 = tk.Label(app, textvariable=folder_path)
lbl1.grid(row=1, column=1, padx=10, pady=10, sticky=tk.NSEW)
# Center the button
button2 = tk.Button(app, text="Browse", command=browse_button)
button2.grid(row=1, column=2, padx=10, pady=10, sticky=tk.NSEW)
button = tk.Button(app, text="Report", command=show_new_window)
button.grid(row=2, column=2, padx=10, pady=10, sticky=tk.NSEW)
# Set the row and column weights
app.columnconfigure(1, weight=1)
app.columnconfigure(2, weight=1)
app.rowconfigure(1, weight=1)
app.mainloop()
# Connect to the database
conn = mysql.connector.connect(user='root',
password='',
host='localhost',
database='magnetico')
# Open the workbook
book = xlrd.open_workbook("mpo1.xls")
# Get the sheet you want
sheet = book.sheet_by_name("Order Details")
# Iterate through the rows of the sheet
for row_idx in range(sheet.nrows):
# Get the values in the row
row = sheet.row_values(row_idx)
# Insert the row into the database
cursor = conn.cursor()
cursor.execute("INSERT INTO perorder (mp_task_nr, batch_id, dropoff_sequence, id_user_payout_type, task_status, city, fleet, id_user, Name, Vendor, order_date, UID, KM, total_amount, Remarks) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s )", row)
conn.commit()
Related
I have created a small python tkinter program designed to have a user write data into text entry boxes in the app then insert this data into a MS SQL server database. When clicking the insert button I am getting the following error message, stopping the app from insert the code into SQL.
cursor.execute("INSERT INTO TEST_DISA_MAIN_TABLE VALUES (:DENSITY_VALUE, :DATE, :TIME, :CHART_VALUE)",
pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000')*
This is my code for the python insert function:
def insertvalue():
# Create a database or connect to one
conn = pyodbc.connect('Driver={SQL Server};'
'Server=MON-SQL-02;'
'Database=ENVIRONMENTAL;'
'Trusted_Connection=yes;')
# Create cursor
cursor = conn.cursor()
# Insert into table
cursor.execute("INSERT INTO TEST_DISA_MAIN_TABLE VALUES (:DENSITY_VALUE, :DATE, :TIME, :CHART_VALUE)",
{
'DENSITY_VALUE': DENSITY_VALUE.get(),
'DATE': DATE.get(),
'TIME': TIME.get(),
'CHART_VALUE': CHART_VALUE.get(),
})
# Commit changes
conn.commit()
#Close Connection
conn.close()
# Clear the text boxes
DENSITY_VALUE.delete(0, END)
DATE.delete(0, END)
TIME.delete(0, END)
CHART_VALUE.delete(0, END)
#Create text boxes
DENSITY_VALUE = Entry(root, width=30)
DENSITY_VALUE.grid(row=2, column=1)
DATE = Entry(root, width=30)
DATE.grid(row=3, column=1)
TIME = Entry(root, width=30)
TIME.grid(row=4, column=1)
CHART_VALUE = Entry(root, width=30)
CHART_VALUE.grid(row=5, column=1)
#Create Text box labels
DENSITY_VALUE_Label = Label(root, text="Density Value")
DENSITY_VALUE_Label.grid(row=2, column=0)
DATE_Label = Label(root, text="Date")
DATE_Label.grid(row=3, column=0)
TIME_Label = Label(root, text="Time")
TIME_Label.grid(row=4, column=0)
CHART_VALUE_Label = Label(root, text="Chart Value")
CHART_VALUE_Label.grid(row=5, column=0)
button = Button(root, text = "Show All Values", command = showvalues)
button.grid(row=0, column=0)
button = Button(root, text = "Insert Values", command = insertvalue)
button.grid(row=1, column=0)
#Commit changes
conn.commit()
# Close connection
conn.close()
root.mainloop()
I used this format to insert data into a sqlite3 db, but the same format is not working when inserting into a SQL Server database. I have tested the connection to the SQL server and can display values in the app so that part works correctly.
I'm trying to find a way to create a search bar for a table connected to a DB Browser database. The search bar I'm looking for is the one where you type in the bar, and the program will "hide" any data that does not follow the "requirements" while leaving the ones that do visible. The following code shows what I've tried so far. Hopefully, I made myself clear, and thank you for your help. I don't know how to attach the database so sorry for the inconvenience.
If you remove the code between the '''Triple Apostrophes,''' you should see a table with a button that says SELECT which reveals the data. The Table shown is where I would like the Search bar to control.
Thank you for your help once again.
from tkinter import ttk
import tkinter as tk
from tkinter import *
import sqlite3
ws = Tk()
ws.title('Directory')
ws.geometry('600x400')
ws.attributes('-topmost', True)
ws.attributes('-fullscreen', True)
'''
def Scankey(event):
val = event.widget.get()
print(val)
if val == '':
data = Table1
else:
data = []
for item in Table1:
if val.lower() in item.lower():
data.append(item)
Update(data)
def Update(data):
listbox.delete(0, 'end')
# put new data
for item in data:
listbox.insert('end', item)
ws = Tk()
entry = Entry(was)
entry.pack()
entry.bind('<KeyRelease>', Scankey)
listbox = Listbox(was)
listbox.pack()
Update()
# Connect to the database
def connect():
conn = sqlite3.connect("MEMBERinformation.db")
cur = conn.cursor()
cur.execute(
"CREATE TABLE IF NOT EXISTS ChurchMemberInformation(MemberID TEXT PRIMARY KEY, "
"FirstName TEXT, LastName TEXT)")
conn.commit()
conn.close()
# Populate Treeview table
def View():
conn = sqlite3.connect("MEMBERinformation.db")
cur = conn.cursor()
cur.execute("SELECT FirstName, LastName FROM ChurchMemberInformation")
rows = cur.fetchall()
for row in rows:
print(row) # print all records in the database
tree.insert("", tk.END, values=row)
conn.close()
#Frame
Table1 = Frame(was)
Table1.pack(anchor=CENTER)
# Connect the database
connect()
# Create Treeview widget
tree = ttk.Treeview(Table1, column=("column1", "column2",), show='headings')
tree['columns'] = ('FIRST NAME', 'LAST NAME')
tree.column('FIRST NAME', width=100)
tree.heading("#1", text="FIRST NAME")
tree.column('LAST NAME', width=100)
tree.heading("#2", text="LAST NAME")
tree.pack()
# Create and pack buttons
button1 = tk.Button(text="SELECT", command=View)
button2 = tk.Button(text="QUIT", command=ws.destroy)
button1.pack()
button2.pack()
ws.mainloop()
I am building a database for tools and matrials list in Python using Tkinter for the GUI. I am running into issues when I try to edit data. Everything works until I click the save button in the editor window. It says: sqlite3.ProgrammingError: You did not supply a value for binding 1. Can anyone see what I am doing wrong here?
Here is my code:
from tkinter import *
import sqlite3
mud = Tk()
mud.title("Mud Data")
mud.geometry("400x600")
# Create database
conn = sqlite3.connect('well_sav.db')
# Create cursor
c = conn.cursor()
# Create table
# c.execute("""CREATE TABLE mud (
# mud_type text,
# mud_weight real ,
# mud_viscosity real,
# mud_pit_number real
# )""")
# Create Submit Function for DB
def submit():
# Connect to DB
conn = sqlite3.connect('well_sav.db')
# Create cursor
c = conn.cursor()
# Insert into table
c.execute("INSERT INTO mud VALUES (:mud_type, :mud_weight, :mud_viscosity, :mud_pit_number)",
{
'mud_type': mud_type.get(),
'mud_weight': mud_weight.get(),
'mud_viscosity': mud_viscosity.get(),
'mud_pit_number': mud_pit_number.get()
})
# Commit changes
conn.commit()
# Close connection
conn.close()
# Clear The Text Boxes
mud_type.delete(0, END)
mud_weight.delete(0, END)
mud_viscosity.delete(0, END)
mud_pit_number.delete(0, END)
# Function to edit a record
def edit():
# Create global variables
global editor
global mud_type_editor
global mud_weight_editor
global mud_viscosity_editor
global mud_pit_number_editor
editor = Tk()
editor.title("Edit mud")
editor.geometry("400x200")
conn = sqlite3.connect('well_sav.db')
c = conn.cursor()
record_id = delete_box.get()
c.execute("SELECT * FROM mud WHERE oid = " + record_id)
records = c.fetchall()
mud_type_editor = Entry(editor, width=30)
mud_type_editor.grid(row=0, column=1, pady=(10, 0))
mud_weight_editor = Entry(editor, width=30)
mud_weight_editor.grid(row=1, column=1)
mud_viscosity_editor = Entry(editor, width=30)
mud_viscosity_editor.grid(row=2, column=1)
mud_pit_number_editor = Entry(editor, width=30)
mud_pit_number_editor.grid(row=3, column=1)
# Create Text box Label
mud_type_label = Label(editor, text="Mud Type")
mud_type_label.grid(row=0, column=0, pady=(10, 0))
mud_weight_label = Label(editor, text="Mud Weight")
mud_weight_label.grid(row=1, column=0)
mud_viscosity_label = Label(editor, text="Mud Viscosity")
mud_viscosity_label.grid(row=2, column=0)
mud_pit_number_label = Label(editor, text="Mud Pit Number")
mud_pit_number_label.grid(row=3, column=0)
# Loop through results
for record in records:
mud_type_editor.insert(0, record[0])
mud_weight_editor.insert(0, record[1])
mud_viscosity_editor.insert(0, record[2])
mud_pit_number_editor.insert(0, record[3])
# Create save button
edit_button = Button(editor, text="Save Update", command=update)
edit_button.grid(row=7, column=1, pady=5, padx=5, ipadx=98)
conn.commit()
conn.close()
# Fucntion for updates
def update():
conn = sqlite3.connect('well_sav.db')
c = conn.cursor()
record_id = delete_box.get()
c.execute("""UPDATE mud SET
mud_type = :name,
mud_weight = :length,
mud_viscosity = :inside_diameter,
mud_pit_number = :outside_diameter
WHERE oid = :oid""",
{
'mud_type': mud_type_editor.get(),
'mud_weight': mud_weight_editor.get(),
'mud_viscosity': mud_viscosity_editor.get(),
'mud_pit_number': mud_pit_number_editor.get(),
'oid': record_id
})
conn.commit()
conn.close()
editor.destroy()
# Function to delete a record
def delete():
conn = sqlite3.connect('well_sav.db')
c = conn.cursor()
c.execute("DELETE FROM mud WHERE oid = " + delete_box.get())
conn.commit()
conn.close()
# Create Query Function
def query():
# Connect to DB
conn = sqlite3.connect('well_sav.db')
# Create cursor
c = conn.cursor()
# Query the DB
c.execute("SELECT *, oid FROM mud")
records = c.fetchall()
# print(records)
# Loop through results
print_records = ''
for record in records:
print_records += str(record[0]) + "\t " + str(record[1]) + \
"\t " + str(record[2]) + "\t " + \
str(record[3]) + str(record[4]) + "\n"
query_label = Label(mud, text=print_records)
query_label.grid(row=20, column=0, columnspan=2)
# Commit changes
conn.commit()
# Close connection
conn.close()
# Math Functions
def volume_per_foot(bha_result_text):
bha_gallons_per_foot = float(mud_viscosity.get()) * \
float(mud_viscosity.get()) / 1029.4
bha_result_text.set(str(bha_gallons_per_foot))
# Create Text Boxes
mud_type = Entry(mud, width=30)
mud_type.grid(row=0, column=1, pady=(10, 0))
mud_weight = Entry(mud, width=30)
mud_weight.grid(row=1, column=1)
mud_viscosity = Entry(mud, width=30)
mud_viscosity.grid(row=2, column=1)
mud_pit_number = Entry(mud, width=30)
mud_pit_number.grid(row=3, column=1)
delete_box = Entry(mud, width=30)
delete_box.grid(row=6, column=1)
# Create Text box Label
mud_type_label = Label(mud, text="Mud Type")
mud_type_label.grid(row=0, column=0, pady=(10, 0))
mud_weight_label = Label(mud, text="Mud Weight")
mud_weight_label.grid(row=1, column=0)
mud_viscosity_label = Label(mud, text="Mud Viscosity")
mud_viscosity_label.grid(row=2, column=0)
mud_pit_number_label = Label(mud, text="Pit Number")
mud_pit_number_label.grid(row=3, column=0)
delete_box_label = Label(mud, text="Select ID")
delete_box_label.grid(row=6, column=0)
# Create Submit Button
submit_button = Button(mud, text="Save", command=submit)
submit_button.grid(row=4, column=1, pady=5, padx=5, ipadx=121)
# Create Query Button
query_button = Button(mud, text="Show Muds", command=query)
query_button.grid(row=5, column=1, pady=5, padx=5, ipadx=79)
# Create edit button
edit_button = Button(mud, text="Edit Muds", command=edit)
edit_button.grid(row=7, column=1, pady=5, padx=5, ipadx=87)
# Create delete button
delete_button = Button(mud, text="Delete Mud", command=delete)
delete_button.grid(row=8, column=1, pady=5, padx=5, ipadx=80)
# Commit changes
conn.commit()
# Close connection
conn.close()
mud.mainloop()
And here is the error message:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "mud.py", line 123, in update
c.execute("""UPDATE mud SET
sqlite3.ProgrammingError: You did not supply a value for binding 1.
Hi #LoudEye and welcome to Stack Overflow! Try using ? instead of : like this:
c.execute("UPDATE mud SET mud_type=?,mud_weight = ?, mud_viscosity=?, mud_pit_number = ? WHERE...",(mud_type_editor.get(), mud_weight_editor.get(),mud_viscosity_editor.get(),mud_pit_number_editor.get()))
Note: You should also use WHERE also with question mark like I've used with SET
thanks in advance for any help!
Ive been looking online for a solution and have struggled to find one. Im trying to save some data from a entry text box on tkinter to a table in SQLite3 but am getting the same error every time no matter what I try. Code below.
please note, I only started to learn coding from youtube videos a few days ago. apologies if this is really simple!
**python file code**
from tkinter import *
import sqlite3
import tkinter.messagebox
# connect to database
conn = sqlite3.connect("RMS.db")
c = conn.cursor()
# tkinter window
class Application:
def __init__(self, master):
self.master = master
# Creating frames in the master
self.left = Frame(master, width=200, height=60, bg="grey")
self.left.pack(side=LEFT)
self.right = Frame(master, width=0, height=0, bg="grey")
self.right.pack(side=RIGHT)
# labels for window
self.driverlevel = Label(self.left, text="Driver Level:", font="calabri 12 bold", fg="white", bg="grey")
self.driverlevel.place(x=1, y=2)
# Entries for left window
self.driverlevel_ent = Entry(self.left, width=20)
self.driverlevel_ent.place(x=5, y=27)
# Save Button
self.submit = Button(self.left, text="Add", font="calabri 12 bold", fg="white", bg="grey", command=self.addlevel)
self.submit.place(x=140, y=15)
def addlevel(self):
self.val1 = self.driverlevel_ent.get()
if self.val1 == "":
tkinter.messagebox.showinfo("Warning", "Please Enter a Value")
else:
sql = "INSERT INTO 'driverlevel' (Level,) VALUES(?,)"
c.execute(sql, (self.val1))
conn.commit()
tkinter.messagebox.showinfo("Success", "Driver Level Added")
# objects
root = Tk()
b = Application(root)
root.geometry("200x60+0+0")
root.resizable(False, False)
root.title("Add Driver Level")
root.mainloop()
**error message when saving**
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/*****/PycharmProjects/******/driverlevel.py", line 41, in addlevel
c.execute(sql, (self.val1))
sqlite3.OperationalError: near ")": syntax error
no idea how to overcome this..
Thanks, Josh
You inserted , after Level, and ?, which was suppose to be this "INSERT INTO 'driverlevel' (Level) VALUES(?) then insert the comma , at the end of (self.val1,) to see it as a tuple because you can insert data into sqlite3 as tuple
You can also insert the data this way
sql = "INSERT INTO driverlevel (Level) VALUES(?,)"
conn = sqlite3.connect("RMS.db")
c = conn.cursor()
c.execute("INSERT INTO driverlevel(level)VALUES(?)", (self.val1,))
conn.commit()
tkinter.messagebox.showinfo("Success", "Driver Level Added")
Make sure you closed the db after insertion.
Full code
from tkinter import *
import sqlite3
import tkinter.messagebox
# connect to database
conn = sqlite3.connect("RMS.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS driverlevel(level text, age text)")
conn.commit()
conn.close()
# tkinter window
class Application:
def __init__(self, master):
self.master = master
# Creating frames in the master
self.left = Frame(master, width=200, height=60, bg="grey")
self.left.pack(side=LEFT)
self.right = Frame(master, width=0, height=0, bg="grey")
self.right.pack(side=RIGHT)
# labels for window
self.driverlevel = Label(self.left, text="Driver Level:", font="calabri 12 bold", fg="white", bg="grey")
self.driverlevel.place(x=1, y=2)
# Entries for left window
self.driverlevel_ent = Entry(self.left, width=20)
self.driverlevel_ent.place(x=5, y=27)
# Save Button
self.submit = Button(self.left, text="Add", font="calabri 12 bold", fg="white", bg="grey", command=self.addlevel)
self.submit.place(x=140, y=15)
def addlevel(self):
self.val1 = self.driverlevel_ent.get()
if self.val1 == "":
tkinter.messagebox.showinfo("Warning", "Please Enter a Value")
else:
#sql = "INSERT INTO driverlevel (Level) VALUES(?,)"
# conn = sqlite3.connect("RMS.db")
# c = conn.cursor()
# c.execute("INSERT INTO driverlevel(level)VALUES(?)", (self.val1,))
# conn.commit()
# tkinter.messagebox.showinfo("Success", "Driver Level Added")
conn = sqlite3.connect("RMS.db")
c = conn.cursor()
sql = "INSERT INTO 'driverlevel' (Level) VALUES(?)"
c.execute(sql, (self.val1,))
conn.commit()
conn.close()
tkinter.messagebox.showinfo("Success", "Driver Level Added")
# objects
root = Tk()
b = Application(root)
root.geometry("200x60+0+0")
root.resizable(False, False)
root.title("Add Driver Level")
root.mainloop()
Am trying to display content entered into entry widget in treeview after saving it in sqlite3 db.The content saved into db but doesn't display the id,Fist name and Surname contents in the treeview.
Your suggestion are welcome to achieve this.
from tkinter import ttk
import tkinter as tk
import sqlite3
def connect():
conn = sqlite3.connect("TRIAL.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY,
First TEXT, Surname TEXT)")
conn.commit()
conn.close()
def Insert():
conn = sqlite3.connect("TRIAL.db")
cur = conn.cursor()
fr=cur.execute("INSERT INTO profile (First, Surname) VALUES(?, ?)",
(first_text.get(), surname_text.get()))
conn.commit()
for items in fr:
tree.insert('', tk.END, values=items)
conn.close()
connect() # this to create the db
root = tk.Tk()
root.geometry("400x400")
tree= ttk.Treeview(root, column=("column", "colunn1"))
tree.heading("#0", text="NUMBER")
tree.heading("#1", text="FIRST NAME")
tree.heading("#2", text="SURNAME")
tree.pack()
first_text = tk.StringVar()
e1 = tk.Entry(root, textvariable=first_text)
e1.pack()
surname_text = tk.StringVar()
e2 = tk.Entry(root, textvariable=surname_text)
e2.pack()
b1 = tk.Button(text="add data", command=Insert)
b1.pack(side=tk.BOTTOM)
root.mainloop()
The problem in your code is that for items in fr does not work. Python sees fr as an empty iterable (put a print statement in the for loop and you will see that it is never executed). So, to insert the data in the treeview, you can get it directly from the entries and retrieve the db id with cur.lastrowid (I have found this solution in the answer to the question How to retrieve inserted id after inserting row in SQLite using Python?):
from tkinter import ttk
import tkinter as tk
import sqlite3
def connect():
conn = sqlite3.connect("TRIAL.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY, First TEXT, Surname TEXT)")
conn.commit()
conn.close()
def Insert():
conn = sqlite3.connect("TRIAL.db")
cur = conn.cursor()
data = (first_text.get(), surname_text.get())
# insert data in db
cur.execute("INSERT INTO profile (First, Surname) VALUES(?, ?)", data)
conn.commit()
# insert data in treeview
tree.insert('', tk.END, text=str(cur.lastrowid), values=data)
conn.close()
connect() # this to create the db
root = tk.Tk()
root.geometry("400x400")
tree= ttk.Treeview(root, column=("column", "colunn1"))
tree.heading("#0", text="NUMBER")
tree.heading("#1", text="FIRST NAME")
tree.heading("#2", text="SURNAME")
tree.pack()
first_text = tk.StringVar()
e1 = tk.Entry(root, textvariable=first_text)
e1.pack()
surname_text = tk.StringVar()
e2 = tk.Entry(root, textvariable=surname_text)
e2.pack()
b1 = tk.Button(text="add data", command=Insert)
b1.pack(side=tk.BOTTOM)
root.mainloop()
EDIT: If you use the columns '#1', '#2', '#3' to avoid using the special
column '#0', then you need to change a bit Insert: you need to pass the row id in values instead of text.
from tkinter import ttk
import tkinter as tk
import sqlite3
def connect():
conn = sqlite3.connect("TRIAL.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY, First TEXT, Surname TEXT)")
conn.commit()
conn.close()
def Insert():
conn = sqlite3.connect("TRIAL.db")
cur = conn.cursor()
data = (first_text.get(), surname_text.get())
# insert data in db
cur.execute("INSERT INTO profile (First, Surname) VALUES(?, ?)", data)
conn.commit()
# insert data in treeview
tree.insert('', tk.END, values=(str(cur.lastrowid),) + data)
conn.close()
connect() # this to create the db
root = tk.Tk()
root.geometry("400x400")
tree = ttk.Treeview(root, column=("column1", "column2", "column3"), show='headings')
tree.heading("#1", text="NUMBER")
tree.heading("#2", text="FIRST NAME")
tree.heading("#3", text="SURNAME")
tree.pack()
first_text = tk.StringVar()
e1 = tk.Entry(root, textvariable=first_text)
e1.pack()
surname_text = tk.StringVar()
e2 = tk.Entry(root, textvariable=surname_text)
e2.pack()
b1 = tk.Button(text="add data", command=Insert)
b1.pack(side=tk.BOTTOM)
root.mainloop()