0

I have a program which involves several classes as frames in tkinter. I have a separate file that uses pygame and is a chess game. I want to be able to load this file up and execute it when I press a certain tkinter button in one of the frames and to then close this file. I am aware that importing the module at the beginning of the tkinter file runs it immediately which is not what I need. I only want it to run when the button is pressed.

I have attempted to use other modules which do not work. I have tried using functions which also did not work.

I expect to be able to run the chess file when the button is pressed, instead of immediately as this holding file is ran. However, when run the chess file executes immediately and this other file does not even run.

Jaydeep Galani
  • 4,842
  • 3
  • 27
  • 47
  • 1
    importing module doesn't run the function immediately. if your function is `myfunction` in `mymodule` use `import mymodule`, then can execute it if a button is clicked : `tk.Button(text='click', command=mymodule.myfunction)`. take a look on this : https://stackoverflow.com/questions/41447065/tkinter-calling-function-when-button-is-pressed – Masoud May 31 '19 at 12:27
  • You can put `import` statements in functions and they will not execute until the function is called. To use this with `tkinter`, put the `import` in the callback function specified via the `command=` option when creating the `Button` widget. – martineau May 31 '19 at 13:05
  • Why would you bother with importing when you press a button. Just list all your imports at the top of your code and be done with it. That said if you have your imports in a function that are activated on button click then you end up importing the same over and over with each click. That is unnecessary. – Mike - SMT May 31 '19 at 14:05

3 Answers3

0

If you want to open the file in the native editor, the same as double clicking on windows, you could call os.startfile(file). That runs the file.

Example:

os.startfile('C:\\Windows\\System32\\notepad.exe')

That would run notepad.

Legorooj
  • 2,646
  • 2
  • 15
  • 35
0

Although I wouldn't recommend doing so, you can make a function that will import a module after you press the button as follows:

Button(command=my_func)

def my_func():
    import foo, bar

However, if you simply do not wish to run functions from a foreign file, put the following code in the file:

if __name__ == "__main__:
    <Code that gets run>

And simply call the function you wish to call:

import bar
bar.foo()

Note that importlib is the better option for importing external files.

Geetansh G
  • 17
  • 7
-1

If you enclose your main Python code in if __name__ == "__main__":, then it will run only if the file is executed, not loaded as the module. It's a common practice.

And if you make your spam.py like that:

def main():
    do_things_here

if __name__ == "__main__":
    main()

You can just import spam at the beginning with nothing happening and do spam.main() when you want to execute it.

But if you really want to load modules dynamically, exec("import " + module_name)

h4z3
  • 5,265
  • 1
  • 15
  • 29
  • A better way to programmaticaly import module is using the `importlib` module : https://docs.python.org/3/library/importlib.html#importlib.import_module You should use `importlib.import_module()` instead of `exec`. – Thibault D. May 31 '19 at 12:31
  • @pLOPeGG Thanks! I'm fairly new to Python myself and never had to use it myself, I got this exec from a book and as I'm now looking at it, the issue presented there was loading modules by name (string, e.g. typed by used). ^_^ – h4z3 May 31 '19 at 12:35
  • I believe `exec` is overkill and not needed for importing modules for any reason. Not to mention `exec` is most often best avoided as it is not a secure option. – Mike - SMT May 31 '19 at 14:07
  • @8goalsIsDecent not sir ;) not all programmers are guys – h4z3 May 31 '19 at 14:10
  • @Mike-SMT well, as I said, it was an example of loading modules by names in string form. In a book which previous release was for Python2, so it might've been older example mentioned in Python3 book. ;) Second importing by name in string in the book was done by `var_module = __import__(module_name_as_str)` – h4z3 May 31 '19 at 14:13
  • @h4z3 those examples may have existed and probably for good reason however I would avoid showing beginners the use of `exec` as this can be a vulnerable bit of code if they use it improperly. Also `exec` makes it really hard to troubleshoot when something goes wrong. – Mike - SMT May 31 '19 at 14:17