0

I have two .py scripts on the same folder, and I have a function like this on one:

def combat_menu():
    exec("combat.py")

Theese scripts are named dungeon.py(the one im executing from) and combat.py.

When I execute this function this error appears:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\Users\hp\Documents\piton\Dungeon\dungeon_Data\dungeon.py", line 25, in combat_menu
    exec("combat.py")
  File "<string>", line 1, in <module>
NameError: name 'combat' is not defined

I execute this function from a tkinter button widget like this:

event_button = t.Button(root, text="Open CM", command=combat_menu)

I have tried importing the code, but this gives ImportError.

Qolden
  • 3
  • 2

1 Answers1

1

The exec() function supports dynamic execution of Python code. It is not executing a shell command. To execute a shell command, in your case calling another python file. Instead you can use for example:

os.system(*cmd*) ->
os.system('combat.py')
Ronald
  • 2,930
  • 2
  • 7
  • 18