I would like to bring to your attention that my code is functioning perfectly; however, I require assistance with obtaining the selected item from the listbox to populate the last cell of the grid through a callback function. When I press the F2 key in the first column of the grid, it triggers the callback function, within which I attempt to invoke the listbox selection function to transfer the selected item into the last cell of the grid. Unfortunately, this transfer does not occur as expected. I would appreciate your guidance on how to successfully transfer the selected item from the listbox into the last cell of the grid.
from tkinter import *
import tkinter as tk
from tkinter import ttk
import pandas as pd
import sqlalchemy as sa
global mrowid
connection_string = ( r"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};" r"DBQ=C:erpmfapython.accdb;" r"ExtendedAnsiSQL=1;" )
connection_url = sa.engine.URL.create( "access+pyodbc", query={"odbc_connect": connection_string} )
engine = sa.create_engine(connection_url)
global i
class GUI(Frame):
def __init__(self, master, *args, **kwargs):
Frame.__init__(self, master, *args, **kwargs)
self.master = master
self.my_frame = Frame(self.master)
self.my_frame.pack()
self.button3 = Button(self.master, text="Chicken Purchse", command = self.open_toplevel_window1)
self.button3.pack()
def open_toplevel_window1(self):
self.top1new = Toplevel(self.master)
self.top1 = Frame(self.top1new)
self.top1.pack(side=TOP, fill=X)
self.framebot1 = Frame(self.top1new)
self.framebot1.pack(side=BOTTOM, fill=X)
self.i=4
rows = []
def add_row():
global i
self.i=self.i+1
items = []
var = IntVar()
c = Checkbutton(self.top1, variable = var)
c.val = var
items.append(c)
tt = int(self.i)
c.grid(row = self.i, column = 0)
for j in range(1,7): #Columns
b = Entry(self.top1)
items.append(b)
b.grid(row=self.i, column=j)
rows.append(items)
def testdf(event):
mlistv.set('A')
if mlistv.get() =='A' : listbox1.forget()
listbox.pack( side = BOTTOM)
listbox.focus()
def testdf1(event):
mlistv.set('B')
if mlistv.get() =='B' : listbox.forget()
listbox1.pack( side = BOTTOM)
listbox1.focus()
def selected_item(event):
if mlistv.get() =='A':
for i in listbox.curselection():
v8.set(listbox.get(i))
listbox.forget()
e8.focus()
mlistv.set('')
elif mlistv.get() =='B':
for i in listbox1.curselection():
v8.set(listbox1.get(i))
listbox1.forget()
mlistv.set('')
entry = self.top1.grid_slaves(row=4, column=3) # how to set the grid last selected column
entry.insert(0,v8.get()) # replace current cell data from selected listbox item
entry.focus() # how to setcofuz last cell where call the callback function
def callback(event):
widget = event.widget
info = widget.grid_info()
rowc = info['row']
colc = info['column']
entry = self.top1.grid_slaves(row=rowc, column=colc)[0]
mlistv.set('B')
if mlistv.get() =='B' : listbox.forget()
listbox1.pack( side = BOTTOM)
listbox1.focus()
# entry.delete(0, END)
# entry.insert(0,'muhamad is the best') # replace current cell data
# entry.focus()
def delete_row():
for rowno, row in reversed(list(enumerate(rows))):
if row[0].val.get() == 1:
for i in row:
i.destroy()
rows.pop(rowno)
i =4
labeltitle = tk.Label(self.top1, text='Remarks', bg="lightblue",width=10,font=("Arial", 12, "bold") )
labeltitle.grid(row =0, column=1)
labeltitler2 = tk.Label(self.top1, text='Remarks', bg="lightblue",width=10,font=("Arial", 12, "bold") )
labeltitler2.grid(row =2, column=1)
bt = Button(self.top1 , text = 'Add Row', command = add_row)
bt.grid(row =159, column=1)
myStringVar1 = StringVar()
myStringVar1.set('Muhammad is the best')
ent5 = Entry(self.top1, text="", textvariable=myStringVar1 , width=10)
myStringVar2 = StringVar()
myStringVar2.set('2')
ent6 = Entry(self.top1, text="", textvariable=myStringVar2 , width=10)
dl = Button(self.top1 , text = 'Delete Row', command = delete_row)
dl.grid(row =159, column=2)
v0 = StringVar()
e0 = Entry(self.top1, textvariable = v0, state = 'readonly')
v0.set('Select')
v1 = StringVar()
e1 = Entry(self.top1, textvariable = v1, state = 'readonly')
v1.set('Col1')
v2 = StringVar()
e2 = Entry(self.top1, textvariable = v2, state = 'readonly')
v2.set('Col2')
v3 = StringVar()
e3 = Entry(self.top1, textvariable = v3, state = 'readonly')
v3.set('Col3')
v4 = StringVar()
e4 = Entry(self.top1, textvariable = v4, state = 'readonly')
v4.set('Col4')
v9 = StringVar()
e9 = Entry(self.top1, textvariable = v4, state = 'readonly')
v9.set('Col4')
v10 = StringVar()
e10 = Entry(self.top1, textvariable = v10, state = 'readonly')
v10.set('Col4')
v5 = StringVar()
e5 = Entry(self.top1, textvariable = v5, state = 'readonly')
v5.set('Col4')
v6 = StringVar()
e6 = Entry(self.top1, textvariable = v6, state = 'readonly')
v6.set('Col4')
e6.grid(row = 1, column = 1 )
v7 = StringVar()
e7 = Entry(self.top1, textvariable = v7)
v7.set('Col4')
e7.grid(row = 1, column = 2 )
v8 = StringVar()
e8 = Entry(self.top1, textvariable = v8)
v8.set('Col4')
e8.grid(row = 1, column = 3 )
top_button = tk.Button(self.top1,text="Exit form System",command = root.destroy,activebackground="blue", activeforeground="white",font=("Arial", 16, "bold"), )
top_button.grid(row =160, column=3)
top_buttonp = tk.Button(self.top1,text="Print",command= printchartacc,activebackground="blue", activeforeground="white",font=("Arial", 16, "bold"), )
top_buttonp.grid(row =160, column=1)
self.top1.grab_set()
add_row()
self.top1new.bind('<F2>', callback)
mlistv= StringVar()
self.charttitle10 = Entry(self.top1, text="", textvariable=mlistv , width=15,font=("Arial", 12, "bold") , state = 'readonly' )
listbox = Listbox(self.framebot1, height = 10, width = 15, selectmode=tk.EXTENDED , activestyle = 'dotbox', font=('Times', 18) )
listbox1 = Listbox(self.framebot1, height = 10, width = 15, selectmode=tk.EXTENDED , activestyle = 'dotbox', font=('Times', 18) )
df = pd.read_sql("SELECT title FROM chartofacc where chartofacc.group in('Purchese','Sales') order by title", engine)
df1 = df['title']
listbox.insert(0, *df1)
df2 = pd.read_sql("SELECT title FROM chartofacc where chartofacc.group in('Chicken_Part') order by title", engine)
df3 = df2['title']
listbox1.insert(0, *df3)
self.top1new.bind('<F3>',testdf)
listbox.bind('<Return>',selected_item)
listbox1.bind('<Return>',selected_item)
if __name__ == "__main__":
root = Tk()
app = GUI(root)
root.mainloop()
1
In order to resolve your query of transferring the selected item from the listbox to the last cell of the grid, you need to do the modification at your callback function and selected_item function.
You need to ensure that the callback function identifies the correct cell.
After that you also need to update the selectced item in the correct cell after choosing from the listbox.
Here I have tried to resolve your query and the code is provided below:
from tkinter import *
import tkinter as tk
from tkinter import ttk
import pandas as pd
import sqlalchemy as sa
# Database connection
connection_string = (
r"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};"
r"DBQ=C:erpmfapython.accdb;"
r"ExtendedAnsiSQL=1;"
)
connection_url = sa.engine.URL.create(
"access+pyodbc", query={"odbc_connect": connection_string}
)
engine = sa.create_engine(connection_url)
class GUI(Frame):
def __init__(self, master, *args, **kwargs):
Frame.__init__(self, master, *args, **kwargs)
self.master = master
self.my_frame = Frame(self.master)
self.my_frame.pack()
self.button3 = Button(self.master, text="Chicken Purchase", command=self.open_toplevel_window1)
self.button3.pack()
def open_toplevel_window1(self):
self.top1new = Toplevel(self.master)
self.top1 = Frame(self.top1new)
self.top1.pack(side=TOP, fill=X)
self.framebot1 = Frame(self.top1new)
self.framebot1.pack(side=BOTTOM, fill=X)
self.i = 4
rows = []
def add_row():
self.i += 1
items = []
var = IntVar()
c = Checkbutton(self.top1, variable=var)
c.val = var
items.append(c)
c.grid(row=self.i, column=0)
for j in range(1, 7): # Columns
b = Entry(self.top1)
items.append(b)
b.grid(row=self.i, column=j)
rows.append(items)
def selected_item(event):
if mlistv.get() == 'B':
for i in listbox1.curselection():
v8.set(listbox1.get(i))
listbox1.forget()
mlistv.set('')
# Update the correct cell in the grid with the selected item
entry = self.top1.grid_slaves(row=self.rowc, column=self.colc + 2)[0]
entry.delete(0, END)
entry.insert(0, v8.get())
entry.focus()
def callback(event):
widget = event.widget
info = widget.grid_info()
self.rowc = info['row'] # Store the row number
self.colc = info['column'] # Store the column number
mlistv.set('B')
if mlistv.get() == 'B':
listbox.forget()
listbox1.pack(side=BOTTOM)
listbox1.focus()
def delete_row():
for rowno, row in reversed(list(enumerate(rows))):
if row[0].val.get() == 1:
for i in row:
i.destroy()
rows.pop(rowno)
# Creating widgets in the Toplevel window
labeltitle = tk.Label(self.top1, text='Remarks', bg="lightblue", width=10, font=("Arial", 12, "bold"))
labeltitle.grid(row=0, column=1)
labeltitler2 = tk.Label(self.top1, text='Remarks', bg="lightblue", width=10, font=("Arial", 12, "bold"))
labeltitler2.grid(row=2, column=1)
bt = Button(self.top1, text='Add Row', command=add_row)
bt.grid(row=159, column=1)
myStringVar1 = StringVar()
myStringVar1.set('Muhammad is the best')
ent5 = Entry(self.top1, text="", textvariable=myStringVar1, width=10)
myStringVar2 = StringVar()
myStringVar2.set('2')
ent6 = Entry(self.top1, text="", textvariable=myStringVar2, width=10)
dl = Button(self.top1, text='Delete Row', command=delete_row)
dl.grid(row=159, column=2)
v0 = StringVar()
e0 = Entry(self.top1, textvariable=v0, state='readonly')
v0.set('Select')
v1 = StringVar()
e1 = Entry(self.top1, textvariable=v1, state='readonly')
v1.set('Col1')
v2 = StringVar()
e2 = Entry(self.top1, textvariable=v2, state='readonly')
v2.set('Col2')
v3 = StringVar()
e3 = Entry(self.top1, textvariable=v3, state='readonly')
v3.set('Col3')
v4 = StringVar()
e4 = Entry(self.top1, textvariable=v4, state='readonly')
v4.set('Col4')
v9 = StringVar()
e9 = Entry(self.top1, textvariable=v4, state='readonly')
v9.set('Col4')
v10 = StringVar()
e10 = Entry(self.top1, textvariable=v10, state='readonly')
v10.set('Col4')
v5 = StringVar()
e5 = Entry(self.top1, textvariable=v5, state='readonly')
v5.set('Col4')
v6 = StringVar()
e6 = Entry(self.top1, textvariable=v6, state='readonly')
v6.set('Col4')
e6.grid(row=1, column=1)
v7 = StringVar()
e7 = Entry(self.top1, textvariable=v7)
v7.set('Col4')
e7.grid(row=1, column=2)
v8 = StringVar()
e8 = Entry(self.top1, textvariable=v8)
v8.set('Col4')
e8.grid(row=1, column=3)
top_button = tk.Button(self.top1, text="Exit form System", command=root.destroy, activebackground="blue", activeforeground="white", font=("Arial", 16, "bold"))
top_button.grid(row=160, column=3)
top_buttonp = tk.Button(self.top1, text="Print", command=printchartacc, activebackground="blue", activeforeground="white", font=("Arial", 16, "bold"))
top_buttonp.grid(row=160, column=1)
self.top1.grab_set()
add_row()
self.top1new.bind('<F2>', callback)
mlistv = StringVar()
self.charttitle10 = Entry(self.top1, text="", textvariable=mlistv, width=15, font=("Arial", 12, "bold"), state='readonly')
listbox = Listbox(self.framebot1, height=10, width=15, selectmode=tk.EXTENDED, activestyle='dotbox', font=('Times', 18))
listbox1 = Listbox(self.framebot1, height=10, width=15, selectmode=tk.EXTENDED, activestyle='dotbox', font=('Times', 18))
df = pd.read_sql("SELECT title FROM chartofacc WHERE chartofacc.group IN ('Purchase', 'Sales') ORDER BY title", engine)
df1 = df['title']
listbox.insert(0, *df1)
df2 = pd.read_sql("SELECT title FROM chartofacc WHERE chartofacc.group IN ('Chicken_Part') ORDER BY title", engine)
df3 = df2['title']
listbox1.insert(0, *df3)
self.top1new.bind('<F3>', lambda event: listbox.pack(side=BOTTOM) or listbox.focus())
listbox.bind('<Return>', selected_item)
listbox1.bind('<Return>', selected_item)
if __name__ == "__main__":
root = Tk()
app = GUI(root)
root.mainloop()
I hope this will resolve your query.