I am writing an application in python using tkinter as the GUI framework. I am saving lists menu commands in an SQL table and iterating through all of the selected rows to build the menu. So far this is working well but I cannot get the command to work when the menu item is clicked. The SQL table has 4 columns ID, Label, Command, Parent. so and example row for the table would look like
1, New, placeholder(), rootFile
When I click on the "New" command, nothing happens. If I replace row[2] with placeholder(), it works as expected, unfortunately this does not allow me to specify different commands for each menu item stored in the SQL database.
import pyodbc #needed for database connectivity
from tkinter import * #imports GUI objects
from tkinter import ttk #imports themed widgets for GUI
from database_connect import SQL_CONNECT
setupSQL = SQL_CONNECT('Server=MILLER2021;','Database=DRAWINGTOOL_DB;') #establish connection to SQL database that contains startup info for applicaton
setupCursor = setupSQL.cursor() #creates a cursor object for the SQL connection for setting up application
root = Tk() #creates top level window widget
root.title("Drawing Tool") #defines the title of root
root.geometry("400x300") #define the size of root
def placeholder():
print("This is an ouptut from a placeholder function")
##Define Menu Bar##
rootMenuBar = Menu(root)
rootFile = Menu(rootMenuBar, tearoff=0)
rootMenuBar.add_cascade(label="File", menu=rootFile) #displays item on menu
setupCursor.execute("SELECT * FROM MENU_COMMANDS WHERE PARENT LIKE 'rootFile'") #selects all commands from SQL database to be added to rootFile menu
for row in setupCursor: #iterate through all rows selected from previous cursor execute query
rootFile.add_command(label=row[1], command=row[2]) #adds new item to the file dropdown
root.config(menu=rootMenuBar) #displays menuBar in root menu
root.mainloop() #keeps code running
command=expects function's name without(). Second: it has to be real name, not string"placeholder"from database. So keeping function's name in database is not so good idea. It may needscommand = globals()["placeholder"]to get real access to functionplaceholder.