#πŸ”’ Linking tkinter Button commands to functions names stored as strings in a configuration file

4 messages Β· Page 1 of 1 (latest)

nocturne acorn
#

Hi there,

First off, my apologies. My title makes it seem like I am asking a straightforward question. TBH I don't actually know what title would suit my problem. I have also tried to search for a solution on Google, consulted AI and StackOverflow, and I am none the wiser. Which is why I am here.

Just to lay the groundwork. I have 2 files a) containing the Application code, and b) a Configurations file into which I have centralised all parameters and variables used throughout the application.

file: config.py

button = {
   "text": "name,
   "x": #,
   "y": #,
   "command": "name_of_the_function"
}

file MyApp.py

def myapp():
  root = tk.Tk()
 
  new_button = tk.Button(root, text = con.button["text"], **command = con.button["command"]**) # how I currently have it writen 

  new_button.pack()

  root.mainloop()

As you can see the command parameter draws on the respective dictionary key. The problem is that the function name I am calling is stored as a string. Additionally, I can't use an explicit reference in the config file as the function in question is not in the same file.

So far, the stringed name of the function is written such that if one were to remove the " ", the command parameter would immediately recognize the function it is being assigned to.

I am clearly not doing something right. Is there a method or a way of coding this such that I can store the function name in a dictionary as a string, which then gets set as a function when the widget gets created?

Your help is much appreciated.

Thank you

radiant coralBOT
#

@nocturne acorn

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

radiant coralBOT
#

@nocturne acorn

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.