0

I am making a sort of OmniCode Tkinter app for all my beginner python programs. All it does is look in my beginner-code file and produce a list of modules (just the string names for now) to be imported (using importlib.import_module()) and executed at the press of a Tkinter button.

Two problems I'm having now are that iteratively creating and passing command= kwargs to each button results in all the buttons executing only the last module in the aforementioned list.

The second problem is that interacting with this last module (if I understand the errors correctly) is impossible further than the initial "Hello! this programme does xyz" console response. It appears that after importing the module and executing, it is immediately closed, thus preventing any further interaction.

Here is my code:

Applications = [filename for filename in
                    os.listdir('C:/Users/****/PycharmProjects/Gui Test')
                        if isfile(filename)]


def OpenApp(app):
    importlib.import_module(app)


def App_Buttons(_root):
    i = 0
    for app in Applications:
        tk.Button(_root,
                  # width=15,
                  height=2,
                  relief='solid',
                  borderwidth=1,
                  text=str(app),
                  command=lambda: OpenApp(app),
                  font=("Century Gothic", 16),
                  ).grid(row=4 + i,
                         column=0,
                         padx=20,
                         pady=15)
        tk.Label(_root,
                 text=f'This is the description of App Name: {str(app)}',
                 font=("Century Gothic", 16)
                 ).grid(row=4 + i,
                        column=1,
                        sticky='w')
        i += 1
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Does this answer your question? [tkinter creating buttons in for loop passing command arguments](https://stackoverflow.com/questions/10865116/tkinter-creating-buttons-in-for-loop-passing-command-arguments) – TheLizzard Aug 23 '21 at 15:55
  • @TheLizzard It does solve the first problem, thank you! I still do get a Module Error, however: `ModuleNotFoundError: No module named 'Do it backwards.py'; 'Do it backwards' is not a package` , assuming 'Do it backwards' is a python script with no function definitions. – Nathan Ngqwebo Aug 23 '21 at 16:26
  • 2
    Is it in the correct directory? Also python files shouldn't have spaces or special characters in their names. Stick to whatever you can import using `import ...` – TheLizzard Aug 23 '21 at 16:28
  • @TheLizzard Now I'm doubly confused. After refactoring the names of all the programmes to look more like: "DoItBackwards", the list of modules is empty. It only populated the list when the files were named with spaces. – Nathan Ngqwebo Aug 23 '21 at 16:42
  • @NathanNgqwebo you need to use `import module_name` as such for example `import DoltBackwards` and then you can access stuff in that module by `DoltBackwards.SomeClass()` or just do `from DoltBackwards import SomeClass` to simply use as `SomeClass()`, you don't need to make any lists – Matiiss Aug 23 '21 at 17:13
  • But consider @Matiiss that by the end of this project I'll likely have 30 such mini programmes, and for reusability, I would really like to find a solution that ensures that I only ever import (and keep open) modules that have been selected on the main app window.\ If I imported all the small programmes by hand, I would lose the automatic fineness, I would lose scalability, and I would miss out on a learning opportunity. – Nathan Ngqwebo Aug 23 '21 at 18:23
  • @NathanNgqwebo possibly then an easier approach may be to use `subprocess.Popen` to open the file as a new process (it requires the computer to have python installed tho), the other option is to convert each small program to an executable file and launch them like such – Matiiss Aug 23 '21 at 18:38
  • Executables sounds interesting. I'll get working. Thank you @Matiiss! – Nathan Ngqwebo Aug 23 '21 at 18:58

0 Answers0