0

I am new to python, but have years of experience in C, C++, C#, and a few other languages. I am trying to write code that can be reused by different programs. Each program will have different number of buttons, different names, and different methods assigned to the button. All the data will be a json file. I able to create the correct number of buttons, assign the correct name, and even assign a correct tooltip. I am not able to dynamically assign to the command=. All the methods are defined.

My question is it possible to dynamically assign a string to command=? I think not, but would like someone with more python experience to tell me I can't. Here is the code:

  diagnosticButtons = get_diagnostic_buttons(str(Path(__file__).parent)) # get the list of diagnostic buttons
  xCoordinate = 20;
  yCoordinate = 20;
  iStep = 30
  iOffset = 60
  iXCount = 0;
  iYCount = 0;
 
  for i in diagnosticButtons:
    diagName =  i["name"]
    toolTip = i["tooltip"]
    method = i["method"]              # method name to assign to commmand
   
    xPosition = xCoordinate + (iOffset * iXCount)
    yPosition = yCoordinate + (iStep * iYCount)

    diagButton = Button(diagFrame, text=diagName, command=method, height=1, width=6)
    diagButton.place(x=xPosition, y=yPosition)
    Tooltip(diagButton, text=toolTip)  
   
    iYCount = iYCount + 1

    if iYCount == 12:                                               # end of the space
      iYCount = 0                                                   # set y back to beginning
      iXCount = iXCount + 1                                         # increment the x
  • So, when you say `method`, do you mean method or function? – quamrana Mar 07 '22 at 21:21
  • Do the answers found in this [question](https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string) help at all? – quamrana Mar 07 '22 at 21:23
  • Which GUI library do you use? – Diblo Dk Mar 08 '22 at 08:56
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 08 '22 at 08:56
  • Thanks quamrana you second comment was exactly what I was looking. I just needed to take a string with a names of methods in a class and assign them to buttons. name = i["method"] method = getattr(Tests, name) – pknight Mar 08 '22 at 13:42

0 Answers0