I am creating a tkinter GUI where I need to open an Access database and create a list of available tables in a combo box so that I can choose which table I want and it can be stored into a variable and then used in the query. I can’t get the combo box to show. If anyone can point me in the right direction.
from tkinter import *
from tkinter import ttk, filedialog
from tkinter.filedialog import askopenfile
import os
import pathlib
from tkinter.messagebox import showinfo
win = Tk()
Set the geometry of tkinter frame
win.title(‘D016 Database’)
win.geometry(“1000x800”)
def open_file():
file = filedialog.askopenfile(mode=‘r’, filetypes=[(‘MS Access File’, ‘*.accdb’)])
global dataf
if file:
filepath = os.path.abspath(file.name)
Label(win, text="The File is located at : " + str(filepath), font=(‘Aerial 8’)).pack()
conn = pyodbc.connect(r’Driver={Microsoft Access Driver (*.mdb, *.accdb)};‘r’DBQ=’+filepath+’;’)
#Get tables
cursor = conn.cursor()
for row in cursor.tables():
table_combo['values'] = row.table_name
#Create combobox
my_combo = ttk.combobox(win, values=table_combo)
my_combo(row=1, column=1, padx=10, pady=20)
my_combo.current(0)
my_combo.set(selected_table)
qry=("select * from selected_table")
dataf = pd.read_sql(qry, conn)
conn.close()
#Access database Label widget
label = Label(win, text=“Click the Button to browse the Files”, font=(‘Georgia 13’))
label.pack(pady=10)
#Access database table
label = Label(win, text=“Choose a table”, font=(‘Georgia 13’))
label.pack(pady=30)
Create a Browse Button
ttk.Button(win, text=“Browse”, command=open_file).pack(pady=20)
win.mainloop()